Asp. Net script reconciliation and Server. Transfer page value Transfer

Source: Internet
Author: User

In Asp. Net, scripts are often used to reconcile and pass values between pages. The following is a simple sample code for ScriptCallBack and Server. Transfer.

WebForm1.aspx
Add the _ doPostBack script to the Head. If the page contains buttons such as HyperLink, the script and two hidden controls "_ EVENTTARGET" and "_ EVENTARGUMENT" are automatically generated by the FrameWork, manually add

<SCRIPT language = "javascript">
<! --
Function _ doPostBack (eventTarget, eventArgument ){
Var theform;
If (window. navigator. appName. toLowerCase (). indexOf ("netscape")>-1 ){
Theform = document. forms ["Form1"]; // note the FormID
} Else {
Theform = document. Form1; // also available here
}
Theform. _ EVENTTARGET. value = eventTarget. split ("$"). join (":");
Theform. _ EVENTARGUMENT. value = eventArgument;
Theform. submit ();
}
// -->
</SCRIPT>

<Form id = "Form1" method = "post" runat = "server">
<INPUT type = "hidden" name = "_ EVENTTARGET">
<INPUT type = "hidden" name = "_ EVENTARGUMENT">
<A href = "javascript :__ doPostBack (ScriptCallBack, ScriptCallBack)"> ScriptCallBack </A>
<ASP: TEXTBOX id = "TextBox1" style = "Z-INDEX: 101; LEFT: pixel PX; POSITION: absolute; TOP: 152px" runat = "server"> sometext </ASP: TEXTBOX>

C #
WebForm1.aspx. cs
Private void Page_Load (object sender, System. EventArgs e)
{
If (IsPostBack)
If (Request. Form ["_ EVENTARGUMENT"] = "ScriptCallBack ")
Server. Transfer ("WebForm2.aspx", true); // The second parameter indicates whether the Form and QuerryString values of the page are retained.
}

WebForm2.aspx. cs
Private void Page_Load (object sender, System. EventArgs e)
{
If (this. Context. Handler! = Sender)
Response. Write (Request. Form ["TextBox1"]);
}

VB. NET
WebForm1.aspx. vb
Private Sub Page_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
If IsPostBack Then
If Request. Form ("_ EVENTARGUMENT") = "ScriptCallBack" Then
Server. Transfer ("WebForm2.aspx", True) The second parameter indicates whether to retain the Form and QuerryString values of the page.
End If
End If
End Sub

WebForm2.aspx. vb
Private Sub Page_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
If Not Me. Context. Handler Is sender Then
Response. Write (Request. Form ("TextBox1 "))
End If
End Sub


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.