关于__doPostBack()方法?
解决方法:
页面一:
提供一个锚点:
<a onclick="javascript:window.open('WebForm2.aspx','_blank')">open webform2</a>
一个文本框,用于显示一些信息:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
一个隐藏的服务器button:
<asp:Button id="Button1" runat="server" Text="Button" Visible="False"></asp:Button>
Button的后台事件为:
private void Button1_Click(object sender, System.EventArgs e)
{
this.TextBox1.Text ="has click...";
}
页面二:
添加一个服务器按钮,添加事件:
private void Button1_Click(object sender, System.EventArgs e)
{
string s="<script language=javascript>";
s+="window.opener.__doPostBack('Button1','');";
s+="</script>";
this.Page.RegisterClientScriptBlock("a",s);
}
此时,在页面二中无法成功地触发页面一中的__doPostBack()事件,是由于在没有某些特定控件的时候,html文件中并不会产生__doPostBack()函数的javascript代码,所以在页面二中点击按钮时会提示“对象不支持此方法”:
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
要使用页面内容中产生以上代码,可以添加一个LinkButton控件(好像只有该控件可以??)
另外的方法是在Page_Load()加上下面方法:
this.Page.GetPostBackEventReference(Button1);
上面方法将触发产生__doPostBack()的javascript代码,msdn中的解释是:
“获取对客户端脚本函数的引用,调用该函数将使用服务器发送回该页”
以上代码可以应用于在子窗口中修改数据,然后刷新父窗口的情况
另外,注意“__doPostBack()”,方法名中是两道下划线,并且区分大小写。
