在一些应用中,希望将模式对话框的值回送(PostBack)到服务端,也就是执行一个服务端操作,从而刷新页面。
举个例子:比方说新建一个订单,此时希望能在模式对话框中选择产品,并将产品的ID返回,同时刷新订单中的产品项,解决的方法有多种,这里只介绍执行服务器操作的方法。
要将客户端数据回送(PostBack)到服务端并执行相应操作,显然必须提交form并设置隐藏域__EVENTTARGET的值,好在asp.net已为我们完成了这些,只要以合适的参数调用__doPostBack就可以了。
另一个问题是如何把模式对话框的值也传过服务端,这个就好说了,有多种方法,在这里用一个隐藏域来保存值,当然这个隐藏域必须加上runat="server"属性。
下面给出部分代码:
主页面TestDialog.aspx:
<form id="form1" runat="server">
<input type="hidden" id="txtValue" runat="server"/>
<br/>
<asp:LinkButton id="lbtnOpen" runat="server" text="打开对话框"/>
<br/>
对话框返回值:<asp:TextBox id="txtValue2" runat="server"/>
</form>
<input type="hidden" id="txtValue" runat="server"/>
<br/>
<asp:LinkButton id="lbtnOpen" runat="server" text="打开对话框"/>
<br/>
对话框返回值:<asp:TextBox id="txtValue2" runat="server"/>
</form>
主页面后置文件TestDiallg.aspx.cs:
private void Page_Load( object Sender, EventArgs e ) {
StringBuilder sb = new StringBuilder();
if ( ! IsClientScriptBlockRegistered( "clientScript" ) ) {
sb.Append( "<script language='javascript'>/n" )
.Append( " function addproduct() { /n" )
.Append( " var ret = window.showModalDialog( 'test2.aspx' ); /n" )
.Append( " if ( ret != undefined ) { /n" )
.Append( " document.getElementById('" + txtValue.ClientID + "').value = ret /n" )
.Append( " __doPastBack('" + lbtnOpen.UniqueID + "', '');/n" )
.Append( " } /n" )
.Append( " }/n " )
.Append( "</script>/n" );
RegisterClientScriptBlock( "clientScript", sb.ToString() );
}
lbtnOpen.Attributes.Add( "onClick", "javascript:addproduct();" );
}
private void lbtnOpen_Click( object Sender, EventArgs e ) {
txtValue2.Text = txtValue.Value;
}
private void InitializeComponent()
{
this.Load += new EventHandler( this.Page_Load );
this.lbtnOpen.Click += new EventHandler( this.lbtnOpen_Click );
}
StringBuilder sb = new StringBuilder();
if ( ! IsClientScriptBlockRegistered( "clientScript" ) ) {
sb.Append( "<script language='javascript'>/n" )
.Append( " function addproduct() { /n" )
.Append( " var ret = window.showModalDialog( 'test2.aspx' ); /n" )
.Append( " if ( ret != undefined ) { /n" )
.Append( " document.getElementById('" + txtValue.ClientID + "').value = ret /n" )
.Append( " __doPastBack('" + lbtnOpen.UniqueID + "', '');/n" )
.Append( " } /n" )
.Append( " }/n " )
.Append( "</script>/n" );
RegisterClientScriptBlock( "clientScript", sb.ToString() );
}
lbtnOpen.Attributes.Add( "onClick", "javascript:addproduct();" );
}
private void lbtnOpen_Click( object Sender, EventArgs e ) {
txtValue2.Text = txtValue.Value;
}
private void InitializeComponent()
{
this.Load += new EventHandler( this.Page_Load );
this.lbtnOpen.Click += new EventHandler( this.lbtnOpen_Click );
}
对话框页面test2.aspx:
<form id="form1" runat="server">
<asp:TextBox id="txtValue" runat="server"/>
<br/>
<input type="button" id="hlnkOk" value="确定" runat="server"/>
</form>
<asp:TextBox id="txtValue" runat="server"/>
<br/>
<input type="button" id="hlnkOk" value="确定" runat="server"/>
</form>
对话框页面后置文件Test2.aspx.cs
private void Page_Load( object Sender, EventArgs e ) {
StringBuilder sb = new StringBuilder();
if ( ! IsClientScriptBlockRegistered( "clientScript" ) ) {
sb.Append( "<script language='javascript'>/n" )
.Append( " function closedialog() { /n" )
.Append( " window.returnValue = document.getElementById('" + txtValue.ClientID + "').value;/n" )
.Append( " window.close();/n" )
.Append( " }/n " )
.Append( "</script>/n" );
RegisterClientScriptBlock( "clientScript", sb.ToString() );
}
hlnkOk.Attributes.Add( "onClick", "javascript:closedialog();" );
}
StringBuilder sb = new StringBuilder();
if ( ! IsClientScriptBlockRegistered( "clientScript" ) ) {
sb.Append( "<script language='javascript'>/n" )
.Append( " function closedialog() { /n" )
.Append( " window.returnValue = document.getElementById('" + txtValue.ClientID + "').value;/n" )
.Append( " window.close();/n" )
.Append( " }/n " )
.Append( "</script>/n" );
RegisterClientScriptBlock( "clientScript", sb.ToString() );
}
hlnkOk.Attributes.Add( "onClick", "javascript:closedialog();" );
}
注意:在对话框页面任何回传(PostBack)操作都将导致一个新窗口被打开,解决方法请到csdn论坛查找。