November 8, 2016

Common model popup for all the pages

we can use common model popup for all pages

Master Page Code snippet

 <cc1:ModalPopupExtender ID="mppopup" runat="server" BehaviorID="modalpopup" BackgroundCssClass="backgroundmodal"
            TargetControlID="btnpopup" CancelControlID="btnCancel" OkControlID="btnOkay"
            PopupControlID="panelmodal">
        </cc1:ModalPopupExtender>
        <asp:Button runat="server" ID="btnpopup" OnClick="btnpopup_Click" Text="PopUp" CssClass="hide" /><!--dummy button-->
        <asp:Panel ID="panelmodal" Style="display: none" CssClass="modal-dialog" runat="server">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" runat="server" id="myModalheader">Alert</h4>
                </div>
                <div class="modal-body">
                    <div class="alert-content">
                        <p runat="server" id="palertmessage">Hello, this is an alert</p>
                    </div>
                </div>
                <div class="modal-footer">
                    <asp:Button ID="btnOkay" runat="server" Text="Ok" CssClass="btn-small" UseSubmitBehavior="true" OnClientClick="javascript:skipLN();" />
                    <asp:Button ID="btnCancel" runat="server" Text="Can" CssClass="btn-small" UseSubmitBehavior="true" OnClientClick="javascript:skipLN();" />
                </div>
            </div>
        </asp:Panel>


class - find the control from master page and did the dynamic activity

public void CallModalPopup(MasterPage master, string RedirectPageName, string AlertBody, string AlertHeader = "Notification", string OKtext = "OK", string CanText = "CANCEL", bool ShowOk = true, bool showCancel = true, bool skipLN = true)
        {

            AjaxControlToolkit.ModalPopupExtender mp = (AjaxControlToolkit.ModalPopupExtender)master.FindControl("mppopup");

            System.Web.UI.HtmlControls.HtmlGenericControl body = (System.Web.UI.HtmlControls.HtmlGenericControl)master.FindControl("palertmessage");

            System.Web.UI.HtmlControls.HtmlGenericControl header = (System.Web.UI.HtmlControls.HtmlGenericControl)master.FindControl("myModalheader");

            Button okay = (Button)master.FindControl("panelmodal").FindControl("btnOkay");
            Button cancel = (Button)master.FindControl("panelmodal").FindControl("btnCancel");

            okay.OnClientClick = "";
            if (RedirectPageName != "")
                if (RedirectPageName.Contains("window.open"))
                    okay.OnClientClick = RedirectPageName;
                else if (skipLN)
                    okay.OnClientClick = "javascript:skipLN();window.location.href='" + RedirectPageName + "'";
                else
                {
                    okay.OnClientClick = "window.location.href='" + RedirectPageName + "'";
                    HttpContext.Current.Session["UserID"] = "ERROR";
                }
            okay.UseSubmitBehavior = true;
            okay.Text = OKtext;
            okay.Focus();
            cancel.Text = CanText;
   
            if (!showCancel)
                cancel.Attributes.Add("style", "display:none !important;");
            body.InnerText = AlertBody;
            header.InnerText = AlertHeader;

            mp.Show();
   
        }

call from Page

CallModalPopup(this.Master, "", "You can register interest for upto 3 interest areas and job roles", showCancel: false);

No comments: