Methods for adding records to the ASP.net GridView-practical tips

Source: Internet
Author: User
Most people recommend using FormView to do the function of adding records, but FormView and GridView are not the same table, so they cannot be displayed in the same table on the same page. If you deliberately assemble FormView or your own stack of new controls using a common form, then there is a very troublesome question, that is, how the column widths of the two tables are coordinated, and in most cases the widths of the columns in the table are automatically adjusted when you make the table. Therefore, the forced designation of width is not applicable in many cases.
Through practice, come up with a way, the main steps are as follows:
1 in the Emptydataview of the GridView, place a normal HTML table so that the header is still displayed when there is no data in the GridView-bound data source (if the data source is 0,gridview for the number of rows returned by default and the header is not displayed), suppose The ID is Tbheader, which serves as a description (acting as a header) for the columns of the tbform that are used for the new features below;
2 under the GridView, place an ordinary HTML table with the number of columns consistent with the number of columns defined in the GridView, but only one row, and then put in each column of the table the controls for the new features (such as a TextBox, etc.), assuming that the table ID is Tbform
3 Adding a client-side script to the page so that the page is displayed to the client, using Javascript to force the two tables together so that only one row of Tbform can be merged into the GridView, so there is a row at the bottom of the GridView, with Tbform the input control and the Add Link (button) defined in the table, the main 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);
Copying objects
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 binding does not have data, the columns defined therein are not displayed and only the Tbheader in the Emptydataview is displayed, and Tbheader and Tbform are merged. If the GridView binding contains data, the Emptydataview will not be displayed (and the tbheader in it will not be shown), but the columns defined in the GridView are displayed, so you can simply merge the GridView itself and the Tbform. The client ID of the GridView can be obtained using Gridview.clientid.
It is easy to know on the server side whether the GridView binding contains data, but for the client is not easy to check, a simple way is to check the page has no Tbheader objects (if there is, the table has no data, if there is no this object, the GridView contains data ...). So verbose)
4 if it is an AJAX environment, the above script may not be executed, you can invoke Sys.Application.load.add (javascriptfunction) to enforce the script, to merge the table, 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: The changetablelayout in the above code is the name of the client script function that calls the code in step 3rd, which is passed in Atlas and is tested on ASP.net AJAX Beta.
Another: In order to better illustrate the effect of the above code, I grabbed a screenshot, including the table header in the first three lines is the GridView, the bottom line is actually from another table, after the client forced the merge, the display effect is like this, looks like a table.

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.