c# - Issue in Update FormView asp.net. -
i have sent value through querystring page. after unable edit mode on modechanging event. when click edit button, postbacks , nothing happens. if clicked edit second time gives error :
(failed load viewstate. control tree viewstate being loaded must match control tree used save viewstate during previous request. example, when adding controls dynamically, controls added during post-back must match type , position of controls added during initial request.)
please tell me wrong.
source code -
<asp:formview id="formview1" runat="server" allowpaging="true" caption="firebrigade" datakeynames="firebrigadeid" onmodechanging="formview1_modechanging" onpageindexchanging="formview1_pageindexchanging"> <itemtemplate> firebrigade id :<asp:label id="lblfid" runat="server" text='<%# eval("firebrigadeid") %>'></asp:label><br /> name :<asp:label id="label3" runat="server" text='<%# eval("fbname") %>'></asp:label><br /> latlong:<asp:label id="label1" runat="server" text='<%# eval("latlng") %>'></asp:label><br /> address: <asp:label id="label2" runat="server" text='<%# eval("address") %>'></asp:label><br /> contact: <asp:label id="label4" runat="server" text='<%# eval("contactnumber") %>'></asp:label><br /> <asp:linkbutton id="editbutton" text="edit" commandname="edit" runat="server"/> </itemtemplate> <edititemtemplate> firebrigade id :<asp:textbox id="txtfid" runat="server" text='<%# bind("firebrigadeid") %>'></asp:textbox><br /> name :<asp:textbox id="txtname" runat="server" text='<%# bind("fbname") %>'></asp:textbox> latlong:<asp:textbox id="txtlatlong" runat="server" text='<%# bind("latlng") %>'></asp:textbox><br /> address: <asp:dropdownlist id="ddladdress" runat="server" ondatabound="ddladdress_databound" appenddatabounditems="true"> <asp:listitem text="select" value="0"></asp:listitem> </asp:dropdownlist> <br /> contact: <asp:textbox id="txtcontact" runat="server" text='<%# bind("contactnumber") %>'></asp:textbox><br /> <asp:linkbutton id="updatebutton" text="update" commandname="update" runat="server" /> <asp:linkbutton id="cancelupdatebutton" text="cancel" commandname="cancel" runat="server" /> </edititemtemplate> </asp:formview>
c# code -
public partial class fifthpage : system.web.ui.page { protected void page_load(object sender, eventargs e) { if (!ispostback) { lblshow.text = request.querystring["q"].tostring(); dataset ds = new dataset(); ds = bind(); da.fill(ds); formview1.datasource = ds; formview1.databind(); } } public dataset bind() { sqlconnection con = new sqlconnection(); con.connectionstring = "data source=system-pc;initial catalog=db;integrated security=true"; sqlcommand cmd = new sqlcommand("select * firebrigade",con); sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds); return ds; } protected void ddladdress_databound(object sender, eventargs e) { dataset ds = new dataset(); ds = bind(); list<string> ls = new list<string>(); foreach (listitem lst in ds.tables[0].rows) { //lst.value = ds.tables[0].rows[0]["address"].tostring(); ls.add(ds.tables[0].rows[0]["address"].tostring()); } dropdownlist ddladd = (dropdownlist)formview1.findcontrol("ddladdress"); ddladd.datasource = ls; } protected void formview1_modechanging(object sender, formviewmodeeventargs e) { formview1.changemode(e.newmode); bind(); } protected void formview1_pageindexchanging(object sender, formviewpageeventargs e) { formview1.pageindex = e.newpageindex; bind(); } }
you should reassign datasource of formview after editing, change code follow
protected void formview1_modechanging(object sender, formviewmodeeventargs e) { formview1.changemode(e.newmode); formview1.datasource = bind(); formview1.databind(); }
Comments
Post a Comment