User control example

Source: Internet
Author: User
A simple example of user control is used as a user control for user login.
It is a very common problem for users to log on to a system. Here I want to use such a problem as a user control to facilitate various users.
1. Create a new Web user control in the project. The added name is FrLogin. ascs.
2. Drag and drop the required control to the web. For example, two labels, two TextBox, and one Button are named lblUserName, lblPassword, txtUserName, txtPassword, and btnLogin.
3. Add the Property Code of two TextBox
Public string UserName
{
Get {return this.txt UserName. Text ;}
Setincluthis.txt UserName. Text = value ;}
}
Public string Password
{
Get {return this.txt Password. Text ;}
Setincluthis.txt Password. Text = value ;}
}
In this way, you can get or set their values when dragging your user control to another Web page.
4. Add a Button event proxy
Double-click your btnLogin to generate the corresponding event processing function.
Private void btnRegister_Click (object sender, System. EventArgs e)
{}
Then declare the event proxy
Public delegate void OnRegisterClickedHandler (object sender, System. EventArgs e); note that the signature here must be correct.
Public event OnRegisterClickedHandler OnRegisterClicked;
5. Modify the btnRegister_Click function
Add the following code to execute the proxy:
If (OnRegisterClicked! = Null)
OnRegisterClicked (sender, e );
By now, your user control has been set. Is it easy? Because the. net environment has helped you complete a lot of background operations.
6. Set FrLogin. drag and drop ascs to the page you want to reference ,. net has generated <% @ Register TagPrefix = "uc1" TagName = "FrLogin" Src = "FrLogin. ascx "%>
<Uc1: FrLogin id = "FrLogin1" backColor = "Black" runat = "server"> </uc1: FrLogin>. The defect here is that it does not help you generate protected FrLogin FrLogin1 in TestPage. aspx. cs; you need to manually add the code. Note that the control name must be consistent with that in the html definition.

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.