Use of user controls of Asp.net,

Source: Internet
Author: User

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 >}

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.