asp.net - Gridview Footer Button Disbale it show Error as Object reference not set to an instance of an object -
hai every 1 new asp.net. question have gridview textboxs in itemtempleates , single button in footer.when click button in gridview rows added manually, , ny coding
<asp:gridview id="gridview2" runat="server" showfooter="true" autogeneratecolumns="false" onselectedindexchanged="gridview2_selectedindexchanged"> <columns> <asp:boundfield headertext="s.no" datafield="rownumber" /> <asp:templatefield headertext="description"> <itemtemplate> <asp:textbox id="textbox132" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="smv"> <itemtemplate> <asp:textbox id="textbox133" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="o/l"> <itemtemplate> <asp:textbox id="textbox134" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="f/l"> <itemtemplate> <asp:textbox id="textbox135" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="s/n"> <itemtemplate> <asp:textbox id="textbox136" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="helper"> <itemtemplate> <asp:textbox id="textbox137" runat="server"></asp:textbox> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="man power"> <itemtemplate> <asp:textbox id="textbox138" runat="server"></asp:textbox> </itemtemplate> <footerstyle horizontalalign="right" /> <footertemplate> <asp:button id="button1" runat="server" text="add new" onclick="button1_click1" /> </footertemplate> </asp:templatefield> </columns> </asp:gridview>
this front end coding....
and aspx.cs coding is
public void bindgrid() { datatable dt = new datatable(); datarow dr = null; dt.columns.add(new datacolumn("rownumber", typeof(string))); dt.columns.add(new datacolumn("column1", typeof(string))); dt.columns.add(new datacolumn("column2", typeof(string))); dt.columns.add(new datacolumn("column3", typeof(string))); dt.columns.add(new datacolumn("column4", typeof(string))); dt.columns.add(new datacolumn("column5", typeof(string))); dt.columns.add(new datacolumn("column6", typeof(string))); dt.columns.add(new datacolumn("column7", typeof(string))); dr = dt.newrow(); dr["rownumber"] = 1; dr["column1"] = string.empty; dr["column2"] = string.empty; dr["column3"] = string.empty; dr["column4"] = string.empty; dr["column5"] = string.empty; dr["column6"] = string.empty; dr["column7"] = string.empty; dt.rows.add(dr); //dr = dt.newrow(); //store datatable in viewstate viewstate["currenttable"] = dt; gridview2.datasource = dt; gridview2.databind(); } private void addnewrowtogrid() { int rowindex = 0; int val = convert.toint16(textbox63.text); if (viewstate["currenttable"] != null) { datatable dtcurrenttable = (datatable)viewstate["currenttable"]; datarow drcurrentrow = null; if (dtcurrenttable.rows.count > 0) { (int = 1; <= dtcurrenttable.rows.count; i++) { //extract textbox values textbox box1 = (textbox)gridview2.rows[rowindex].cells[1].findcontrol("textbox132"); textbox box2 = (textbox)gridview2.rows[rowindex].cells[2].findcontrol("textbox133"); textbox box3 = (textbox)gridview2.rows[rowindex].cells[3].findcontrol("textbox134"); textbox box4 = (textbox)gridview2.rows[rowindex].cells[4].findcontrol("textbox135"); textbox box5 = (textbox)gridview2.rows[rowindex].cells[5].findcontrol("textbox136"); textbox box6 = (textbox)gridview2.rows[rowindex].cells[6].findcontrol("textbox137"); textbox box7 = (textbox)gridview2.rows[rowindex].cells[7].findcontrol("textbox138"); button btn = this.gridview2.footerrow.findcontrol("btnfooter") button; drcurrentrow = dtcurrenttable.newrow(); drcurrentrow["rownumber"] = + 1; dtcurrenttable.rows[i - 1]["column1"] = box1.text; dtcurrenttable.rows[i - 1]["column2"] = box2.text; dtcurrenttable.rows[i - 1]["column3"] = box3.text; dtcurrenttable.rows[i - 1]["column4"] = box4.text; dtcurrenttable.rows[i - 1]["column5"] = box5.text; dtcurrenttable.rows[i - 1]["column6"] = box6.text; dtcurrenttable.rows[i - 1]["column7"] = box7.text; rowindex++; **if (rowindex+1 > val) { btn.visible = false; }** } { dtcurrenttable.rows.add(drcurrentrow); } viewstate["currenttable"] = dtcurrenttable; gridview2.datasource = dtcurrenttable; gridview2.databind(); } } else { response.write("viewstate null"); } //set previous data on postbacks setpreviousdata(); } private void setpreviousdata() { int rowindex = 0; int val = convert.toint16(textbox63.text); if (viewstate["currenttable"] != null) { datatable dt = (datatable)viewstate["currenttable"]; if (dt.rows.count > 0) { (int = 0; < dt.rows.count; i++) { textbox box1 = (textbox)gridview2.rows[rowindex].cells[1].findcontrol("textbox132"); textbox box2 = (textbox)gridview2.rows[rowindex].cells[2].findcontrol("textbox133"); textbox box3 = (textbox)gridview2.rows[rowindex].cells[3].findcontrol("textbox134"); textbox box4 = (textbox)gridview2.rows[rowindex].cells[4].findcontrol("textbox135"); textbox box5 = (textbox)gridview2.rows[rowindex].cells[5].findcontrol("textbox136"); textbox box6 = (textbox)gridview2.rows[rowindex].cells[6].findcontrol("textbox137"); textbox box7 = (textbox)gridview2.rows[rowindex].cells[7].findcontrol("textbox138"); button btn = this.gridview2.footerrow.findcontrol("btnfooter") button; box1.text = dt.rows[i]["column1"].tostring(); box2.text = dt.rows[i]["column2"].tostring(); box3.text = dt.rows[i]["column3"].tostring(); box4.text = dt.rows[i]["column4"].tostring(); box5.text = dt.rows[i]["column5"].tostring(); box6.text = dt.rows[i]["column6"].tostring(); box7.text = dt.rows[i]["column7"].tostring(); rowindex++; **if (rowindex+1> val) { btn.visible = false; }** } } } } protected void button1_click1(object sender, eventargs e) { addnewrowtogrid(); }
here when click button1 add new row in gridview. have text box in aspx page. when textbox show (int) value, textbox value , gridview rows equal means button disable.
please help.
i use code ....
gridview2.showfooter = false; gridview2.footerrow.visible = false;
Comments
Post a Comment