In web development, reusableCodeWe can write it as a general module for reference wherever necessary. This article describes how to add attributes to a user control to implement ASP. NET web programming.
In web development, some code is often repeated in many places, such as the navigation bar, user logon/registration, and some fixed topics on the home page. These reusable codes can be written as a common module for reference. This saves development time and facilitates future maintenance.
In ASP. net web programming provides a "User Control" that can help us do this. The file extension is ". because the ascx file is used to insert the ASPX page, and An ASPX form can only contain one <form> flag, therefore, the ascx user control cannot contain the <form> </form> flag.
The following uses a classic getting started example to create a simple user control named hello. ascx:
<HTML> <Body> <H1> classic Entry Example <HR> <H3> Hello word </Body> </Html>
|
Save this code as a hello. ascx file and call it on the ASPX page. The specific call is as follows:
Hello. aspx <% @ Register tagprefix = "Wen" Tagname = "hello" src = "Hello. ascx" %> <HTML> <Body> <Form ID = FRM runat = Server> <Wen: Hello id = myhello runat = Server> </Form> </Body> </Html>
|
Enter http: // localhost/Hello. aspx in the IP address of the IE browser to run. The "Hello word" string is printed on the page ".
Code Description: 1) command @ register defines the label name "hello" and label prefix name "Wen" of the user control file ";
2) the src attribute is the file name associated with the user control;
3) <Wen: Hello id = myhello runat = Server> is a statement that calls the user control hello. ascx in the aspx form.
The preceding DEMO code does not add attributes to the control. Next we will take a user login file, write it as a user control, and add the username and password attributes to it. It is easy to add attributes to a user control, as long as it is defined in the <SCRIPT> </SCRIPT> block in the ascx file.
Userlogin. ascx <HTML> <Title> User Logon </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> <HR> <Tr> <TD> </TD> <TD> <asp: linkbutton text = "login" runat = "server"> </TD> </Tr> </Table> </Body> </Html> <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, we have added the username and password attributes to the userlogin. ascx file. The following demo demonstrates how to reference these two attributes on the ASPX page.
Userlogin. aspx <% @ Register tagprefix = "Wen" Tagname = "userlogincontorl" src = "userlogin. ascx" %> <HTML> <Title> reference attribute </title> <Body> <Form runat = "server"> <Wen: userlogin. ascxid = "mylogin" runat = "server"> </Form> Username: <asp: Label id = "lab1" runat = "server"/> <br> Password: <asp: Label id = "lab2" runat = "server"/> <br> </Body> </Html> <Script language = "C #" runat = "server"> Void page_load (Object sender, eventargs e ){ If (ispostback ){ Lab1.text = mylogin. Username; Lab2.text = mylogin. Password; } } </SCRIPT> |