Example:
A user control is the search function of this site.
User Control:
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "webusercontrol2.ascx. cs" inherits = "_ usercontrols_webusercontrol2" %>
<P>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "query"/>
</P>
Page:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default5.aspx. cs" inherits = "default5" %>
<% @ Register src = "_ usercontrols/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">
<Div>
<Uc1: webusercontrol2 id = "webusercontrol21" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
The user control is used in bold.Code(I drag ASCs directly to aspx, or I can write the code myself. Can be clicked out, very simple ,)
Now there are user controls on the page, and the result set to be queried is displayed on Aspx. We must perform a write operation and specific implementation in the query button of the user control.
Q & A logic: use delegated events to implement functions. (Click the user control button in the user control, and load the event on the page to implement the delegate event ,,,)
Define an event in the query button of the user control (a parameter is required here, and the query condition is the value of the textbox of the user control ).
The parameters of the textbox of the user control must be uploaded to the page and executed in the load event. , An event to query data.
Page CS files ,,,
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Public partial class default5: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Webusercontrol21.myevent + = new _ usercontrols_webusercontrol2.mydele (webusercontrol21_myevent); // note that webusercontrol21 is the referenced User Control ID ,,
}
void webusercontrol21_myevent (string content)
{< br> response. Write ("condition is" + content);
}< BR >}