使用占位符访问动态生成的gridview单元格的值 [英] Access value of dynamically generated gridview cells with placeholder
问题描述
大家好,
我有一个以下定义的gridview
< asp:GridView ID = timegrid runat = server AutoGenerateColumns = false ShowHeader = true OnRowDataBound = timegrid_RowDataBound OnRowCreated = timegrid_RowCreated >
< Columns>
\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\
< asp:TemplateField>
< ItemTemplate>
< asp:PlaceHolder ID = place1 runat = server > < / asp:PlaceHolder > \\\ \\ 5字段此类型
< / ItemTemplate >
< / asp:TemplateField >
< / 列 >
< / asp:GridView >
并使用文本框动态生成行并将其放在此占位符中,如下所示:
PlaceHolder pc1 =((PlaceHolder)e.Row.Cells [ 4 ]。控制[ 1 跨度>]);
TextBox tb = new TextBox();
tb.ID = tb_4;
tb.Width = 50 ;
tb.Style.Add( text-align, center);
pc1.Controls.Add(tb);
按预期工作正常。
现在我有一个位于gridview外面的按钮。单击此按钮,我想访问动态生成的控件以及其他gridview单元格值,我无法访问它。我试过像
TextBox textBox = timegrid.Rows [ 0 ]。单元格[ 4 ]。FindControl( tab_4) as TextBox;
string temp = textBox.Text; \\Object reference error
这里我只有一行有多列。我在突出显示的行中出现错误。这个控件在回发时失效(点击按钮)?如果是这样,如何避免?
怎么办?有什么建议吗??
下面是类似问题的链接,标记为已解决。请仔细阅读这些链接。希望它会有所帮助。
http://www.codeproject.com/Answers/599178/access-FindControl-plusdynamicallypluscreatedplusc#answer1
http://www.codeproject.com/Answers/596883/howpluscanplusiplusgetplusenteredplustextboxesplus#answer1
http://www.codeproject.com /答案/ 594571 / Ipluswantplustoplusretrieveplusandplussaveplusthep#ANSWER2
Hello every one,
I have a gridview with following definition
<asp:GridView ID="timegrid" runat="server" AutoGenerateColumns="false" ShowHeader="true" OnRowDataBound="timegrid_RowDataBound" OnRowCreated="timegrid_RowCreated">
<Columns>
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="place1" runat="server"></asp:PlaceHolder>\\5 fields of this type
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and generated row dynamically with textbox and placed it in this place holder as follows:
PlaceHolder pc1 = ((PlaceHolder)e.Row.Cells[4].Controls[1]);
TextBox tb = new TextBox();
tb.ID = "tb_4" ;
tb.Width = 50;
tb.Style.Add("text-align", "center");
pc1.Controls.Add(tb);
Its working fine as expected.
Now i have one button located outside gridview. On click of this button i want to access that controls which were generated dynamically as well as other gridview cells values also,i am not able to access it. I tried like
TextBox textBox = timegrid.Rows[0].Cells[4].FindControl("tab_4") as TextBox;
string temp = textBox.Text;\\Object reference error
Here i have only single row with multiple columns.I am getting error at highlighted line.Does this controls looses on postback(on button click)?If so,how to avoid that?
How to proceed? Any suggestions please..?
Below are the links for similar kind of problem, which are marked solved. Please go through these links. Hope it will help.
http://www.codeproject.com/Answers/599178/access-FindControl-plusdynamicallypluscreatedplusc#answer1
http://www.codeproject.com/Answers/596883/howpluscanplusiplusgetplusenteredplustextboxesplus#answer1
http://www.codeproject.com/Answers/594571/Ipluswantplustoplusretrieveplusandplussaveplusthep#answer2
这篇关于使用占位符访问动态生成的gridview单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!