The value of two custom controls and the application of interfaces

Source: Internet
Author: User

"Two user controls are used in A. aspx page. The user control A has A button, and the textbox value in control B is obtained when you click it.

Because the name will change during generation, how should I write the findcontrol name?
In addition, are there several solutions to such problems ?"

Insus. NET provides its own solution to this problem on the Forum. Let's take a look at the solution:

First, create a site, and then create two user controls: UcA and UcB. Pull a TextBox on The UcB control.Copy codeThe Code is as follows: <% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "UcB. ascx. cs" Inherits = "UcB" %>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>

Create an interface IGetValue:Copy codeThe Code is as follows: IGetValue. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
/// <Summary>
/// Summary description for IGetValue
/// </Summary>
Namespace Insus. NET
{
Public interface IGetValue
{
String GetValue ();
}
}

Next, the user control UcB implements this interface, and the interface returns the Text value of TextBox.Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class UcB: System. Web. UI. UserControl, IGetValue
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Public string GetValue ()
{
Return this. TextBox1.Text. Trim ();
}
}

Create An aspx page, such as Default. aspx, switch to the design mode, and pull the two user controls UcA and UcB to Default. aspx:Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<% @ Register Src = "UcA. ascx" TagName = "UcA" TagPrefix = "uc1" %>
<% @ Register Src = "UcB. ascx" TagName = "UcB" TagPrefix = "uc2" %>
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Fieldset>
<Legend> UcA
</Legend>
<Uc1: UcA ID = "UcA1" runat = "server"/>
</Fieldset>
<Fieldset>
<Legend> UcB
</Legend>
<Uc2: UcB ID = "UcB1" runat = "server"/>
</Fieldset>
</Form>
</Body>
</Html>

Create an Interface to obtain the UcB user control.Copy codeThe Code is as follows: IGetUserControl. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
/// <Summary>
/// Summary description for IGetUserControl
/// </Summary>
Namespace Insus. NET
{
Public interface IGetUserControl
{
UserControl GetUc ();
}
}

After the interface is created, implement the IGetUserControl interface in Default. aspx. cs.Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class _ Default: System. Web. UI. Page, IGetUserControl
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Public UserControl GetUc ()
{
Return this. UcB1;
}
}

At the end, we write the button Click event in the UcA user control:Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class UcA: System. Web. UI. UserControl
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
IGetUserControl ucb = (IGetUserControl) this. Page;
IGetValue value = (IGetValue) ucb. GetUc ();
Response. Write ("<scr" + "ept> alert ('" + value. GetValue () + "') </scr" + "ERT> ");
}
}

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.