Loopback (postback) The return value of the modal dialog box to the service side

Source: Internet
Author: User
Tags tostring
dialog box in some applications, you want to echo (postback) The value of the modal dialog box to the server, which is to perform a service-side operation to refresh the page.

For example: Create a New order, at this time want to be able to select the product in the modal dialog box, and the product ID return, while refreshing the product items in the order, there are many ways to solve, here only the way to perform server operations.

To send the client data back (postback) to the server and perform the appropriate action, it is obvious that you must submit the form and set the value of the hidden field __eventtarget, but ASP.net has done this for us, as long as you call __doPostBack with the appropriate parameters.

Another problem is how to pass the value of the modal dialog box to the server, which is all right, there are several ways to save the value with a hidden field, of course, this hidden field must be runat= "server" attribute.

Some of the code is given below:

Main Page testdialog.aspx:


<form id= "Form1" runat= "Server" >
<input type= "hidden" id= "TxtValue" runat= "Server"/>
<br/>
<asp:linkbutton id= "Lbtnopen" runat= "Server" text= "Open dialog box"/>
<br/>
dialog box return value: <asp:textbox id= "txtValue2" runat= "Server"/>
</form>

Main Page post file 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);
}

dialog box Page test2.aspx:

<form id= "Form1" runat= "Server" >
<asp:textbox id= "TxtValue" runat= "Server"/>
<br/>
<input type= "button" id= "Hlnkok" value= "OK" runat= "Server"/>
</form>

dialog box page post file 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 ();");
}



Related Article

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.