Asp.net basics 3 (use of custom controls)

Source: Internet
Author: User
First, the implementation of the custom Control inherits the Control to add custom properties, and then rewrite the Render method. This method is used to output the Control code. Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Linq;
Using System. Text;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Namespace OwnControl
{
Public class NameControl: Control
{
Private string name;
Private bool isImport;

Public string Name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}

Public bool IsImport
{
Get
{
Return isImport;
}
Set
{
IsImport = value;
}
}

Protected override void Render (HtmlTextWriter writer)
{
String txt;
If (isImport)
Txt = string. Format ("Else
Txt = string. Format ("Writer. Write (txt );
}
}
}

 

 

Write the above files to a separate class project and compile them. Add a reference to the project in a web project.

Then use this control on the asp.net page.

Registration control: <% @ Register Namespace = "OwnControl" TagPrefix = "useown" Assembly = "OwnControl" %>

Control: <useown: NameControl ID = "use1" runat = "server" Name = "yijianliang" IsImport = "true"> </useown: NameControl>

 

Code <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "UseOwnControlForm. aspx. cs" Inherits = "TestCodeBehind. UseOwnControlForm" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% @ Register Namespace = "OwnControl" TagPrefix = "useown" Assembly = "OwnControl" %>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Useown: NameControl ID = "use1" runat = "server" Name = "yijianliang" IsImport = "true"> </useown: NameControl>
<Useown: NameControl ID = "user2" runat = "server" Name = "yijianxiang" IsImport = "false"> </useown: NameControl>
</Div>
</Form>
</Body>
</Html>

 

 

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.