自己动手做WEB控件

   新年,把自己刚做的一个控件拿出来给大家分享,给自己动手做控件的朋友切磋切磋。
    现在在山西长治带一帮童子军做一个警用地理信息系统,前期一直在做设计,现在项目刚刚进入编码阶段。一日有组员做一个数据录入界面,有日期输入字段。该老弟直接用一个TEXTBOX就完事了,我批评说应该使用日历控件让用户可以选择。
    该老弟就在界面上直接拖了一个Calendar控件,我说你能不能做一个弹出式的,该老弟说微软没有提供弹出式的日历控件。我一查,果真如此,说那只能自己定义一个控件了。该老弟说不会。有项目组成员踊跃自荐,说他来做。我想想应该鼓励,就让他做了。晚上我来检查成果,他是提交了。他使用一个select和一个Calendar控件包装成了一个下拉式日历控件,具体源码如下:
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="dpList.ascx.cs" ClassName="dpList" Inherits="dpList" %>
<style type="text/css" media="screen">
#floater {
position: absolute;
left: 10px;
top: 46px;
z-index: 1;
}
-->
</style>
<script language="javascript" type="text/javascript">
function show()
{
document.getElementById("floater").style.display='inline';

}
</script>
<select οnfοcus="show()" style="width: 112px"  id="Select1" runat="server">
<option value="选择日期"> </option>
</select>
<div id="floater" style=" display:none;">
<asp:Calendar ID="Calendar1"
    runat="server" BackColor="White" BorderColor="Black"
    Font-Names="Times New Roman" Font-Size="10pt" ForeColor="Black" Height="220px" NextPrevFormat="FullMonth" Width="400px" OnSelectionChanged="Calendar1_SelectionChanged" DayNameFormat="Shortest" TitleFormat="Month">
    <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
    <TodayDayStyle BackColor="#CCCC99" />
    <DayStyle Width="14%" />
    <OtherMonthDayStyle ForeColor="#999999" />
    <NextPrevStyle Font-Size="8pt" ForeColor="White" />
    <DayHeaderStyle Font-Bold="True" Font-Size="7pt" ForeColor="#333333" Height="10pt" BackColor="#CCCCCC" />
    <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="13pt"
        ForeColor="White" Height="14pt" />
    <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt"
        ForeColor="#333333" Width="1%" />
</asp:Calendar>
</div>
后台:
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 dpList : System.Web.UI.UserControl
{
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Select1.Items[0].Value = Calendar1.SelectedDate.ToShortDateString();
       
    }
}

    应该说这是一个用户定义的控件,但是该老兄将代码提交给使用者的时候竟然不知道怎么样将选定的日期传出,要调用者使用会话变量去取。我想这也是初学者容易犯的一个错误。
    在又好气又好笑的同时,我给他添加了一个GET/SET函数,解决了他的设置和取值的问题:
    public DateTime myDate
    {
        get
        {
            return Calendar1.SelectedDate;
        }
        set
        {
            Calendar1.SelectedDate = value;
            Select1.Items[0].Value = value.ToShortDateString();
          }
    }

    public string getDateString()
    {
        return Calendar1.SelectedDate.ToShortDateString();
    }

    解决了设置和读取值的问题后,调用者开始接入界面,到调用人员调试界面的时候,问题开始出现:当界面上有几个日期控件的时候,所有的弹出的日历只能在第一个日期控件的位置下显示,所有的修改日期只能影响到第一个日期控件的值。
    我看了看,只能一笑了之。这是因为他使用的是客户端的DIV和SELECT的原因,实例化后并没有生成不同的名称的标签。
    叹了口气,我只能重做。
    花了大约15分钟,重新制作了这个日期控件:
前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="dpList.ascx.cs" Inherits="dpList" %>

<div id="div1" runat="server" style="width: 268px; height: 20px; display: block; position: absolute; clear: none;">

    <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" Width="152px"> </asp:TextBox>
    <asp:Button ID="Button1" runat="server" Font-Size="12px" ForeColor="Blue" OnClick="Button1_Click" Text="▼" TabIndex="-1" />

    <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="Black" Font-Names="Verdana" Font-Size="9pt" ForeColor="Black"
    Height="189px" NextPrevFormat="ShortMonth" Width="263px"
    OnSelectionChanged="Calendar1_SelectionChanged" OnVisibleMonthChanged="reOpen" Visible="False" BorderStyle="Solid" CellSpacing="1">
    <SelectedDayStyle BackColor="#333399" ForeColor="White" />
    <TodayDayStyle BackColor="#999999" ForeColor="White" />
    <OtherMonthDayStyle ForeColor="#999999" />
    <DayStyle BackColor="#CCCCCC" />
    <NextPrevStyle Font-Size="8pt" ForeColor="White" Font-Bold="True" />
    <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
        Height="8pt" />
    <TitleStyle BackColor="#333399" Font-Bold="True" Font-Size="12pt" ForeColor="White"
        Height="12pt" BorderStyle="Solid" />
</asp:Calendar>
</div>
&nbsp;&nbsp;
后台:
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;
using System.Text;

/// <summary>
///模块编号: <模块编号,可以引用系统设计中的模块编号>
///编写日期:2010-02-07
/// </summary>
public partial class dpList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Calendar1.Visible)
            {
                Calendar1.Visible = false;
                div1.Style.Value = "width: 268px; height: 20px; display: block; position: absolute; clear: none;";
            }
        }
    }


    public DateTime myDate
    {
        get
        {
            return Calendar1.SelectedDate;
        }
        set
        {
            Calendar1.SelectedDate = value;
            TextBox1.Text = value.ToString("yyyy-MM-dd");
        }
    }

    public string getDateString()
    {
        return Calendar1.SelectedDate.ToShortDateString();
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
        Calendar1.Visible = false;
        div1.Style.Value = "width: 268px; height: 20px; display: block; position: absolute; clear: none;";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Calendar1.Visible = true;
        div1.Style.Value = "width: 268px; height: 180px; display: block; position: absolute; clear: none;";
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() != "")
        {
            Calendar1.SelectedDate = Convert.ToDateTime(TextBox1.Text);
        }
    }

    //选择月份重新打开的函数:
    protected void reOpen(object sender, MonthChangedEventArgs e)
    {
        Calendar1.Visible = true;
        div1.Style.Value = "width: 268px; height: 180px; display: block; position: absolute; clear: none;";
    }
}
然后又加上了TEXTBOX输入的检验控件,交付调用人员后基本可以使用了。
在进一步测试的过程中又发现当客户端中弹出日历后从TEXTBOX直接失去焦点关闭不了日历,又花了两个多小时去修改。
由于DIV没有失去焦点的事件,所以在处理的过程中花费了一些力气。
本来是想直接提供源码给大家的,一来上面的代码可是使用,二来春节准备写该贴的时候儿子(中学生)说花了那么大的力气,应该可以收点费,于是就让儿子去淘宝注册了一个账号,当成宝贝来卖了,以资其游戏费用。
下载完整源码请登录:http://item.taobao.com/auction/item_detail-0db2-6be85da63eee3578daab35dca554e380.jhtml#
收费5元,也测试一下开源软件能否赢利。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闲人1970

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值