首页 > 编程 > .NET > 正文

Asp.Net中的脚本回调和Server.Transfer页面传值

2024-07-10 13:07:38
字体:
来源:转载
供稿:网友
菜鸟学堂:

在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" 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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表