asp.net – 使用itemtemplate动态地将列添加到Grid-view
发布时间:2020-12-15 22:28:42 所属栏目:asp.Net 来源:网络整理
导读:我想知道如何将dynmically列添加到gridview.网格视图假设获得用户输入.我知道如何将itemtemplate用于特定的列数,但我不知道如何使用itemtemplate(文本框)字段动态添加列并生成数据绑定. 解决方法 您需要创建一个实现ITemplate的类,完整代码如下: public cla
我想知道如何将dynmically列添加到gridview.网格视图假设获得用户输入.我知道如何将itemtemplate用于特定的列数,但我不知道如何使用itemtemplate(文本框)字段动态添加列并生成数据绑定.
解决方法
您需要创建一个实现ITemplate的类,完整代码如下:
public class DynamicTemplateField : ITemplate { public void InstantiateIn(Control container) { //define the control to be added,i take text box as your need TextBox txt1 = new TextBox(); txt1.ID = "txt1"; container.Controls.Add(txt1); } } //Method to bind the Grid View public void BindData() { TemplateField temp1 = new TemplateField(); //Create instance of Template field temp1.HeaderText = "New Dynamic Temp Field"; //Give the header text temp1.ItemTemplate = new DynamicTemplateField(); //Set the properties **ItemTemplate** as the instance of DynamicTemplateField class. gv.Columns.Add(temp1); //add the instance if template field in columns of grid view //Bind the grid view gv.DataSource = [your data source]; gv.DataBind(); } 的RowDataBound protected void gv_RowDataBound(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { TextBox txt1 = e.Row.FindControl("txt1") as TextBox; txt1.Text = e.Row.DataItem["Name"]; //Assign any column value of your datasource } } .aspx页面 <asp:GridView ID = "gv" runat = "server" > <Columns> </Columns> </asp:GridView> 您可以操作DynamicTemplateField类以添加不同类型的控件 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么我的客户端去服务器来检查在使用ASP.NET MVC包时是否
- asp.net-mvc – 如何在ASP.NET MVC控制器中访问HttpServerU
- asp.net – 如何以程序方式检测漫游器
- asp.net-mvc – 在操作过滤器上使用缓存数据,以避免再次执行
- asp.net-mvc – 用于表行的Asp.Net MVC WebGrid帮助器自定义
- asp.net-mvc-3 – 在“@”字符后面出现意外的“foreach”关
- asp.net-mvc – 编写单元测试以验证所有ASP.NET MVC控制器是
- asp.net-mvc – MVC中的SSRS Reportviewer,通过自动调整ifr
- asp.net-mvc – 用于MVC的程序集中的资源的Bundle Script和
- asp.net-mvc-5 – 在MVC 5期货中找不到Html.Serialize助手
推荐文章
站长推荐
- 解决ASP.NET中的后台任务中的Autofac组件
- asp.net-mvc – asp.net mvc – 整数字段的可选/
- asp.net – 如何从.aspx页面检索查询字符串值并将
- asp.net – 在标题后添加Gridview行
- asp.net-mvc – ASP.NET MVC UpdateModel空属性
- asp.net – 是否可以为MaskedEditExtender设置掩
- ASP.NET网站Windows窗体应用程序WCF服务:客户端
- asp.net – 缓存用户控件时可以使用多个VaryByCu
- Asp网络核心 – 从模型到javascript
- asp.net-core – 外部包中的模型显示在swagger文
热点阅读