How can I show modal windows in an ASP.NET WebForms application? I need to display a modal window with 2 buttons (OK/Cancel) and retrieve which button is pressed in my code. I'm also using jQuery and Bootstrap for this project, if that affects my options.
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
Step 1 : Start a new ASP.NET MVC Web application. Add a new controller called Home and in the Home controller, the Index method is available to you but there is not a view with it so add a view by right-clicking in Index action. Step 2 : In index. aspx add one HTML button to open our modal popup window like below.
Open (Show) Bootstrap Modal Popup Window using jQuery The following JavaScript function will be called from Server Side (Code Behind) using RegisterStartupScript method of ClientScript class when the ASP.Net Button is clicked.
Hey check this code using modalpopupextender
. But first, you'll need to install AjaxControlToolKit from the Nuget Package Manager and add it as an assembly reference at the top of your .aspx page as a directive, like this:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
Then here is the code from the modalpopupextender
:
<asp:Button ID="btnOpenPopUp" runat="server" text="Open PopUp" />
<asp:Label ID="lblHidden" runat="server" Text=""></asp:Label>
<ajaxToolkit:ModalPopupExtender ID="mpePopUp" runat="server" TargetControlID="lblHidden" PopupControlID="divPopUp" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>
<div id="divPopUp" class="pnlBackGround">
<div id="Header" class="header" >MyHeader</div>
<div id="main" class="main">Main PopUp </div>
<div id="buttons">
<div id="DivbtnOK" class="buttonOK"><asp:Button id="btnOk" runat="server" text="Ok" /></div>
<div id="Divbtncancel" class="buttonOK"><asp:Button id="btnCancel" runat="server" text="Cancel" /></div>
</div>
</div>
then from Code behind On Click event of the button Open PopUp :
protected void btnOpenPopUp_Click(object sender, ImageClickEventArgs e)
{
mpePopUp.Show();
}
then on click of Ok Button :
protected void btnOk_Click(object sender, ImageClickEventArgs e) {
//Do Work
mpePopUp.Hide(); }
On Cancel click button :
protected void btnCancel_Click(object sender, ImageClickEventArgs e)
{
//Do Work
mpePopUp.Hide();
}
Tip: If you don't have the ajax toolkit it can be installed with Nuget.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With