简体   繁体   中英

TextChanged event not firing for text box, when the text is changed from javascript

I have a text box which im filling of date from the calendar extender and the code is as below:-

<label for="input-one" class="float"><strong>Date</strong></label><br />                  
<asp:TextBox ID="txtDate" runat="server" CssClass="inp-text" Enabled="false" AutoPostBack="true"
             Width="300px" ontextchanged="txtDate_TextChanged"></asp:TextBox>
<asp:ImageButton ID="btnDate2" runat="server" AlternateText="cal2" 
                 ImageUrl="~/App_Themes/Images/icon_calendar.jpg" style="margin-top:auto;" 
                 CausesValidation="false" onclick="btnDate2_Click" />
<ajaxToolkit:CalendarExtender ID="calExtender2" runat="server" 
                              Format="dddd, MMMM dd, yyyy" 
                              OnClientDateSelectionChanged="CheckDateEalier" 
                              PopupButtonID="btnDate2" TargetControlID="txtDate" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                            ControlToValidate="txtDate" ErrorMessage="Select a Date" Font-Bold="True" 
                            Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator><br />

Javascript code is :-

function CheckDateEalier(sender, args) 
{
    sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}

My requirement is that as the date is entered in to the textbox, I want to call this method:

public void TimeSpentDisplay()
{
        string date = txtDate.Text.ToString();
        DateTime dateparsed = DateTime.ParseExact(date, "dddd, MMMM dd, yyyy", null);
        DateTime currentDate = System.DateTime.Now;
        if (dateparsed.Date > currentDate.Date)
        {
            divtimeSpent.Visible = true;
        }
        if (dateparsed.Date < currentDate.Date)
        {
            divtimeSpent.Visible = true;
        }
        if (dateparsed.Date == currentDate.Date)
        {
            divtimeSpent.Visible = false;
        }
}

Please help me that how i achieve this as im calling this method inside txtDate_TextChanged method but the event is not firing as the text is changed inside the textbox.

Please suggest how I can achieve this or give me an alternate idea to fulfill my requirement.

you have to change the AutoPostBack property of the textbox to true.

Hope this may helps you.

我已经修复了该问题,因为我的文本框位于ajax更新面板中,这就是事件未触发的原因。我在该文本框的更新面板中添加了一个触发器,并且效果很好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM