Problem in using AutoPostback in asp.net c#

守給你的承諾、 提交于 2020-01-05 07:24:15

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6040905/problem-in-using-autopostback-in-asp-net-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!