下载地址:http://download.csdn.net/source/872577
注册控件的方法
<%@ register TagPrefix="uc" TagName="myDateInput" Src="~/MyDateInput.ascx" %>
引用方法
<uc:myDateInput ID="date1" runat="server" Text="2008-12-15"></uc:myDateInput>
定义属性
public string Text
{
get
{
return this.tb_Date.Text;
}
set
{
this.tb_Date.Text = value;
}
}
定义事件,
这里最好用this引用,以免和父级冲突,或许不用考虑.
父级同名控件应该不会冲突吧,没试过.
protected void btn_select_Click(object sender, EventArgs e)
{
this.p_Calendar.Visible = true;
}
protected void c_1_SelectionChanged(object sender, EventArgs e)
{
this.tb_Date.Text = c_1.SelectedDate.ToShortDateString();
this.p_Calendar.Visible = false;
}
前台
- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyDateInput.ascx.cs" Inherits="MyDateInput" %>
- <table>
- <tr>
- <td>日期输入用户控件</td>
- <td>
- <asp:textbox ID="tb_Date" runat="server"></asp:textbox>
- <asp:button ID="btn_select" runat="server" Text="..." OnClick="btn_select_Click" />
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <asp:panel ID="p_Calendar" runat="server" Visible="false">
- <asp:calendar ID="c_1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" OnSelectionChanged="c_1_SelectionChanged">
- <selecteddaystyle BackColor="#CCCCFF" Font-Bold="True" />
- <todaydaystyle BackColor="#FFCC66" ForeColor="White" />
- <selectorstyle BackColor="#FFCC66" />
- <othermonthdaystyle ForeColor="#CC9966" />
- <nextprevstyle Font-Size="9pt" ForeColor="#FFFFCC" />
- <dayheaderstyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
- <titlestyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
- </asp:calendar>
- </asp:panel>
- </td>
- </tr>
- </table>
后台
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class MyDateInput : System.Web.UI.UserControl
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- public string Text
- {
- get
- {
- return this.tb_Date.Text;
- }
- set
- {
- this.tb_Date.Text = value;
- }
- }
- protected void btn_select_Click(object sender, EventArgs e)
- {
- this.p_Calendar.Visible = true;
- }
- protected void c_1_SelectionChanged(object sender, EventArgs e)
- {
- this.tb_Date.Text = c_1.SelectedDate.ToShortDateString();
- this.p_Calendar.Visible = false;
- }
- }