asp.net - GridView, Add a new row dynamically grid and sql database by getting value from two textbox outside gridview -


i have gridview , above gridview have 2 textbox. when fill textbox , cleck add button, content should add gridview , sql database dynamically, , when click edit button presented in each row of gridview, should values above textboxes, add should change update button, after update gridview has populate again updated data.

this html page design

<body>    <form id="form1" runat="server">         <asp:scriptmanager id="script1" runat="server"></asp:scriptmanager>     <div>          <table align="center" style="width:50%;">             <tr>                 <td class="auto-style1">                     <asp:label id="label1" runat="server" text="country name" font-bold="true" forecolor="red"></asp:label>                 </td>                 <td>                     <asp:textbox id="text1" runat="server" width="180px"></asp:textbox>                 </td>                 <td>&nbsp;</td>             </tr>             <tr>                 <td class="auto-style1">                     <asp:label id="label2" runat="server" text="country notes" font-bold="true" forecolor="red"></asp:label>                 </td>                 <td>                     <asp:textbox id="text2" runat="server" width="180px"></asp:textbox>                 </td>                 <td>&nbsp;</td>             </tr>             <tr>                 <td>                     <br />                     <asp:button id="button1" runat="server" text="add" backcolor="#990000" forecolor="white" onclick="button1_click" />                 </td>                 <td>&nbsp;</td>                 <td>&nbsp;</td>             </tr>          </table>          <br />         <br />      <asp:updatepanel id="updatepanel1" runat="server">                <triggers>                      <asp:asyncpostbacktrigger controlid="gridview1" eventname="pageindexchanging" />                  </triggers>         <contenttemplate>         <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px" cellpadding="3" datakeynames="countryid" datasourceid="sqldatasource1" onrowediting="cmd = new sqlcommand(&quot;insert country1 (name,countrynotes) values(@name, @countrynotes)&quot;, con);"  >             <columns>                  <asp:commandfield showdeletebutton="true" showeditbutton="true" showselectbutton="true" />                  <asp:boundfield datafield="countryid" headertext="countryid" readonly="true" sortexpression="countryid" insertvisible="false" />                 <asp:boundfield datafield="name" headertext="name" sortexpression="name" />                 <asp:boundfield datafield="countrynotes" headertext="countrynotes" sortexpression="countrynotes" />             </columns>             <footerstyle backcolor="white" forecolor="#000066" />             <headerstyle backcolor="#006699" font-bold="true" forecolor="white" />             <pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" />             <rowstyle forecolor="#000066" />             <selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" />             <sortedascendingcellstyle backcolor="#f1f1f1" />             <sortedascendingheaderstyle backcolor="#007dbb" />             <sorteddescendingcellstyle backcolor="#cac9c9" />             <sorteddescendingheaderstyle backcolor="#00547e" />         </asp:gridview>             </contenttemplate>     </asp:updatepanel>         <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:atsconnectionstring %>" deletecommand="delete [country1] [countryid] = @original_countryid , (([name] = @original_name) or ([name] null , @original_name null)) , (([countrynotes] = @original_countrynotes) or ([countrynotes] null , @original_countrynotes null))" insertcommand="insert [country1] ([name], [countrynotes]) values (@name, @countrynotes)" selectcommand="select * [country1]" updatecommand="update [country1] set [name] = @name, [countrynotes] = @countrynotes [countryid] = @original_countryid , (([name] = @original_name) or ([name] null , @original_name null)) , (([countrynotes] = @original_countrynotes) or ([countrynotes] null , @original_countrynotes null))" conflictdetection="compareallvalues" oldvaluesparameterformatstring="original_{0}">             <deleteparameters>                 <asp:parameter name="original_countryid" type="int32" />                 <asp:parameter name="original_name" type="string" />                 <asp:parameter name="original_countrynotes" type="string" />             </deleteparameters>             <insertparameters>                 <asp:parameter name="name" type="string" />                 <asp:parameter name="countrynotes" type="string" />             </insertparameters>             <updateparameters>                 <asp:parameter name="name" type="string" />                 <asp:parameter name="countrynotes" type="string" />                 <asp:parameter name="original_countryid" type="int32" />                 <asp:parameter name="original_name" type="string" />                 <asp:parameter name="original_countrynotes" type="string" />             </updateparameters>         </asp:sqldatasource>      </div>     </form> </body> </html> 

you need handle in onrowediting method.

protected void onrowediting(object sender, gridviewediteventargs e) {    e.neweditindex -- index of gridview, store in session or variable .helps while updating    //assign value textbox.    text1.text=gridview1.rows[e.neweditindex].cells[0].text     text2.text=gridview1.rows[e.neweditindex].cells[1].text  } 

and in 'add' button event check editindex null

public void button1_click(object sender, system.eventargs e) {   if (editindex null)   /// insert   else   // update editindex } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -