c# - How do I get the textbox value to the database within a repeater? -
i have repeater populate database:
using (sqlconnection conn = new sqlconnection(connstring)) { sqlcommand cmd = new sqlcommand(@"select communityname, cid, budget donation year = year(getdate()) order communityname", conn); conn.open(); sqldataadapter adp = new sqldataadapter(cmd); dataset mydataset = new dataset(); adp.fill(mydataset); myrep.itemdatabound += new repeateritemeventhandler(myrep_itemdatabound); myrep.datasource = mydataset; myrep.databind(); } void myrep_itemdatabound(object sender, repeateritemeventargs e) { var textbox = e.item.findcontrol("community"); textbox.clientidmode = clientidmode.static; textbox.id = "community" + (e.item.itemindex + 1); } repeater:
<asp:updatepanel id="updatepanel" runat="server" updatemode="always"> <contenttemplate> <asp:repeater id="myrep" runat="server"> <itemtemplate> <div class="form-group"> <asp:label id='thislbl' runat="server" text='<%# eval("communityname") %>' /> <asp:textbox runat="server" id="community" text='<%# eval("budget") %>' cssclass="form-control" /> </div> </itemtemplate> </asp:repeater> </contenttemplate> </asp:updatepanel> this creates 6 textboxes labels , values, question how detect of these boxes belongs record pulled in database? want able modify value in these boxes , hit button save them database can't seem wrap head around getting them proper records.
should set id of textbox can parse through , match proper record? in itemdatabound?
you have put hidden field inside repeater item template takes value budget, , hidden field keep cid value has read in post request. of course need button , click event handler.
<asp:repeater id="myrep" runat="server"> <itemtemplate> <div class="form-group"> <asp:label id='thislbl' runat="server" text='<%# eval("communityname") %>' /> <asp:textbox runat="server" id="txtbudget" text='<%# eval("budget") %>' cssclass="form-control" /> <asp:hiddenfield runat="server" id="hdoriginalbudget" value='<%# eval("budget") %>' /> <asp:hiddenfield runat="server" id="hdcid" value='<%# eval("cid") %>' /> </div> </itemtemplate> </asp:repeater> <br /> <asp:button id="btn" runat="server" onclick="btn_click" /> in code behind need loop inside repeater check whether text box has been changed comparing value hidden field. after save budget value in database need realign hidden field value the new value entered user, otherwise save value after each post back:
protected void btn_click(object sender, eventargs e) { foreach (repeateritem item in myrep.items) { var txtbudget = item.findcontrol("txtbudget") textbox; var hdoriginalbudget = item.findcontrol("hdoriginalbudget") hiddenfield; var hdcid = item.findcontrol("hdcid") hiddenfield; if (txtbudget.text != hdoriginalbudget.value) { //if enter here means user changed value of text box using (sqlconnection conn = new sqlconnection(connstring)) { sqlcommand cmd = new sqlcommand(@"update donation set budget = @budget cid = @cid", conn); cmd.parameters.add(new sqlparameter("@budget", int.parse(txtbudget.text))); cmd.parameters.add(new sqlparameter("@cid", int.parse(hdcid.value))); conn.open(); cmd.executenonquery(); } //after write in database realign values hdoriginalbudget.value = txtbudget.text; } } } take care code missing basic validation, if user writes invalid value in textbox (for example "yyy") breaks. please don't put in production is!
Comments
Post a Comment