Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using asp.net. my gridview have a dropdown with select2 script.when i press add button in gridview my select2 script not working. this happens after add update panel. Please help

Here code :

ASP.NET
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
     <ContentTemplate>
         <asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" Font-Names="Arial" ClientIDMode="Static" CssClass="table table-striped table-bordered table-hover" Width="100%" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" AllowPaging="False" ShowFooter="False" OnRowCommand="grdItems_RowCommand" DataKeyNames="RowId,ItemId">
             <Columns>
                 <asp:TemplateField HeaderText="No">
                     <ItemTemplate>
                         <asp:DropDownList ID="ddlNo" Font-Size="12px" runat="server" CssClass="js-example-placeholder-single" Width="100%" AutoPostBack="true" TabIndex="1" OnSelectedIndexChanged="ddlNo_SelectedIndexChanged"></asp:DropDownList>
                     </ItemTemplate>
                     <HeaderStyle Font-Bold="true" Font-Names="Tahoma" Font-Size="12px" Width="15%" BackColor="#8DC894"
                         ForeColor="Green" HorizontalAlign="Left" />
                     <ItemStyle HorizontalAlign="Left" />
                 </asp:TemplateField>
                 <asp:TemplateField HeaderText="Qty(Pcs)">
                     <ItemTemplate>
                         <asp:TextBox ID="txtQty" runat="server" AutoCompleteType="None" Text='<%# Bind("Qty") %>'
                             ClientIDMode="Static" CssClass="form-control text-right" onkeyup="Calculate();"
                             onkeypress="return isNumberKey(event)" TabIndex="2"></asp:TextBox>
                     </ItemTemplate>

                     <HeaderStyle Font-Bold="true" Font-Names="Tahoma" CssClass="text-right" Font-Size="12px" Width="5%" BackColor="#8DC894" ForeColor="Green" HorizontalAlign="Right" />
                     <ItemStyle HorizontalAlign="Right" />
                     <FooterTemplate>
                     </FooterTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField>
                     <ItemTemplate>
                         <asp:Button ID="btnAddNewRow" ToolTip="Add New Contract" CommandArgument="RowId"
                             CommandName="Add" CssClass="GridAddbtn" runat="server" TabIndex="3" />
                         <asp:Button ID="btngridDelete" ToolTip="Delete" CommandArgument="RowId" CommandName="Delete" data-target="#mpSearch"
                             CssClass="GridDeletebtn" runat="server" TabIndex="4" />
                     </ItemTemplate>
                     <HeaderStyle Font-Bold="true" Font-Names="Tahoma" Font-Size="12px" Width="5%" BackColor="#8DC894" ForeColor="Green" HorizontalAlign="Left" />
                     <ItemStyle HorizontalAlign="Right" />
                 </asp:TemplateField>
             </Columns>
             <AlternatingRowStyle BackColor="#C2D69B" />
             <HeaderStyle BackColor="Green"></HeaderStyle>
         </asp:GridView>
     </ContentTemplate>
 </asp:UpdatePanel>



this is javascript code :

JavaScript
<script type="text/javascript">
          $(".js-example-placeholder-single").select2({
              placeholder: "Select a state",
              allowClear: true
          });

      </script>
Posted
Updated 28-Jan-16 1:20am
v4
Comments
F-ES Sitecore 28-Jan-16 7:19am    
What is "select2" script? There is no mention of it in the code you posted.
amnk.info 28-Jan-16 7:21am    
I just added now javascript code.
you can see select2 script here
https://fk.github.io/select2-bootstrap-css/
F-ES Sitecore 28-Jan-16 9:26am    
The problem is that your jQuery code attaches events to elements that were on the page when the page first loads. However your updatepanel removes those elements and replaces them, and the new elements don't have your events attached to them.

As mentioned below if you use the pageLoad event to attach your events that is called not only when the page first loads but also every time the update panel fires.

http://forums.asp.net/t/1982846.aspx?How+to+run+javascript+after+updatepanel+execution+completed
amnk.info 28-Jan-16 9:44am    
thanks for reply. i will try this

1 solution

Try moving the script to a global function called pageLoad:
HTML
<script>
function pageLoad(){
    $(".js-example-placeholder-single").select2({
        placeholder: "Select a state",
        allowClear: true
    });
}
</script>

That will work if you only need a single startup script per page. If you need more than one, then try:
HTML
<script>
$(function(){
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(){
        $(".js-example-placeholder-single").select2({
            placeholder: "Select a state",
            allowClear: true
        });
    });
});
</script>
 
Share this answer
 
Comments
amnk.info 28-Jan-16 9:26am    
thanks for reply. I used your code.same problem again.not working. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900