自定义控件入门

来源:互联网 发布:算法 英语翻译 编辑:程序博客网 时间:2024/06/10 07:43

下载地址: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;
    }

前台

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyDateInput.ascx.cs" Inherits="MyDateInput" %>
  2. <table>
  3.     <tr>
  4.     <td>日期输入用户控件</td>
  5.     <td>
  6.         <asp:textbox ID="tb_Date" runat="server"></asp:textbox>
  7.         <asp:button ID="btn_select" runat="server" Text="..." OnClick="btn_select_Click" />
  8.     </td>
  9.     </tr>
  10.     <tr>
  11.     <td></td>
  12.     <td>
  13.         <asp:panel ID="p_Calendar" runat="server" Visible="false">
  14.             <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">
  15.                 <selecteddaystyle BackColor="#CCCCFF" Font-Bold="True" />
  16.                 <todaydaystyle BackColor="#FFCC66" ForeColor="White" />
  17.                 <selectorstyle BackColor="#FFCC66" />
  18.                 <othermonthdaystyle ForeColor="#CC9966" />
  19.                 <nextprevstyle Font-Size="9pt" ForeColor="#FFFFCC" />
  20.                 <dayheaderstyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
  21.                 <titlestyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
  22.             </asp:calendar>
  23.         </asp:panel>
  24.     </td>
  25.     </tr>
  26. </table>

后台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class MyDateInput : System.Web.UI.UserControl
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.     }
  16.     public string Text
  17.     {
  18.         get
  19.         {
  20.             return this.tb_Date.Text;
  21.         }
  22.         set
  23.         {
  24.             this.tb_Date.Text = value;
  25.         }
  26.     }
  27.     protected void btn_select_Click(object sender, EventArgs e)
  28.     {
  29.         this.p_Calendar.Visible = true;
  30.     }
  31.     protected void c_1_SelectionChanged(object sender, EventArgs e)
  32.     {
  33.         this.tb_Date.Text = c_1.SelectedDate.ToShortDateString();
  34.         this.p_Calendar.Visible = false;
  35.     }
  36. }

原创粉丝点击