在asp.net c#中使用AutoPostback的问题
我有两个带有日历扩展器的文本框控件和一个下拉列表,如下所示。
<asp:TextBox ID="txtStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="txtStartDate"
runat="server" Format="yyyy-MM-dd" />
<asp:TextBox ID="txtEndDate" runat="server" ReadOnly="True"
ontextchanged="txtEndDate_TextChanged">
</asp:TextBox>
<asp:CalendarExtender ID="txtEndDate_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtEndDate" Format="yyyy-MM-dd">
</asp:CalendarExtender>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
我想做的是,当用户在 txtEndDate 中选择日期时,我想调用一个函数来加载 DropDownList1 中的数据,即 DataBind DropDownList1 。
当我将 txtEndDate 的 AutoPostBack 属性设置为 True 并调用 txtEndDate_TextChanged 上的方法时,该事件不会被触发。
我哪里出错了任何人都可以帮忙。我只想在用户在 txtEndDate
中选择日期时加载 DropDownList1
。我该怎么办。
这是我在 aspx.cs
中所做的,
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String DB = "";
String AccountID = "";
if (Session["login"] != null && Session["db"] != null)
{
AccountID = Session["login"].ToString();
DB = Session["db"].ToString();
Label4.Text = AccountID;
}
else
{
Response.Redirect("log.aspx");
}
}
}
protected void txtEndDate_TextChanged(object sender, EventArgs e)
{
String databs = Session["db"].ToString();
Response.Write(databs);
ddlist1_crtno a = new ddlist1_crtno();
a.filldropdown1(this.DropDownList1, databs);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
// LOG OUT***********////////////
Session.Abandon();
Response.Redirect("log.aspx");
}
}
我将相同的事件放在 ButtonClick 事件上,一切正常。
i have two text box controls with calender extender and a dropdownlist as follows.
<asp:TextBox ID="txtStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="txtStartDate"
runat="server" Format="yyyy-MM-dd" />
<asp:TextBox ID="txtEndDate" runat="server" ReadOnly="True"
ontextchanged="txtEndDate_TextChanged">
</asp:TextBox>
<asp:CalendarExtender ID="txtEndDate_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtEndDate" Format="yyyy-MM-dd">
</asp:CalendarExtender>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
What i want to do is when user selects a date in txtEndDate
i want to call a function to load data in DropDownList1
, i.e DataBind DropDownList1
.
When i Set AutoPostBack
property of txtEndDate
to True and call a method on txtEndDate_TextChanged
the event does not get fired.
Where am i going wrong can anyone help. I just want to load DropDownList1
when user selects a date in txtEndDate
. What do i have to do.
here is what i had done in my aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String DB = "";
String AccountID = "";
if (Session["login"] != null && Session["db"] != null)
{
AccountID = Session["login"].ToString();
DB = Session["db"].ToString();
Label4.Text = AccountID;
}
else
{
Response.Redirect("log.aspx");
}
}
}
protected void txtEndDate_TextChanged(object sender, EventArgs e)
{
String databs = Session["db"].ToString();
Response.Write(databs);
ddlist1_crtno a = new ddlist1_crtno();
a.filldropdown1(this.DropDownList1, databs);
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
// LOG OUT***********////////////
Session.Abandon();
Response.Redirect("log.aspx");
}
}
I put the same event on ButtonClick event and everything worked fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试从
TextBox
控件中删除ReadOnly="true"
。这可能会解决您的问题。具有
ReadOnly="true"
的TextBox
控件存在问题,每当发生 PostBack 时,文本框的值都会丢失,这就是事件不发生的原因射击。编辑:这是文本框的一个已知问题,目前还没有解决方案。显然有解决方法。
Try and remove the
ReadOnly="true"
from theTextBox
control. This might solve your problem.There is a problem with the
TextBox
controls havingReadOnly="true"
that the value of the textbox is lost whenever PostBack happens and that is the reason why the event isn't firing.Edit: It is a known issue with the textboxes which has no solution to it yet. there obviously are workarounds to it.