Implementation of partial refresh in Asp. Net

Source: Internet
Author: User

The implementation of partial refresh in Asp. Net is described 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 createperformancebyxianhuimeng ()
{
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 ("", 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 <30; I)
{
System. Random rd = new System. Random (Environment. TickCount * I );;
Dr = dt. NewRow ();
Dr [0] = I;
Dr [1] = "Chapter E of Mencius" 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 = createperformancebyxianhuimeng ();
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 and output the result. Result: "sb. ToString ());
Response. End ();
}
}

/// Add this override void VerifyRenderingInServerForm (Control control) to avoid
/// The control "GridView1" of the type "GridView" must be placed in the form tag with runat = server.
/// Exception
Public override void VerifyRenderingInServerForm (Control control Control)
{}
</Script>

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> Using AJAX to partially refresh the GridView for Data Binding </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> = 200 & h. status <300 ){
Document. getElementById ("d"). innerHTML = h. responseText;
}
Else {
Document. getElementById ("d"). innerHTML = "}
}
}
H. send (null );
}
Alert ("this prompt appears only when the page is opened for the first time. ");
</Script>

</Head>
<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 1 </option>
<Option value = "2"> Project 2 </option>
</Select>
</Form>
<Div id = "d"> </div>
</Body>
</Html>

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.