How to call modal box from server side onSelectedIndexChanged event of dropdown?

Durgesh Pawar 20 Reputation points
2023-07-31T06:35:46.2266667+00:00

I am not able to show bootstrap modal box from server side onselectedindexChanged event in dotnet webforms. What Should I do?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,330 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 26,761 Reputation points Microsoft Vendor
    2023-08-01T01:46:18.16+00:00

    Hi @Durgesh Pawar,

    Have you tried that code and it still doesn't work?

    Whether the drop-down list is inside or outside the modal box, it does not affect the use of the RegisterStartupScript method of the ClientScript class.

    I tested your code and it works.

      protected void CompanyNameDDL_SelectedIndexChanged(object sender, EventArgs e)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "$('#NewVehicleCompany').modal('show')", true);
            }
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DropDownList ID="CompanyNameDDL" runat="server" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="CompanyNameDDL_SelectedIndexChanged" data-toggle="dropdown" data-target="#NewVehicleCompany">
            <asp:ListItem Text="Select Fruit" Value="0" />
                                <asp:ListItem Text="Mango" Value="1" />
                                <asp:ListItem Text="Apple" Value="2" />
                                <asp:ListItem Text="Banana" Value="3" />
                </asp:DropDownList>
          <%--  modal box--%>
       <div id="NewVehicleCompany" class="modal fade">
           <div class="modal-dialog">
               <div class="modal-content">
                   <div class="modal-header">
                       <h4 class="modal-title">Modal Box</h4>
                       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                   </div>
                   <div class="modal-body">
                       <!-- Add the content of the modal box here -->
                       This is the modal box content.                                                     </div>
                   <div class="modal-footer">
                       <asp:Button ID="btnCloseModal" runat="server" Text="Close" CssClass="btn btn-danger" data-dismiss="modal" />
                   </div>
               </div>
           </div>
       </div>
        </form>
    </body>
    </html>
    
    

    enter image description here

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful