Script callbacks and Server.Transfer page values in asp.net

Source: Internet
Author: User
asp.net|server| Script | page

In ASP.net, the script callback and the values between pages are often used, and the following is a simple sample code for Scriptcallback and Server.Transfer

WebForm1.aspx
Add a __doPostBack script to the head, and if the page contains a Hyperlink button control, the script and 2 hidden controls "__eventtarget" and "__eventargument" are automatically generated by the framework, If you do not need to 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 here
} else {
Theform = document. form1;//and 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:112px; 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 values of the page's form and querrystring are preserved
}

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 the values of the form and querrystring of the page are preserved
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.