[ASP. NET Development Series] adding properties to user controls

Source: Internet
Author: User
Tags net reference return string tag name tagname
Asp.net| Control

In web development often some code is repeated in many places, such as navigation bar, user login/registration and a number of fixed columns above the home page. These reusable code can be written as a common module for the need to reference, which saves the development time and convenient maintenance later.

A "user control" is provided in asp.net Web programming to help us do this, with the file name extension ". ascx", because the ascx file is used to insert into the ASPX page, and an ASPX form can contain only one <form> flag, Therefore, the Ascx user control cannot contain <form></form> flags.

The following is a classic introductory example to create a simple user control with a file name of Hello.ascx:

<body>

</body>

Save this code as a Hello.ascx file and call it on the ASPX page, as follows:

Hello.aspx

<% @Register tagprefix= "Wen" tagname= "Hello" src= "Hello.ascx"%>

<body>

<form id=frm runat=server>

<wen:hello Id=myhello runat=server>

</form>

</body>

Enter http://localhost/Hello.aspx running in the address of IE browser, will print out the string "Hello word" on the page.

Code Description: 1 The instruction @register defines the user control file's tag name "Hello" and the tag prefix name "Wen";

2 The SRC attribute is the associated filename connected to the user control;

3) <wen:hello Id=myhello runat=server> This is the statement that invokes the user control Hello.ascx in an ASPX form.

The above demo code does not add properties to the control, let's take a user login file, write it as a user control, add username and password to the two properties. Adding a property to a user control is simple, as long as it is defined in the <script></script> block in the ascx file.

Userlogin.ascx

<title> User Login </title>

<body>

<table>

<tr>

<td> User name:</td>

<td><asp:textbox id= "txt1" runat= "Server" ></td>

</tr>

<tr>

<td> Password:</td>

<td><asp:textbox id= "txt2" textmode= "password" runat= "Server" ></td>

</tr>

<tr>

<td></td>

<td><asp:linkbutton text= "Landing" runat= "Server" ></td>

</tr>

</table>

</body>

<script language= "C #" runat= "Server" >

public string username{

Get{return txt1. Text;}

Set{txt1. Text=value;}

}

public string password{

Get{return txt2. Text;}

Set{txt2. Text=value;}

}

</script>

Now that we've added the username and Password properties to the Userlogin.ascx file, the following demo demonstrates how to reference both properties on an ASPX page.

Userlogin.aspx

<% @Register tagprefix= "Wen" tagname= "Userlogincontorl" src= "Userlogin.ascx"%>

<title> Reference Properties </title>

<body>

<form runat= "Server" >

<wen:userlogin.ascx id= "MyLogin" runat= "Server" >

</form>

User name: <asp:label id= "LAB1" runat= "Server" ><br>

Password: <asp:label id= "LAB2" runat= "Server" ><br>

</body>

<script language= "C #" runat= "Server" >

void Page_Load (Object Sender,eventargs e) {

if (IsPostBack) {

Lab1.text=mylogin.username;

Lab2.text=mylogin.password;

}

}

</script>



Related Article

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.