asp.net - How to fill the text box with value of a column of a GridView row -
in grid have following columns
<columns> <asp:templatefield headertext="select pay"> <itemtemplate> <asp:checkbox id="chkselect" runat="server" oncheckedchanged="chkselect_oncheckedchanged" autopostback="true" /> </itemtemplate> </asp:templatefield> <asp:boundfield datafield="reference" headertext="invoice" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center"> </asp:boundfield> <asp:boundfield datafield="chargeddate" headertext="date of charge" headerstyle-horizontalalign="center" itemstyle-horizontalalign="center" dataformatstring="{0:mm-dd-yyyy}"> </asp:boundfield> <asp:boundfield datafield="amount" headertext="amount" headerstyle- horizontalalign="center" itemstyle-horizontalalign="center"> </asp:boundfield> <asp:templatefield headertext="amount applied"> <itemtemplate> <asp:textbox id="txtpayamount" width="80px" runat="server" autopostback="true" /> </itemtemplate> <itemstyle horizontalalign="center" /> </asp:templatefield> </columns>
please tell, in grid if chose check box, amount value should displayed in textbox of same row. if checked more 1 check boxes values should displayed in text boxes of related check boxes/ rows. sum should displayed in textbox below grid. should need write under "onselect_checkedchanged" event?
please try below code snippet.
aspx
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datakeynames="totalamount" showfooter="true"> <columns> <asp:templatefield headertext="select pay"> <itemtemplate> <asp:checkbox id="chkselect" runat="server" oncheckedchanged="chkselect_oncheckedchanged" autopostback="true" /> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="amount"> <itemtemplate> <%# eval("totalamount", "{0:#,##0.00}")%> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="amount applied"> <itemtemplate> <asp:textbox id="txtpayamount" width="80px" runat="server" /> </itemtemplate> <footertemplate> <asp:textbox id="textbox1" width="80px" runat="server" /> </footertemplate> <itemstyle horizontalalign="center" /> </asp:templatefield> </columns> </asp:gridview>
aspx.cs
public partial class forum : system.web.ui.page { protected void page_init(object sender, system.eventargs e) { gridview gv = new gridview(); gv.id = "gridview1"; gv.autogeneratecolumns = false; gv.showfooter = true; gv.datakeynames = new string[] { "totalamount" }; templatefield tf = new templatefield(); tf.itemtemplate = new mycustomtemplate(); gv.columns.add(tf); boundfield bf = new boundfield(); bf.datafield = "totalamount"; gv.columns.add(bf); tf = new templatefield(); tf.itemtemplate = new mycustomtexttemplate(); tf.footertemplate = new mycustomfootertexttemplate(); gv.columns.add(tf); this.form1.controls.add(gv); } protected void page_load(object sender, system.eventargs e) { gridview gv = form1.findcontrol("gridview1") gridview; if (!ispostback) { dynamic data = new[] { new { id = 1, name = "name1",totalamount= 10}, new { id = 2, name = "name2",totalamount= 20}, new { id = 3, name = "name3",totalamount= 30}, new { id = 4, name = "name4",totalamount= 40}, new { id = 5, name = "name5",totalamount= 50} }; gv.datasource = data; gv.databind(); } } protected void page_prerender(object sender, system.eventargs e) { } protected void radgrid1_needdatasource(object sender, gridneeddatasourceeventargs e) { dynamic data = new[] { new { id = 1, name ="name_1"}, new { id = 2, name = "name_2"}, new { id = 3, name = "name_3"}, new { id = 4, name = "name_4"}, new { id = 5, name = "name_5"} }; } } public class mycustomtemplate : itemplate { public void instantiatein(system.web.ui.control container) { checkbox cb = new checkbox(); cb.id = "chkselect"; cb.autopostback = true; cb.checkedchanged += new eventhandler(cb_checkedchanged); container.controls.add(cb); } protected void cb_checkedchanged(object sender, eventargs e) { gridviewrow row = (sender checkbox).namingcontainer gridviewrow; textbox txtpayamount = row.findcontrol("txtpayamount") textbox; if ((sender checkbox).checked) { txtpayamount.text = convert.tostring((row.parent.parent gridview).datakeys[row.rowindex]["totalamount"]); } else { txtpayamount.text = string.empty; } int totalamount = 0; foreach (gridviewrow rowv in (row.parent.parent gridview).rows) { checkbox chk = rowv.findcontrol("chkselect") checkbox; if (chk.checked) { totalamount += convert.toint32((row.parent.parent gridview).datakeys[rowv.rowindex]["totalamount"]); } } gridviewrow rowf = (row.parent.parent gridview).footerrow; ((textbox)rowf.findcontrol("textbox1")).text = totalamount.tostring(); } } public class mycustomtexttemplate : itemplate { public void instantiatein(system.web.ui.control container) { textbox cb = new textbox(); cb.id = "txtpayamount"; container.controls.add(cb); } } public class mycustomfootertexttemplate : itemplate { public void instantiatein(system.web.ui.control container) { textbox cb = new textbox(); cb.id = "textbox1"; container.controls.add(cb); } }
Comments
Post a Comment