asp.net gridview data binding and XML binding

Source: Internet
Author: User

ASP tutorials. NET GridView data binding and XML binding

When the GridView edit state gets the new value, it often gets the value before the modification.

My analysis:

Many times I put the data-binding function call directly in the Page_Load () function, when the GridView edit state gets the new value, it often gets the value before the modification, because the page load executes the Page_Load () function, the data-binding function is executed again, The value in the GridView then becomes the value before the change, and the value is the value before it is changed.

Solution:

Place the data-binding function in the IF (!this.page.ispostback) of the Page_Load () function so that the value obtained is the changed value.

To get the GridView cell Value method:

Get the first cell contents in the RowUpdating event

((textbox) (Gridview.rows[e.rowindex].cells[1].controls[0])). Text

Data binding to

<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "Basicgridview"%>

<!doctype HTML PUBLIC "-//w3c//dtd xhtml 1.1//en" "HTTP://WWW.W3.ORG/TR/XHTML11/DTD/XHTML11.DTD" >

<title>untitled page</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server" >
</asp:gridview>

</div>
</form>
</body>

File:default.aspx.cs


using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.web.ui.webcontrols.webparts;
Using System.Web.UI.HtmlControls;
Using System.web.configuration;
Using System.Data.SqlClient;

public partial class Basicgridview:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (!this.ispostback)
{
string connectionstring = webconfigurationmanager.connectionstrings["Northwind"].connectionstring;
String selectsql = "Select ProductID, ProductName, UnitPrice from Products";
SqlConnection con = new SqlConnection (connectionstring);
SqlCommand cmd = new SqlCommand (Selectsql, con);
SqlDataAdapter adapter = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();

Adapter.fill (ds, "products");

Gridview1.datasource = ds;
Gridview1.databind ();
}
}
}


File:web.config

<?xml version= "1.0"?>
<configuration>
<connectionstrings>
<add name= "Northwind" connectionstring= "Data source=localhostsqlexpress;initial catalog=northwind;integrated Security=sspi "/>
</connectionstrings>
</configuration>

XML data binding to the ASP GridView

<%@ page language= "C #"%>
<%@ import namespace= "System.Configuration"%>
<%@ Import namespace= "System.Data"%>

<script runat= "Server" >
void Page_Load (object sender, EventArgs e)
{
DataSet Authorsdataset;
string filepath = Server.MapPath ("Authors.xml");
Authorsdataset = new DataSet ();
Read the contents of the XML file into the dataset
Authorsdataset.readxml (filepath);
Authorsgird.datasource = Authorsdataset.tables[0].defaultview;
Authorsgird.databind ();
}

</script>

<title>reading XML data into a DataSet object </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "Authorsgird" runat= "Server"
Autogeneratecolumns= "false" cellpadding= "4" headerstyle-backcolor= "Blue" headerstyle-forecolor= "white"
headerstyle-horizontalalign= "Center" headerstyle-font-bold= "true" >
<columns>
<asp:boundfield headertext= "Last Name" datafield= "LastName"/>
<asp:boundfield headertext= "Name"
Datafield= "FirstName" itemstyle-horizontalalign= "right"/>
</columns>
</asp:gridview>
</div>
</form>
</body>

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.