前面所有的控件为自己编码,然后编译成dll,继承Control来生成用户控件。
现在介绍一种,直接用页面文件来标记的用户控件。
也就是在新建立文件的时候建立,后缀名为.ascx 的文件。
然后再其中直接写入webcontrol ,如TextBox,Button等。但是除过html,body等客户端标记。
代码:
代码
<%@ 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="计算" onclick="calcBtn_Click"/>
<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="计算" onclick="calcBtn_Click"/>
对应的后置代码如下:主要是button事件
代码
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();
}
}
}
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();
}
}
}
对此类控件的使用有两种方式:
1,在页面用register来引用,然后src标记文件所在的位置
<%@ 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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ownControl:webUseControl ID="use1" runat="server" />
</div>
</form>
</body>
</html>
2,动态加载,在页面load事件里直接加载
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)
{
//动态加载控件:
//Control calc2 = Page.LoadControl(@"CalcWebUserControl.ascx");
//Page.Controls.Add(calc2);
}
}
}
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)
{
//动态加载控件:
//Control calc2 = Page.LoadControl(@"CalcWebUserControl.ascx");
//Page.Controls.Add(calc2);
}
}
}