如何调用TextBox WebForm控件的TextChanged

问题描述 投票:0回答:6

我使用TextBox WebForm Control配置为Bootstrap Datetimepicker。我需要启动服务器端TextChanged事件。我的代码不起作用,因此它不会调用服务器端部分。

HTML

<div class='datepicker input-group date' id='datetimepickerStart'>
                                    <asp:TextBox ID="StartDate" class="form-control dateField" placeholder="Fecha" required runat="server" OnTextChanged="StartDate_TextChanged"></asp:TextBox>
     <span class="input-group-addon">
     <span class="glyphicon glyphicon-calendar"></span>
</span>
</div>

C#

protected void StartDate_TextChanged(object sender, EventArgs e)
{
    // It does not fire :(        
}

我试图像这样强迫这个事件,但没有快乐。

JS

$('#datetimepickerStart').datetimepicker();

$('#datetimepickerStart').datetimepicker().on('dp.change', function (event) {
       console.log(event.date);
      $('#StartDate').change(); // It does not help
      $('#StartDate').html(event.date); // It does not help             
});

有任何线索如何修复它?

c# asp.net webforms bootstrap-datetimepicker textchanged
6个回答
1
投票

在互联网上搜索了很多,我发现这个解决方案对我有用:

$('#datetimepickerStart').datetimepicker().on('dp.change', function (event) {
    __doPostBack('<%= Page.ClientID %>');
});

在代码隐藏上:

public void RaisePostBackEvent()
{
    // Do whatever, maybe call the OnTextChange method.
}

你甚至可以传递一些参数:

__doPostBack('<%= Page.ClientID %>', argument);

public void RaisePostBackEvent(string Arg){...}

0
投票

为了在离开文本框时触发TextChanged事件,您必须将TextBox控件的AutoPostBack属性设置为true。

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.autopostback(v=vs.110).aspx

<asp:TextBox ID="StartDate" class="form-control dateField" autopostback="true" placeholder="Fecha" required runat="server" OnTextChanged="StartDate_TextChanged"></asp:TextBox>

您还需要确保OnTextChanged的事件处理程序的名称与代码中方法的名称相匹配(可能这只是一个错字)。

最后,我发现TextChanged事件有点挑剔并导致不必要的回发,页面滚动,失去焦点等,因此您可能需要考虑使用客户端解决方案(例如JQuery)。


0
投票

方法名称不正确,将autopostback= 'true'添加到文本框中


0
投票

将此行更改为

<asp:TextBox ID="StartDate" class="form-control dateField" placeholder="Fecha" AutoPostBack="True" required runat="server" OnTextChanged="EndDate_TextChanged"></asp:TextBox>

它会好的。


0
投票

解决方案是添加ApiController并通过JQuery消费我需要的东西。

https://blogs.msdn.microsoft.com/henrikn/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms/


0
投票

默认情况下,ASP.Net页面缓存所有服务器控件更改的事件,并在Postback事件后执行。因此,通过将控件的Autopostback属性设置为true来覆盖此默认值,如下所示。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.