【问题标题】:Problem in using AutoPostback in asp.net c#在asp.net c#中使用AutoPostback的问题
【发布时间】:2011-05-18 06:50:40
【问题描述】:

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

<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

当我将txtEndDateAutoPostBack 属性设置为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 事件上,一切正常。

【问题讨论】:

  • 这些代码是否包含在UpdatePanel 中?
  • @Piyush,非常抱歉,我听不懂你的意思。
  • &lt;asp:UpdatePanel&gt;&lt;/asp:UpdatePanel&gt;内的所有控件(TextBox和DropDownList)
  • 没有。它在里面
    ...ASP CONTROLS..>
  • 尝试从txtEndDate 控件中删除ReadOnly="true" 并查看结果。

标签: c# asp.net data-binding autopostback


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2010-12-14
    • 2010-10-18
    • 2016-04-17
    • 2023-04-04
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    相关资源
    最近更新 更多