Add Custom Attributes of Asp.net Custom Controls

Source: Internet
Author: User

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 >}

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.