简体   繁体   中英

Refresh javascript code, after autopostback dropdownlist

I have a form, and a dropdownlist for companies:

                          <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                 <ContentTemplate>
                                     <%--DataSourceID="sdsEmpresaTarea"--%>
                                     <asp:DropDownList ID="ddlEmpresaTarea" runat="server"
                                         DataValueField="IdEmpresa" DataTextField="Empresa" 
                                         AutoPostBack="true" ViewStateMode="Enabled">
                                     </asp:DropDownList>
                                 </ContentTemplate>
                          </asp:UpdatePanel>

a second dropdownlist shows the contacts for the selected company in the dropdownlist for companies:

<asp:DropDownList ID="ddlContactoTarea" runat="server"
   DataValueField="IdContacto" DataTextField="Nombre" ViewStateMode="Enabled">
</asp:DropDownList>

a datepicker textbox:

  <asp:TextBox ID="txtDate" runat="server" Text="" CssClass="datepicker" MaxLength="10" ViewStateMode="Enabled"/>
      <asp:RequiredFieldValidator ID="rfvDate" runat="server" 
          ErrorMessage="Date required"
          ControlToValidate="txtDate"  Display="Dynamic"> 
      </asp:RequiredFieldValidator>

The problem is when I expand the dropdownlist and I select an option, the dropdownlist execute the autopostback and in code I have a method to search the contacts for the company selected in the dropdownlist for companies, and then the datepicker doesn't work. I have to stress that when I charge the page, the datepicker works good, but then when I select a company in the dropdownlist, then datepicker doesn't work, and I think that this happened because the javascript code is not refresh after the autopostback.

How can I refresh the javascript code each time the dropdownlist for companies is selected?

Post your Javascript Code inside the Sys.Application.add_load

$(function(){
            Sys.Application.add_load(function () { 
           // your Java script Code here
                   });

            });

An UpdatePanel completely replaces the contents of the update panel on an update. This means that those events you subscribed to are no longer subscribed because there are new elements in that update panel.

function EndRequestHandler(sender, args) {
    try {
        // re-bind your javascript/jQuery events here
    } catch (e) {};
}
window.onload = Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

The PageRequestManager is a javascript object which is automatically available if an update panel is on the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM