SoFunction
Updated on 2025-03-07

Directly operate the Gridview control to insert new records in 2.0/3.5


       </asp:TemplateField>
   </Columns>
   <PagerStyle. BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
   <SelectedRowStyle. BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
   <HeaderStyle. BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
   <EditRowStyle. BackColor="#999999" />
   <AlternatingRowStyle. BackColor="White" ForeColor="#284775" />
</asp:GridView>
First of all, a button "outAdd" was added before the Gridview control to whether the control displays the <foottemplate> template content of the Gridview control. Obviously, this template is hidden at the beginning of the page startup. Therefore, the Click event code for this button (located in the file) is quite simple, like this:
protected void outAdd_Click(object sender, EventArgs e)
{
   = true;

}
Next is the relevant tag code for Gridview. As you can see, the DepartNameBox text box is provided in the <foottemplate> of the DepartName column for user input (because this field in this example is only a simple text field, for other types of fields, you need to create a corresponding control for operation).
Also, please note that in the <FooterTemplate> in the first column we have added two buttons Add and Hide buttons, and their event codes are as follows:
<script. runat="server">
void btnCancel_Click(object sender, EventArgs e)
{
   = false;
}

void btnAdd_Click(object sender, EventArgs e)
{
   TextBox t1 = ("DepartNameBox") as TextBox;

   ["DepartName"].DefaultValue = ;

   ();

}
The event of the Hide button is used to cancel the footer template of the Gridview, so the showfooter property is set to false, and by clicking the Add button, you can insert (add) the added records into the database table.
The Add button is selected when the user decides to add a new record. At this time, the showfooter attribute will be set to true to display the foottemplate of each column, so as to achieve the purpose of displaying a new blank row.
It is particularly noteworthy that, here you must place the Click event handler function of the buttons "Add" and "Hide" in inline in the ASPX file, and cannot be placed in the code file; otherwise, a compilation error as shown in Figure 3 will occur.

Figure 3 Compilation error prompts caused by placing the Click event handler function of the button "Add" and "Hide" in the code file
In addition, DataKeyNames="DepartID" must be specified in the property declaration of GridView1; otherwise, if you click the "Delete" or "Update" link at runtime, the "Classic" error message will appear, as shown in Figure 4.

Figure 4 Error prompts caused by DataKeyNames of GridView not specified
4. Observe the operation results
Press F5 to run the above example page, and if everything goes well, you will observe the initial snapshot as shown in Figure 5.

Figure 5 Initial snapshot of the example page
Then, click the "Add New Record" button and a snapshot as shown in Figure 6 will appear.


Figure 6 Snapshot of the running example of adding the "insert" function for the GRIDVIEW control

Obviously, clicking "Add" after entering the department name can add the new department to the database table, while clicking the "Hide" button can hide the display of this footer.
5. Summary
The Gridview control introduced in 2.0 is quite powerful, but there are certain shortcomings. This article shows only one of them, and it is also shown only one way to prompt the insertion function for the Gridview control.
Finally, note that although the debugging of this example is passed in VS2008, according to my experience, there should be no problems in the 2.0 environment of VS2005.