In this demonstration, an interface is used to dynamically load the user control on the webpage, and JQuery is used to pass the webpage processing value to the user control.
In programming, I prefer to use interfaces and think they can process the same behavior between different objects.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
/// <Summary>
/// Summary description for ISetValable
/// </Summary>
Namespace Insus. NET
{
Public interface ISetValable
{
Void SetValue (string value );
}
}
The above interface is to assign a value to the control after the object is implemented.
Next, we create a user control. The ascx of the user control places a Label to display the value passed from the page. In the real environment, it may not be a simple Label control, but other controls or objects.
Copy codeThe Code is as follows:
<% @ Control Language = "C #" AutoEventWireup = "true" CodeFile = "InsusUc. ascx. cs" Inherits = "InsusUc" %>
<Asp: Label ID = "LabelMessage" runat = "server" Text = ""> </asp: Label>
In the ascx. cs code, you need to implement the interface and assign the parameters of the method implemented by the interface to the Text of the Label.
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 InsusUc: System. Web. UI. UserControl, ISetValable
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Public void SetValue (string value)
{
This. LabelMessage. Text = value;
}
}
OK. The interface and user control have been created. You need to create a webpage. Write a web method in. aspx. cs:
. Aspx:
Animation demonstration: