ASP. NET user control operation ASPX page (capture user control events on the ASPX page)

Source: Internet
Author: User

1. Simple instances

First, create a "web user control" WebUserControl1.ascx,

Add a button on the page and respond to the Click event:

[Csharp] <asp: Button ID = "Button1" runat = "server" Text = "enter a random number" onclick = "button#click"/>
<Asp: Button ID = "Button1" runat = "server" Text = "enter a random number" onclick = "button#click"/>

Then write the background code:

[Csharp] // defines a non-data event processing method
Public event EventHandler Click;
 
Protected void button#click (object sender, EventArgs e)
{
Click (this, EventArgs. Empty );
}
// Define a non-data event processing method
Public event EventHandler Click;

Protected void button#click (object sender, EventArgs e)
{
Click (this, EventArgs. Empty );
}

Now we have finished the user control. Create a WebForm1.aspx page, add a Textbox to the page, and add the user control to the page:

[Html] <% @ Register src = "WebUserControl1.ascx" tagname = "WebUserControl1" tagprefix = "uc1" %>
<! 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 ID = "TextBox1" runat = "server"> </asp: TextBox>
<Br/>
<Uc1: WebUserControl1 ID = "WebUserControl11" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
<% @ Register src = "WebUserControl1.ascx" tagname = "WebUserControl1" tagprefix = "uc1" %>
<! 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 ID = "TextBox1" runat = "server"> </asp: TextBox>
<Br/>
<Uc1: WebUserControl1 ID = "WebUserControl11" runat = "server"/>
</Div>
</Form>
</Body>
</Html>

Then, we add a function response to the user control event in the background. The Code is as follows:

[Csharp] protected void Page_Load (object sender, EventArgs e)
{
WebUserControl11.Click + = new EventHandler (WebUserControl11_Click );
}
 
Void WebUserControl11_Click (object sender, EventArgs e)
{
TextBox1.Text = new Random (). Next (1000,999 9). ToString ();
}
Protected void Page_Load (object sender, EventArgs e)
{
WebUserControl11.Click + = new EventHandler (WebUserControl11_Click );
}

Void WebUserControl11_Click (object sender, EventArgs e)
{
TextBox1.Text = new Random (). Next (1000,999 9). ToString ();
}

At this point, our example is complete. In aspx, we can capture user control events and do what we want to do.

 

 

2. example with event data

Of course, sometimes we not only want to capture events on the aspx page, but also want to get some parameters. At this time, EventArgs will need to appear.

First, create a web user control WebUserControl2.ascx

Add two buttons on the page to respond to the Click event respectively, as shown below:

[Html] <asp: Button ID = "Button1" runat = "server" Text = "Modify the page title to edit"
Onclick = "button#click"/>
<Asp: Button ID = "Button2" runat = "server" Text = "Modify the page title to add"
Onclick = "Button2_Click"/>
<Asp: Button ID = "Button1" runat = "server" Text = "Modify the page title to edit"
Onclick = "button#click"/>
<Asp: Button ID = "Button2" runat = "server" Text = "Modify the page title to add"
Onclick = "Button2_Click"/>

C # code: First add a class ChangePageTitleEventArgs to pass event data. The Code is as follows:

[Csharp] public class ChangePageTitleEventArgs: EventArgs
{
Public ChangePageTitleEventArgs (string title)
{
This. Title = title;
}
 
Public string Title
{
Get;
Private set;
}
 
}
Public class ChangePageTitleEventArgs: EventArgs
{
Public ChangePageTitleEventArgs (string title)
{
This. Title = title;
}

Public string Title
{
Get;
Private set;
}

}

Then we will compile the background code of the user control:

[Csharp] public event EventHandler <ChangePageTitleEventArgs> ChangePageTitle;
 
Protected void button#click (object sender, EventArgs e)
{
ChangePageTitle (this, new ChangePageTitleEventArgs ("edit "));
}
 
Protected void Button2_Click (object sender, EventArgs e)
{
ChangePageTitle (this, new ChangePageTitleEventArgs ("new "));
}
Public event EventHandler <ChangePageTitleEventArgs> ChangePageTitle;

Protected void button#click (object sender, EventArgs e)
{
ChangePageTitle (this, new ChangePageTitleEventArgs ("edit "));
}

Protected void Button2_Click (object sender, EventArgs e)
{
ChangePageTitle (this, new ChangePageTitleEventArgs ("new "));
}

 

The code of the user control is OK. Then we create a WebForm2.aspx page to add the user control to this page.

[Csharp] <% @ Register src = "WebUserControl2.ascx" tagname = "WebUserControl2" tagprefix = "uc1" %>
<! 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">
<Uc1: WebUserControl2 ID = "WebUserControl21" runat = "server"/>
</Form>
</Body>
</Html>
<% @ Register src = "WebUserControl2.ascx" tagname = "WebUserControl2" tagprefix = "uc1" %>
<! 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">
<Uc1: WebUserControl2 ID = "WebUserControl21" runat = "server"/>
</Form>
</Body>
</Html>

Compile the background code and add the processing function for the time of the user control:

[Csharp] protected void Page_Load (object sender, EventArgs e)
{
WebUserControl21.ChangePageTitle + = new EventHandler <ChangePageTitleEventArgs> (WebUserControl21_ChangePageTitle );
}
 
Void WebUserControl21_ChangePageTitle (object sender, ChangePageTitleEventArgs e)
{
This. Page. Title = e. Title;
}
Protected void Page_Load (object sender, EventArgs e)
{
WebUserControl21.ChangePageTitle + = new EventHandler <ChangePageTitleEventArgs> (WebUserControl21_ChangePageTitle );
}

Void WebUserControl21_ChangePageTitle (object sender, ChangePageTitleEventArgs e)
{
This. Page. Title = e. Title;
}

Now, we have completed the code and run the test. The effect is as follows:

 


 

 

From tu jiankai's column

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.