【问题标题】:TextChanged event not firing for text box, when the text is changed from javascript当文本从 javascript 更改时,文本框的 TextChanged 事件未触发
【发布时间】:2012-06-25 19:14:13
【问题描述】:

我有一个文本框,我从日历扩展器中填写日期,代码如下:-

<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 代码是:-

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

我的要求是当日期输入到文本框中时,我想调用这个方法:

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;
        }
}

请帮助我如何实现这一点,因为我在 txtDate_TextChanged 方法中调用此方法,但由于文本框内的文本发生更改,事件未触发。

请建议我如何实现这一目标或给我一个替代想法来满足我的要求。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我已修复它,因为文本框位于 ajax 更新面板内,这就是事件未触发的原因。我在该文本框的更新面板中添加了一个触发器,它工作正常。

    【讨论】:

    • 请添加您尝试过的内容。会更有帮助。
    【解决方案2】:

    您必须将文本框的 AutoPostBack 属性更改为 true。

    希望对你有所帮助。

    【讨论】:

    • 我这样做了,但 txtDate_TextChanged 事件仍然没有触发。您还可以在代码中看到它设置为 true。
    • 请通过在 TEXTBOX 中简单地输入文本来检查这是您的活动作品
    • 不,它不起作用,我启用了文本框并手动更改了文本框文本,但事件没有触发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多