asp.net child form and parent form interactive real-practice sharing _ Practical tips

Source: Internet
Author: User

Today in the project encountered this problem, in fact, just window.returnvalue simple application, not the exclusive content of ASP.net. As an accumulation, a simple implementation model is recorded.

The file used in Figure 1

As we can see in Figure 1, only two pages are used, where Default.aspx as the parent page, default2.aspx as a subpage. There are two TextBox a button on the Default.aspx page with the following code:

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox runat= "Server" id= "A1" >
</asp:TextBox>
<asp:textbox id= "TextBox1" runat= "Server" ontextchanged= "textbox1_textchanged" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
</div>
</form>
</body>

In the Button1 Click event, we registered the window script with the following code:
Copy Code code as follows:

Protectedvoid Button1_Click (object sender, EventArgs e)
{
StringBuilder s =new StringBuilder ();
S.append ("<script language=javascript>");
S.append ("var a=window.showmodaldialog (' default2.aspx ');");
S.append ("if (a!=null)");
S.append ("document.all (' TextBox1 '). value=a;");
S.append ("</script>");
Type Cstype =this. GetType ();
ClientScriptManager cs = Page.clientscript;
String sname = "LT";
if (!cs. IsStartupScriptRegistered (Cstype, sname))
Cs. RegisterStartupScript (Cstype, Sname, s.tostring ());
}

of which s.append ("Var a=window.showmodaldialog (' default2.aspx ');"); A window to default2.aspx the page and receive its return value.

After receiving the return value, we assign it to TextBox1.

The Default2.aspx page has a TextBox and a button with the following code:

(It is important to note that the <base target= "_self"/> in the head.

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default2.aspx.cs" inherits= "DEFAULT2"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<base target= "_self"/>

<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox runat= "Server" id= "T1" ></asp:textbox>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>

</div>
</form>
</body>

We use a script to return a value to the parent page in the Button_Click event of the Default2.aspx page. The code is as follows:
Copy Code code as follows:

Protectedvoid Button1_Click (object sender, EventArgs e)
{
StringBuilder s =new StringBuilder ();
S.append ("<script language=javascript>" + "\ n");
S.append ("window.returnvalue=") +this. Getselectvalue () + "';" + "\ n");
S.append ("Window.close ()"; + "\ n");
S.append ("</script>");
Type Cstype =this. GetType ();
ClientScriptManager cs = Page.clientscript;
String csname = "Ltype";
if (!cs. IsStartupScriptRegistered (Cstype, CSName))
Cs. RegisterStartupScript (Cstype, CSName, s.tostring ());

}

After the success of the script registration, we can do the following experiment:

1 Open the Default1.aspx page, enter the number 55 in the TextBox with ID A1 and click the button

2 Enter the number 66 in the window and click the button to close the subform form.

3) View the results

From the results, we can see that we have retained the values entered into the parent form and received the values passed over from the subform.

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.