制作一个用户控件,两个按钮btn1,btn2,将他们的点击事件公开(aspx文件中可以单独编写他们的事件,给出编写的方法),以属性的方式公开两个日期(起,止),(aspx中可以调用他们,给出调用方法)

解决方案 »

  1.   

    用户控件ascx
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="mytest.ascx.cs" Inherits="testuser.mytest" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:Button id="Button1" Text="Button" runat="server"></asp:Button>
    <asp:Button id="Button2" Text="Button" runat="server"></asp:Button>
      

  2.   

    用户控件的后台代码:Mytest.ascx.cs
    namespace testuser
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// mytest 的摘要说明。
    /// </summary>
    public class mytest : System.Web.UI.UserControl
    {
    public delegate void Bu1ClickHandler( object sender ,System.EventArgs e );
    public delegate void  Bu2ClickHandler( object sender , System.EventArgs e );
    public event Bu1ClickHandler bu1click;
    public event Bu2ClickHandler bu2click;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    if( this.bu1click != null )
    bu1click( sender , e );
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    if( this.bu2click != null )
    {
    bu2click( sender ,e );
    }

    }
    public DateTime StartDate{ 
    get{
    if( ViewState["sd"] == null )
    return DateTime.Now;
    else
    return (DateTime)ViewState["sd"];
    }
    set{
    ViewState["sd"] = value;
    }
    }
    public DateTime EndDate
    {
    get{
    if( ViewState["ed"] == null )
    return DateTime.Now;
    else
    return (DateTime)ViewState["ed"];
    }
    set
    {
    ViewState["ed"] = value;
    }
    }
    }
    }
      

  3.   

    引用控件的页面
    <%@ Register TagPrefix="uc1" TagName="mytest" Src="mytest.ascx" %>
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="testuser.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <uc1:mytest id="Mytest1" runat="server"></uc1:mytest>
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 48px" runat="server"></asp:TextBox>
    <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 376px; POSITION: absolute; TOP: 48px" runat="server"
    Text="设定起始时间"></asp:Button>
    <asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 384px; POSITION: absolute; TOP: 96px" runat="server"
    Text="设定结束时间"></asp:Button>
    <asp:Button id="Button3" style="Z-INDEX: 105; LEFT: 288px; POSITION: absolute; TOP: 192px" runat="server"
    Text="显示起始时间"></asp:Button>
    <asp:Button id="Button4" style="Z-INDEX: 106; LEFT: 288px; POSITION: absolute; TOP: 160px" runat="server"
    Text="显示结束时间"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 107; LEFT: 176px; POSITION: absolute; TOP: 184px" runat="server">Label</asp:Label>
    </form>
    </body>
    </HTML>
      

  4.   

    页面后台
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace testuser
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected mytest Mytest1;
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button3;
    protected System.Web.UI.WebControls.Button Button4;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button Button2;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    Mytest1.bu1click += new testuser.mytest.Bu1ClickHandler(Mytest1_bu1click);
    Mytest1.bu2click += new testuser.mytest.Bu2ClickHandler(Mytest1_bu2click);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Button3.Click += new System.EventHandler(this.Button3_Click);
    this.Button4.Click += new System.EventHandler(this.Button4_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    protected void Mytest1_bu1click( object sender , System.EventArgs e )
    {
    Response.Write("bu1 click");
    }
    protected void Mytest1_bu2click( object sender , System.EventArgs e )
    {
    Response.Write("bu2 click");
    } private void Button1_Click(object sender, System.EventArgs e)
    {
    try{
    Mytest1.StartDate = Convert.ToDateTime( this.TextBox1.Text );
    }
    catch( Exception ex )
    {
    Response.Write("格式不对");
    }
    } private void Button2_Click(object sender, System.EventArgs e)
    {
    try
    {
    Mytest1.EndDate = Convert.ToDateTime( this.TextBox2.Text );
    }
    catch( Exception ex )
    {
    Response.Write("格式不对");
    }
    } private void Button4_Click(object sender, System.EventArgs e)
    {
       this.Label1.Text = Mytest1.EndDate.ToString(); } private void Button3_Click(object sender, System.EventArgs e)
    {
    this.Label1.Text = Mytest1.StartDate.ToString();
    }
    }
    }
      

  5.   

    这样要求还是用JavaScript写更加方便。