Asp.net subforms and parent forms

Source: Internet
Author: User

Today, I encountered this problem in the project. It is actually a simple application of window. returnValue, not the exclusive content of asp.net. As accumulation, record a simple implementation model.

Figure 1 files used

As shown in figure 1, only two pages are used, Default. aspx is used as the parent page, and Default2.aspx is used as the Child page. The Default. aspx page has two TextBox buttons. The Code is as follows:

Copy codeThe Code is 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">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<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 = "button#click"/>
</Div>
</Form>
</Body>
</Html>

In the Click event of Button1, we register the pop-up window script. The Code is as follows:Copy codeThe Code is as follows: protectedvoid button#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 (! = Null )");
S. Append ("document. all ('textbox1'). value = ;");
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 ());
}

S. Append ("var a = window. showModalDialog ('default2. aspx ');"); is used to pop up the Default2.aspx 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. The Code is as follows:

(Note that the <base target = "_ self"/> mark in the head is very important.

Copy codeThe Code is 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">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Base target = "_ self"/>
</Head>

<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: textbox runat = "server" ID = "t1"> </asp: textbox>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclick = "button#click"/>

</Div>
</Form>
</Body>
</Html>

In the butult2.aspx Page's Button_Click event, we use a script to return a value to the parent page. The Code is as follows:Copy codeThe Code is as follows: protectedvoid button#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 script is successfully registered, we can perform the following experiments:

1) on the Default1.aspx page, enter the number 55 in the TextBox with id a1, and then click the Button.

2) In the pop-up window, enter the number 66 and click the new form button to close the subform.

3) view results

From the results, we can see that we retain the value first input to the parent form, and receive the value passed from the child form.

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.