The development requirement of Asp.net is that the user control (UserControl) is integrated with a function. The data to be processed must be stored in the database and displayed on the webpage, so that the user can detect the data to be processed.
In this demonstration, Insus. NET is just a simple example and is implemented using delegation. For better understanding, you can first look at the operation animation demonstration:
Create a PageB. aspx webpage and UcB. ascx user control on your new site. Pull a TextBox, CheckBox, And Button control on UcB. ascx:
Copy codeThe Code is as follows:
UcB. ascx
<% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "UcB. ascx. cs" Inherits = "UcB" %>
<Fieldset>
<Legend> User control field </legend>
Catalog:
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> <br/>
Enabled:
<Asp: CheckBox ID = "CheckBox1" runat = "server"/> <br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Process..." OnClick = "button#click"/>
</Fieldset>
In the UcB. ascx. cs code page, write a delegate attribute to store the data processed in all user controls.
Copy codeThe Code is as follows:
View Code
Private Delegate _ InsusData;
Public Delegate InsusData
{
Set
{
_ InsusData = value;
}
}
Then, in The OnClick event of the Button, store the data to the attribute you just wrote:
Copy codeThe Code is as follows:
View Code
Protected void button#click (object sender, EventArgs e)
{
Object [] data = new object [2];
Data [0] = this. TextBox1.Text. Trim ();
Data [1] = this. CheckBox1.Checked;
_ InsusData. DynamicInvoke (data );
}
Complete UcB. ascx. cs code:
Copy codeThe Code is as follows:
View Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class UcB: System. Web. UI. UserControl
{
Private Delegate _ InsusData;
Public Delegate InsusData
{
Set
{
_ InsusData = value;
}
}
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
Object [] data = new object [2];
Data [0] = this. TextBox1.Text. Trim ();
Data [1] = this. CheckBox1.Checked;
_ InsusData. DynamicInvoke (data );
}
}
Here, I need to pull the user control UcB TO THE PageB page and place the corresponding control to display the data:
Copy codeThe Code is as follows:
PageB. aspx
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "PageB. aspx. cs" Inherits = "PageB" %>
<% @ Register Src = "UcB. ascx" TagName = "UcB" TagPrefix = "uc1" %>
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Uc1: UcB ID = "UcB1" runat = "server"/>
<Br/>
<Fieldset>
<Legend> Page Content
</Legend>
Show User Control Data: <br/>
Catalog:
<Asp: Label ID = "Label1" runat = "server" Text = ""> </asp: Label>
<Br/>
Enabled:
<Asp: CheckBox ID = "CheckBox1" runat = "server" Enabled = "false"/> <br/>
</Fieldset>
</Form>
</Body>
</Html>
In the PageB. aspx. cs code, declare a delegate method with two parameters:
Copy codeThe Code is as follows:
Private delegate void GetUserControlData (string catalog, bool isEnable );
Write another method that is the same as the delegate method (same parameters) You just wrote, and use parameters to display data for the page control:
Copy codeThe Code is as follows:
Void ShowDataToPage (string catalog, bool isEnable)
{
This. Label1.Text = catalog;
This. CheckBox1.Checked = isEnable;
}
Finally, in the Page_Load event in PageB. aspx. cs, you can obtain user control data:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
GetUserControlData obj = new GetUserControlData (ShowDataToPage );
This. UcB1.InsusData = obj;
}
OK, complete PageB. aspx. cs code:
Copy codeThe Code is as follows:
PageB. aspx. cs
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class PageB: System. Web. UI. Page
{
Private delegate void GetUserControlData (string catalog, bool isEnable );
Protected void Page_Load (object sender, EventArgs e)
{
GetUserControlData obj = new GetUserControlData (ShowDataToPage );
This. UcB1.InsusData = obj;
}
Void ShowDataToPage (string catalog, bool isEnable)
{
This. Label1.Text = catalog;
This. CheckBox1.Checked = isEnable;
}
}