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