在文本框中选择日期时如何启用下拉列表
问题描述:
当日期放在下拉列表上方的文本框中时,如何启用(原因: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;
}