日期:2014-05-17  浏览次数:20504 次

GridView动态生成模版列,并绑定模版列添加事件。
情况如下:

我根据条件,从多张表中查询得到一个DataTable。条件不一样,得到的DataTable的列也不一样,字段不一样,列数不一样。
所以必须动态生成Gridview。

1、Gridview中都是以模版列来绑定的,我要根据字段的不同绑定不同的模版列,大概是这个意思:

        if (templateType == "txt")
        {
            TextBox txtBox = new TextBox();
        }
        else if(templateType == "ddl")
        {
            DropDownList dropDownList = new DropDownList();
        }

其中DropDownList中的数据也是来自数据库,绑定的时候都要显示查询的结果。

2、在Gridview中对某个单元格进行编辑后,单元格背景色变为红色,以提醒用户哪个单元格进行过修改。例如:

3、最后点击保存按钮,将所有数据保存。

尝试了2天了,请大家帮帮忙,谢谢了。




------解决方案--------------------
直接输出吧,简单多了
------解决方案--------------------
不是有个编辑模板吗?
GRIDVIEW 增删改查

背景色变
protected void gvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#cdebed'");
            e.Row.Attributes.Add("onmousedown", " if (c!='#cdebed'){b=c;this.style.backgroundColor='#cdebed';c='#cdebed'}else {c=b}");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            
        }
    }

------解决方案--------------------
这样动态的最好不用GridView控件,转得比较晕。

后台输出table(类似下面代码)

前台脚本实现编辑效果

ajax 请求和提交数据


        public string GetSubListPage(int pageIdx)
        {
            StringBuilder sbUrlTmp = new StringBuilder();
            Model.View_SubList sbList = new View_SubList();

            int pageCount = 0;
            DataTable dt = GetSubListAndCount(pageIdx, out pageCount);
            sbUrlTmp.Append("<table id='tableTastList'><tr>");
   &nbs