Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker automatically causing a postback in ASP.NET #1728

Open
hvjackson opened this issue Dec 21, 2015 · 6 comments
Open

Datepicker automatically causing a postback in ASP.NET #1728

hvjackson opened this issue Dec 21, 2015 · 6 comments

Comments

@hvjackson
Copy link

I use Datepicker with an ASP.NET Web Forms site. Version 1.4.1 worked fine for me but as of 1.5.0 I am noticing the following bug:

If an ASP.NET TextBox with the setting "AutoPostBack=true" is used as the base element for a Datepicker, the datepicker code automatically triggers a page postback, which causes the page to reload, which causes the postback to be triggered, etc. In other words, the page keeps refreshing indefinitely.

It is not easy to make a sample snippet of ASP.NET code but here is how I am creating a textbox:

        <asp:TextBox runat="server" ID="rangeStartBox" CssClass="input-small datepicker" AutoPostBack="true"/>

Normally that should trigger a POST only when the textbox is actually changed, but starting in 1.5.0 a POST is triggered every time the Datepicker is initialized.

@MrBenjaminRay
Copy link

MrBenjaminRay commented Apr 16, 2016

I have been having the same issue. Setting AutoPostBack="false" prevents the unintentional postback, but you obviously lose AutoPostBack functionality.

When an instance of the plugin is created on a field, the plugin's constructor Datepicker() calls update(), which in turn triggers the jQuery change event on the input element by calling this.element.change(). This tells ASP.Net that the value has changed, triggering an ASP.Net PostBack.

I have worked around this issue as follows:

1) Update bootstrap-datepicker.js by changing line 826 (v1.6.0) from this:

    this.element.change();

to this:

    if (typeof preventDatePickerChange == 'undefined' || !preventDatePickerChange) {
        // Prevent change event if we're just initializing the plugin on a field
        this.element.change();
    }

2) Create and set the variable preventDatePickerChange to true before initializing the plugin on the field, then set it to false after. I do this everywhere I use the datepicker plugin. In an ASP.Net environment, this would have to be run every time the field is posted back.

    var preventDatePickerChange = true; // Initialize & set the variable to block change event
    $('some-selector').datepicker({
        // some options
    });
    preventDatePickerChange = false; // Set variable to allow all future change events

After making these changes, I no longer have any unwanted PostBacks, and I have yet to find any negative side-effects.

@AlucardKun
Copy link

Hi Guys,

I have the same problem in version 1.6.4.

To temporaly solve my problem, i commented the line 802 in bootstrap-datepicker.js:

from this:

this.fill();
this.element.change();
return this;

to this:

this.fill();
//this.element.change();
return this;

I did not see any negative impact on the page.

@Azaret
Copy link
Contributor

Azaret commented Oct 26, 2016

Hi,
I think the main issue in this case if that the update event is triggered at startup, but should not, am I right ?

@MrBenjaminRay
Copy link

You are correct.

@MrBenjaminRay
Copy link

This is fixed in the upcoming version 1.7.0.

See the commit here: 110b120

@shivakadakol
Copy link

this.fill();
//this.element.change();
return this;

work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants