How to call TextChanged of TextBox WebForm Control

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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){...}

Solution 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.

Solution 3

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

Solution 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.

Solution 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/

Solution 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.

enter image description here

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply