在文本框中选择日期时如何启用下拉列表 [英] how to enable Dropdownlist when date is selected in the text box

查看:94
本文介绍了在文本框中选择日期时如何启用下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当日期放在下拉列表上方的文本框中时,如何启用(原因:Dropdownlist).如果您知道

How to enable( Reason:Dropdownlist )when date is put in text box present above the drop down list .kindly post the code if u know

推荐答案

一个方法是使用 textchanged事件 [ ^ ],并检查用户是否使用输入了有效日期datetime-tryparse [ ^ ]方法.然后启用下拉列表.
您可以使用已启用属性启用下拉列表[^ ]
例如:yourDropdownID.Enabled ="True";
One approach would be to use the textchanged event[^] of the textbox and check if the user has entered a valid date using datetime-tryparse[^] method. Then enable the dropdownlist.
You can enable the dropdownlist by using the Enabled property[^]
ex: yourDropdownID.Enabled = "True";


设置文本框的Autopostback属性为true

Set Autopostback property of textbox true

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   DateTime date1;
   if (DateTime.TryParse(dateString, out dateValue))
        dropdownlist1.Enabled=true;
   else
        dropdownlist1.Enabled=false;
}


Default.aspx页面代码.

Default.aspx page Code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page2.aspx.cs" Inherits="Default.aspx" %>

<!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>DropDown Demo with Textbox</title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" style="Width:120px" runat="server"

            Enabled="False">
    <asp:ListItem>santosh_k</asp:ListItem>
    <asp:ListItem>Amit_K</asp:ListItem>
    </asp:DropDownList>
    </div>
    <div>Choose Date:
    <asp:TextBox ID="txtDate"  style="margin-top:50px"  runat="server"></asp:TextBox>
        <asp:Calendar ID="Calendar1" runat="server"

            onselectionchanged="Calendar1_SelectionChanged1"></asp:Calendar>
    </div>
    <div>

    </div>
    </form>
</body>
</html>




Default.aspx.cs背后的代码




Default.aspx.cs Code Behind

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
   {
       txtDate.Text = Calendar1.SelectedDate.ToString();
       DropDownList1.Enabled = true;
   }


这篇关于在文本框中选择日期时如何启用下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆