How to get value from TemplateField in GridView using asp.net C#? -
i trying retrieve info entered gridview textbox, getting no info that. here code:
in asp
<asp:gridview id="add" runat="server"> <columns> <asp:templatefield headertext="select" headerstyle-width="100px"> <itemtemplate> <asp:checkbox id="cbox_select" runat="server" /> </itemtemplate> </asp:templatefield> <asp:boundfield datafield = "name" headertext = "fee type" /> <asp:templatefield headertext="amount" headerstyle-width="100px"> <itemtemplate> <asp:textbox id="textbox" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> and in c#,
for (int = 0; i< n;i++) { string = ((textbox)add.rows[i].cells[2].findcontrol("textbox")).text; } can help me this?
you want loop through gridviewrowcollection, , find control.
foreach (gridviewrow row in add.rows) { var textbox = row.findcontrol("textbox") textbox; var = textbox.text; } demo <asp:button id="button1" runat="server" text="submitbutton" onclick="submitbutton_click" /> <asp:gridview id="add" runat="server"> <columns> <asp:templatefield headertext="amount" headerstyle-width="100px"> <itemtemplate> <asp:textbox id="textbox" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> public class sampleclass { public int id { get; set; } public string text { get; set; } } protected void page_load(object sender, eventargs e) { if (!ispostback) { add.datasource = new list<sampleclass>() { new sampleclass {id = 1, text = "one"}, new sampleclass {id = 2, text = "two"}, }; add.databind(); } } protected void submitbutton_click(object sender, eventargs e) { foreach (gridviewrow row in add.rows) { var textbox = row.findcontrol("textbox") textbox; var = textbox.text; } } c# asp.net gridview
No comments:
Post a Comment