Asp.Net中的指令碼回調和Server.Transfer頁面傳值

來源:互聯網
上載者:User
asp.net|server|指令碼|頁面

在Asp.Net中經常要用到指令碼回調和頁面間的傳值,下面是關於ScriptCallBack和Server.Transfer簡單的範例程式碼

WebForm1.aspx
給Head中增加__doPostBack指令碼,如果頁面含有HyperLink等按鈕控制項,該指令碼和2個隱藏控制項"__EVENTTARGET"和"__EVENTARGUMENT"由FrameWork自動產生,若沒有需要手動添加

<SCRIPT language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
    var theform;
    if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
        theform = document.forms["Form1"];//注意此處的FormID
        } else {
        theform = document.Form1;//還有此處
    }
    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);//第二個參數指示是否保留頁面的Form和QuerryString的值
}

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)'第二個參數指示是否保留頁面的Form和QuerryString的值
        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




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.