在asp.net c#中使用AutoPostback的问题

发布于 2024-11-08 07:22:43 字数 2352 浏览 0 评论 0原文

我有两个带有日历扩展器的文本框控件和一个下拉列表,如下所示。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-11-15 07:22:44

尝试从 TextBox 控件中删除 ReadOnly="true"。这可能会解决您的问题。

具有 ReadOnly="true"TextBox 控件存在问题,每当发生 PostBack 时,文本框的值都会丢失,这就是事件不发生的原因射击。

编辑:这是文本框的一个已知问题,目前还没有解决方案。显然有解决方法。

Try and remove the ReadOnly="true" from the TextBox control. This might solve your problem.

There is a problem with the TextBox controls having ReadOnly="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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文