How to call TextChanged of TextBox WebForm Control

孤人 提交于 2019-12-25 04:26:45

问题


I use TextBox WebForm Control configured like a Bootstrap Datetimepicker. And I need to initiate server side TextChanged event. The code I have does not work so it does not call server side part.

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 :(        
}

I try to force this event like this but no joy.

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

Any clue how to fix it?


回答1:


After searching a lot on the Internet I found this solution that worked to me:

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

And on the code-behind:

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

You can even pass some arguments in the like:

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

and

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



回答2:


In order for the TextChanged event to fire when you leave the textbox, you have to set the TextBox control's AutoPostBack property to 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>

You also need to make sure that the name of the event handler for OnTextChanged matches the name of the method in your code (maybe this was just a typo).

Finally, I've found the TextChanged event to be a bit finicky and cause unwanted postbacks, page scrolling, loss of focus, etc., so you may want to consider using a client-side solution (eg. JQuery) instead.




回答3:


name of method not correct, add autopostback= 'true' to textbox




回答4:


Change this line to

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

and it will be ok.




回答5:


Solution is to add ApiController and consume via JQuery what I need.

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




回答6:


By default ASP.Net page caches all the server control changed events and executes after Postback event. So override this default by setting Autopostback property of a control to true like below.



来源:https://stackoverflow.com/questions/38745150/how-to-call-textchanged-of-textbox-webform-control

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