Using the gridview control in ASP. NET 2.0 to operate data -- using the gridview to insert a new record

Source: Internet
Author: User

In the gridview control, you can insert a new record (see ASP. NET 2.0 in the gridview control), but if you want to implement in the gridview control, in the last row of the gridview control, provide a blank line to the user to enter the record, it is undoubtedly very convenient. The following describes the implementation method.
First, we plan to ask the user to select a record. When the user needs to add a record, click the Add button and a blank row is displayed in the last row of the gridview, enter the user by field, as shown in:
When you decide not to enter a new blank record, you can press the "cancel" button to return the blank row. To achieve this, we can make full use of the footer template function of the gridview to customize, because there are three columns, so in the footer template of each column, the definition is as follows:

<Asp: gridview id = "gridview1" runat = "server" performanceid = "sqlperformance1" datakeynames = "mermerid" autogeneratecolumns = "false" showfooter = "true">
<Columns>
<Asp: templatefield>
<Itemtemplate>
<Asp: Label id = "customeridlabel" runat = "server"> <% # eval ("customerid") %> </ASP: Label>
</Itemtemplate>
<Footertemplate>
<Asp: textbox id = "customeridtextbox" runat = "server"> </ASP: textbox>
</Footertemplate>
</ASP: templatefield>

 

<Asp: templatefield>
<Itemtemplate>
<Asp: Label id = "companynamelabel" runat = "server"> <% # eval ("companyName") %> </ASP: Label>
</Itemtemplate>
<Footertemplate>
<Asp: textbox id = "companynametextbox" runat = "server"> </ASP: textbox>
</Footertemplate>
</ASP: templatefield>

<Asp: templatefield>
<Footertemplate>
<Asp: dropdownlist id = "contacttitledropdownlist" runat = "server" performanceid = "sqlperformance2" datatextfield = "contacttitle" datavaluefield = "contacttitle">
</ASP: dropdownlist>
<Asp: sqldatasource id = "sqldatasource2" runat = "server" selectcommand = "select distinct [contacttitle] from [MERs]"
Connectionstring = "Server = localhost; uid = sa; Password = xxx; database = northwind">
</ASP: sqldatasource>

<Asp: button id = "button1" runat = "server" text = "add" onclick = "button#click"/>
<Asp: button id = "cancelbutton1" runat = "server" text = "cancel" onclick = "cancelbutton#click"/>
</Footertemplate>

<Itemtemplate>
<Asp: dropdownlist id = "contacttitledropdown" selectedvalue = '<% # BIND ("contacttitle ") %> 'runat = "server" performanceid = "sql1_c4" datatextfield =" contacttitle "datavaluefield =" contacttitle "> </ASP: dropdownlist>
<Asp: sqldatasource id = "sql1_c4" runat =" server "selectcommand =" select distinct [contacttitle] from [MERs]"
Connectionstring = "Server = localhost; uid = sa; Password = xxxx; database = northwind" enablecaching = "true">
</ASP: sqldatasource>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>

The above isCodeIn the first and second columns, the <foottemplate> Columns provide the customerid and companyName text boxes for user input. In the <footertemplate> column of the third column, display contracttitle in the form of dropdownlistbox .. Note the ADD and cancel buttons in the footertemplate in the third column. Their Event code is as follows:

the cancel button event is used to cancel displaying the footer template of the gridview. Therefore, the showfooter attribute is set to false, and the addbutton1 button is selected when the user decides to add a record, in this case, the showfooter attribute is set to true to display the foottemplate of each column to display a new blank row.
in the update code button#click event, gridview1.footerrow is used first. the findcontrol method extracts the values of the newly added fields, assigns values to the insertparameters set of sqldatasource (one-to-one correspondence must be noted), and finally uses the insert method of sqldatasource, you can add a new record to the database.
In addition, to display the data in the MERs table in the database northwind when loading the form, you need to set the attribute of sqldatsource1 by using the following code:

insertcommand = "insert into [MERs] ([customerid], [companyName], [contacttitle]) values (@ customerid, @ companyName, @ contacttitle) "
selectcommand =" select top 5 [customerid], [companyName], [contacttitle] from [MERs] "
connectionstring =" Server = localhost; uid = sa; Password = XXXXX; database = northwind ">





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.