实用的自定义组件(静态结构有交互)
.ascx代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="按钮文本框控件.ascx.cs" Inherits="按钮文本框控件" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btn_mins" runat="server" Text="-" onclick="btn_mins_Click" />
<asp:TextBox ID="txt_value" runat="server" Width="40px" Text="0"></asp:TextBox>
<asp:Button ID="btn_add" runat="server" Text="+" onclick="btn_add_Click" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="请输入数字" ControlToValidate="txt_value"
ValidationExpression="^[1-9]\d*$ "></asp:RegularExpressionValidator>
</ContentTemplate>
</asp:UpdatePanel>
.ascx.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 按钮文本框控件 : System.Web.UI.UserControl
{
public int Value {
get { return int.Parse(txt_value.Text); }
//
set { txt_value.Text = value.ToString(); }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_mins_Click(object sender, EventArgs e)
{
int _v = int.Parse(txt_value.Text);
if (_v > 0) { _v--; txt_value.Text = _v.ToString(); }
}
protected void btn_add_Click(object sender, EventArgs e)
{
int _v = int.Parse(txt_value.Text);
if (_v < 10) { _v++; txt_value.Text = _v.ToString(); }
}
}
.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%--添加以前信息才能引入自定义组件--%>
<%@ Register Src="~/testControl.ascx" TagName="testControl" TagPrefix="myCTL" %>
<%@ Register Src="~/按钮文本框控件.ascx" TagName="myControl" TagPrefix="yayun" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<myCTL:testControl ID="TestControl1" runat="server" />
<yayun:myControl id="test2" runat="server"></yayun:myControl>
</div>
</form>
</body>
</html>
效果如下: