Examples of data binding using the Ajax local Refresh GridView Example _ Practical Tips

Source: Internet
Author: User

The specific code is as follows:

Copy Code code as follows:

<% @ Page Language = "C #"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

< script runat = "server" >  
  System.Data.DataView Createdatasourcebyxianhuimeng ()
  {
    System.Data.DataTable dt = new System.Data.DataTable ();
    System.Data.DataRow Dr;
    dt. Columns.Add (New System.Data.DataColumn ("id", typeof (System.Int32));
    dt. Columns.Add (New System.Data.DataColumn ("Student Name", typeof (System.String));
    dt. Columns.Add (New System.Data.DataColumn ("language", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("Mathematics", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("English", typeof (System.Decimal));
    dt. Columns.Add (New System.Data.DataColumn ("Computer", typeof (System.Decimal));

     for (int i = 1; i < i + +)
    {
      System.Random rd = new System.Random (Environment.tickcount * i);;
      dr = dt. NewRow ();
      dr[0] = i;
      dr[1] = "Mencius E chapter" "+ i.ToString ();
      dr[2] = System.Math.Round (rd. Nextdouble () * 100, 2);
      dr[3] = System.Math.Round (rd. Nextdouble () * 100, 2);
      dr[4] = System.Math.Round (rd. Nextdouble () * 100, 2);
      dr[5] = System.Math.Round (rd. Nextdouble () * 100, 2);
      dt. Rows.Add (DR);
   }
    System.Data.DataView dv = new System.Data.DataView (DT);
     return DV;
 }

protected void Page_Load (object sender, EventArgs e)
{
if (request.querystring["id"]!= null)
{
Response.clearcontent ();
Gridview1.datasource = Createdatasourcebyxianhuimeng ();
Gridview1.databind ();
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
System.IO.StringWriter SW = new System.IO.StringWriter (SB);
HtmlTextWriter HTW = new HtmlTextWriter (SW);
Literal Header = new Literal ();
Header. Text = "HEADER.CONTROLS.ADD (header);
Header.rendercontrol (HTW);
Gridview1.rendercontrol (HTW);
Response.Write ("Query the data here, output the result is OK.") Result: "+ sb." ToString ());
Response.End ();
}
}

/Add this override void Verifyrenderinginserverform to avoid the presence of
/Type "GridView" control "GridView1" must be placed inside a form tag with Runat=server.
/The exception
public override void Verifyrenderinginserverform (Control control)
{ }
</script >

< html xmlns = "http://www.w3.org/1999/xhtml" >
< head ID = "Head1" runat = "server" >
< title > Using AJAX, local refresh GridView for data binding simple implementation </title >

< script type = "Text/javascript" >
function GetData (p) {
document.getElementById ("D"). InnerHTML = "reading data ... " ;
h = window. XMLHttpRequest? New window. XMLHttpRequest (): New ActiveXObject ("MSXML2. XMLHTTP ");
H.open ("Get", ' <%=request.filepath%>?id= ' + P.value, true);
H.onreadystatechange = function () {
if (h.readystate = = 4) {
if (h.status >= && h.status < 300) {
document.getElementById ("D"). InnerHTML = H.responsetext;
}
else {
document.getElementById ("D"). InnerHTML = "}
}
}
H.send (NULL);
}
Alert ("This prompt, only appears on the first open page.") " );
</script >

< BODY >
< form ID = "Form1" runat = "server" >
< Asp:gridview ID = "GridView1" runat = "server" ></Asp:gridview >
< Asp:panel ID = "Header" runat = "server" ></asp:panel >
< select onchange = "GetData (this)" >
< option value = "1" > Project </option >
< option value = "2" > Item two </option >
</Select >
</form >
< div id = "D" ></div >
</Body >
</HTML >

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.