Asp.net basics 5 (custom controls, page-tagged controls)

Source: Internet
Author: User
All the preceding controls are self-encoded, compiled into dll, and inherit Control to generate user controls. This section describes a user control that is directly marked with a page file. That is, a file with the suffix. ascx is created when a new file is created. Then, directly write webcontrol, such as TextBox and Button. However, except for html, body, and other client tags. Code:

 

Code <% @ Control Language = "C #" AutoEventWireup = "true" CodeBehind = "CalcWebUserControl. ascx. cs" Inherits = "TestCodeBehind. CalcWebUserControl" Description = "A simple CalcControl" %>
<Asp: TextBox ID = "txtOper1" runat = "server"> </asp: TextBox> +
<Asp: TextBox ID = "txtOper2" runat = "server"> </asp: TextBox> =
<Asp: TextBox ID = "txtResult" runat = "server"> </asp: TextBox>
<Asp: Button ID = "calcBtn" runat = "server" Text = "computing" onclick = "calcBtn_Click"/>

 

 

The corresponding post code is as follows: Mainly button events

 

Code Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;

Namespace TestCodeBehind
{
Public partial class CalcWebUserControl: System. Web. UI. UserControl
{
Protected void Page_Load (object sender, EventArgs e)
{

}

Protected void calcBtn_Click (object sender, EventArgs e)
{
Int rest = int. Parse (txtOper1.Text. Trim () + int. Parse (txtOper2.Text. Trim ());
TxtResult. Text = rest. ToString ();
}
}
}

 

 

There are two ways to use such controls:

1. Use register to reference the file on the page, and then use src to mark the location of the file.

 

 

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


<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% @ Register TagPrefix = "ownControl" TagName = "webUseControl" Src = "~ /CalcWebUserControl. ascx "%>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<OwnControl: webUseControl ID = "use1" runat = "server"/>
</Div>
</Form>
</Body>
</Html>

 

2. Dynamic Loading: directly loading in the page load event

 

Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;

Namespace TestCodeBehind
{
Public partial class TestCalcWebUserControlForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Dynamically load controls:
// Control calc2 = Page. LoadControl (@ "CalcWebUserControl. ascx ");
// Page. Controls. Add (calc2 );
}
}
}

 

 

 

 

 

 

 

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.