Add record in Asp.net gridview

Source: Internet
Author: User

Most people suggest using formview to add records, but formview and gridview are not in the same table, so they cannot be displayed in the same table on the same page. If you intentionally assemble formview or a bunch of new widgets using common tables, you will encounter a very troublesome problem, that is, how to coordinate the column widths of the two tables. In most cases, when you create a table, the width of each column in the table is automatically adjusted, therefore, the specified width is not applicable in many cases.
Through practice, we have come up with a method. The main steps are as follows:
1) place a common HTML table in the emptydataview of the gridview to display the header when no data exists in the data source bound to the gridview (if the data source returns 0 rows, the gridview does not display the header by default. If the ID is tbheader, it serves to provide instructions for the following columns of the tbform control for the new function (Act as the header );
2) place a common HTML table under the gridview. The number of columns is consistent with the number of columns defined in the gridview, but the number of rows is only one row, then, add the control (such as textbox) for new functions in the columns of the table. Assume that the table ID is tbform.
3) Add a client script to the page to forcibly merge the two tables when the page is displayed on the client, in this way, only one row of tbform can be merged into the gridview, so there is an extra row at the bottom of the gridview, including the input control defined in the tbform table and the "add" Link (button), mainly Code (JavaScript) is as follows:
Function mergetable (source, DEST)
{
VaR row;
VaR cell;
VaR sourcetb = Document. All (source );
VaR desttb = Document. All (DEST );
For (VAR I = 0; I <sourcetb. Rows. length; I ++)
{
Row = Document. createelement ("TR ");
For (var j = 0; j <sourcetb. Rows (I). cells. length; j ++)
{
Cell = Document. createelement ("TD ");
Row. appendchild (cell );
// Copy the object
For (k = 0; k <sourcetb. Rows (I). cells (j). All. length; k ++)
Cell. appendchild (sourcetb. Rows (I). cells (j). All. Item (k ));
}
Desttb. tbodies (0). appendchild (ROW );
}
For (VAR I = sourcetb. Rows. Length-1; I> = 0; I --)
{
Sourcetb. deleterow (I)
}
}
Function changetablelayout ()
{
If (document. All ('tbheader') = NULL)
Mergetable ('tbform ','');
Else
Mergetable ('tbform', 'tbheader ');
}
Changetablelayout ();
If the gridview is bound with no data, only the tbheader in emptydataview is displayed instead of the columns defined in it. In this case, the tbheader and tbform must be merged. If the gridview contains data when it is bound, emptydataview is not displayed (of course, tbheader is not displayed), but each column defined in the gridview is displayed, therefore, you only need to merge the gridview and tbform. The client ID of the gridview can be obtained using gridview. clientid.
On the server side, it is easy to know whether the data is included after the gridview is bound, but for the client side, it is not easy to check. A simple method is to check whether there is a tbheader object in the page (If yes, this indicates that the table has no data. If this object is not available, it indicates that the gridview contains data... so long)
4) In the Ajax environment, the preceding script may not be executed. You can call sys. application. load. add (javascriptfunction) to forcibly execute scripts to merge tables. The main code is as follows (C #):
Scriptmanager myscriptmanager = scriptmanager. getcurrent (PAGE );
If (myscriptmanager. isinpartialrenderingmode)
{
Page. clientscript. registerstartupscript (this. GetType (), "showfulltable1", "changetablelayout (); \ n", true );
}
Else
{
Page. clientscript. registerstartupscript (this. GetType (), "showfulltable2", "SYS. application. Load. Add (changetablelayout); \ n", true );
}
Note: changetablelayout in the above Code is the name of the client script function, in which the code in step 3rd is called. The above code is passed in Atlas and is finally tested in ASP. NET Ajax beta.
In addition, to better illustrate the effects of the above code, I caught one. The first three rows including the header in the figure are the gridview. The bottom row actually comes from another table. After the client is forcibly merged, the display is like this, and it looks like a table.
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.