Today, I received a project that used a lot of Asp.net user-defined controls. I think it would be nice to add custom attributes to the custom controls, I have been searching for Baidu and Google for a long time and haven't found what I want. I accidentally found that adding custom attributes to user-defined controls is as simple as this.CodePost it for your reference.
TheSource code:
File Name: default2.aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default2.aspx. cs" inherits = "default2" %>
<% @ Register src = "user_control/test. ascx" tagname = "test" tagprefix = "uc1" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Uc1: Test ID = "test1" runat = "server" showmsg = "http://blog.my400800.cn
"/>
</Div>
</Form>
</Body>
</Html>
File Name: default2.aspx. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class default2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
}
Source code of the custom control:
File Name: Test. ascx
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "test. ascx. cs" inherits = "user_control_test" %>
<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label>
File Name: Test. ascx. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public partial class user_control_test: system. Web. UI. usercontrol
{
# Region custom control property settings
Private string myvar;
Public String showmsg
{
// Myvar
Get {return myvar ;}
Set {myvar = value ;}
}
# Endregion
protected void page_load (Object sender, eventargs e)
{< br> label1.text = (myvar);
}< BR >}