在GridView控件添加控件动态 [英] Adding controls in gridview dynamically
本文介绍了在GridView控件添加控件动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要控件添加到动态的GridView,所以我加了一个占位符,但它给我一个错误。
保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
占位符plachldr = e.Row.FindControl(PLACEHOLDER2)作为占位符;
按钮BTN =新按钮(){ID =btnShhow,文本=显示};
plachldr.Controls.Add(BTN); 占位符占位符= e.Row.FindControl(PLACEHOLDER1)作为占位符;
文本框TXT1 =新的TextBox();
placeholder.Controls.Add(TXT1);
}
同时加入了控制占位符,则是给我下面的错误:
对象引用未设置到对象的实例。
块引用>下面是我的GridView的标记:
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSEOnSelectedIndexChanging =GridView1_SelectedIndexChangingonrowdatabound =GridView1_RowDataBound>
<柱体和GT;
< ASP:BoundField的数据字段=名称的HeaderText =名称/>
< ASP:BoundField的数据字段=薪资的HeaderText =工资/>
< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:占位符ID =PLACEHOLDER1=服务器>< / ASP:占位符>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:占位符ID =PLACEHOLDER2=服务器>< / ASP:占位符>
< / ItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
< / ASP:GridView的>
解决方案您需要检查plachldr或占位符为空或不是,也检查
ROWTYPE
保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{ 如果(如果(e.Row.RowType == DataControlRowType.DataRow)
{
占位符plachldr = e.Row.FindControl(PLACEHOLDER2)作为占位符;
如果(plachldr!= NULL)
{
按钮BTN =新按钮(){ID =btnShhow,文本=显示};
plachldr.Controls.Add(BTN);
} 占位符占位符= e.Row.FindControl(PLACEHOLDER1)作为占位符;
如果(占位符!= NULL)
{
文本框TXT1 =新的TextBox();
placeholder.Controls.Add(TXT1);
}
}}I need to add controls to a GridView dynamically, so I added a PlaceHolder, but it it giving me an error.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { PlaceHolder plachldr = e.Row.FindControl("PlaceHolder2") as PlaceHolder; Button btn = new Button() { ID = "btnShhow", Text = "Show" }; plachldr.Controls.Add(btn); PlaceHolder placeholder = e.Row.FindControl("PlaceHolder1") as PlaceHolder; TextBox txt1 = new TextBox(); placeholder.Controls.Add(txt1); }
While adding the control to the PlaceHolder, is is giving me the following error:
Object reference not set to an instance of an object.
Here's the markup for my GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" onrowdatabound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Salary" HeaderText="Salary" /> <asp:TemplateField> <ItemTemplate> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
解决方案You need to check plachldr or placeholder is null or not and also check for the
RowType
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if( if (e.Row.RowType == DataControlRowType.DataRow) { PlaceHolder plachldr = e.Row.FindControl("PlaceHolder2") as PlaceHolder; if(plachldr!=null) { Button btn = new Button() { ID = "btnShhow", Text = "Show" }; plachldr.Controls.Add(btn); } PlaceHolder placeholder = e.Row.FindControl("PlaceHolder1") as PlaceHolder; if(placeholder!=null) { TextBox txt1 = new TextBox(); placeholder.Controls.Add(txt1); } } }
这篇关于在GridView控件添加控件动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文