locked
buttons in modal popup RRS feed

  • Question

  • User702049738 posted

    Hello experts;

    How do you get button ids in modal popup.

    thank you

    Wednesday, March 2, 2016 3:14 PM

All replies

  • User475983607 posted

    What kind of button?  A server control or HTML input?  Are you trying to get the ID on the server or client? 

    Where is your source code?

    Wednesday, March 2, 2016 3:50 PM
  • User702049738 posted

    Hi;

    It is a server control

    see code below

    protected void result ()
    {

    ModalPopupExtenderconfirmpasswordedit.Show();

    Button clickedButton = sender as Button;
    AjaxControlToolkit.ModalPopupExtender row = (AjaxControlToolkit.ModalPopupExtender)clickedButton.NamingContainer;

    string temp = row.ToString();

    If (temp == "btrnok")

    {

         label1.text = "Ok button was clicked on";

    }

    else 

    {

          label1.text = "another button was clicked on";

    }

    }

    this code doesn't work though

    Wednesday, March 2, 2016 4:20 PM
  • User-986267747 posted

    Hi olybobo,

    Button clickedButton = sender as Button;

    protected void result ()

    I don't find the parameter called sender in your parameters inside the result method.

    this code doesn't work though

    I couldn't understand your code, if you'd like to get the button's id in your modal popup. You should show the related information in the button click event. You could refer to the following code.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
    
        .modalBackground
    
        {
    
            background-color: Black;
    
            filter: alpha(opacity=90);
    
            opacity: 0.8;
    
        }
    
        .modalPopup
    
        {
    
            background-color: #FFFFFF;
    
            border-width: 3px;
    
            border-style: solid;
    
            border-color: black;
    
            padding-top: 10px;
    
            padding-left: 10px;
    
            width: 300px;
    
            height: 140px;
    
        }
    
    </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:Button ID="btnShow" runat="server" Text="Show Modal Popup" />
    
            
            <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" 
                PopupControlID="Panel1" TargetControlID="btnShow" BackgroundCssClass="modalBackground">
    
            </ajaxToolkit:ModalPopupExtender>
    
            <asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" align="center" style = "display:none">
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                     This is an ASP.Net AJAX ModalPopupExtender Example<br />
                     <asp:Button ID="btnOK" runat="server" Text="OK" OnClick="btnModalPop_Click" />
                     <asp:Button ID="btnClose" runat="server" Text="Close" OnClick="btnModalPop_Click" />
    
    </asp:Panel>
        
        
        </div>
        </form>
    </body>
    </html>
    
        public partial class ModalPopupSample : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnModalPop_Click(object sender, EventArgs e)
            {
                Button btn = sender as Button;
                ModalPopupExtender1.Show();
                if (btn.ID == "btnOK")
                {
                    Label1.Text = "Ok button was clicked on";
                }
                else
                {
                    Label1.Text = "another button was clicked on";
                }
    
            }
        }

    this code doesn't work though

    Because i'm not sure what you problem, Could you share the completed code with us and describe your problem in detailed?

    Best Regards,

    Klein zhang

    Thursday, March 3, 2016 6:36 AM
  • User702049738 posted

    aspx

           <%--this hiddentargetcontrol was created in this manner because a modal popup must have a target control
                                even it is not being used --%>
        
    
    <asp:Button ID = "btnupdatepasswordedit" runat = "server" Text = "Update" OnClick = "btnupdatepasswordedit_click" Visible = "true" />
                     <asp:Label ID="lblinfo" runat="server"></asp:Label>
                               
    
    <asp:Button ID="HiddenTargetControlForModalPopup" runat="server" Style="display: none" />
                     <ccl:ModalPopupExtender ID="ModalPopupExtenderconfirmpasswordedit" runat="server" PopupControlID="PNL"
            CancelControlID="btnmodalpasswordeditcancel" BackgroundCssClass="modalpopupbackground"
            TargetControlID="HiddenTargetControlForModalPopup">
        </ccl:ModalPopupExtender>
       
        <asp:Panel ID="PNL" runat="server" CssClass="modalPopup" Height="200px" Width="300px"
            Style="display: none">
            <div class = "Row">
            <div class = "Cell">
                Are you sure you want to update the password?      
            </div> 
        </div>
        <div class ="Row">
        <div class ="Cell">
        <asp:Button ID = "btnmodalpasswordedit" runat = "server" Text = "OK" />
        <asp:Button ID = "btnmodalpasswordeditcancel" runat = "server" Text = "Cancel" />
        </div>
        </div>
        </asp:Panel>
    
    
    aspx.cs
     protected void btnupdatepasswordedit_click(object sender, EventArgs e)
        {   
                    
                ModalPopupExtenderconfirmpasswordedit.Show();
                 //used to determine what button was clicked on
                Button clickedButton = sender as Button;
    
                AjaxControlToolkit.ModalPopupExtender temp = (AjaxControlToolkit.ModalPopupExtender)clickedButton.NamingContainer.FindControl("btnmodalpasswordedit");
               string temp = row.ToString();
              
               
               if (temp == "btnmodalpasswordedit")
               {
                   
                    
                  lblinfo.text = "Modalpopup ok button was clicked";
      
               }
               else
                {
    
                    lblinfo.text = "another button was clicked";
    
                 }
    
    
    
    }

    Thursday, March 3, 2016 8:13 PM
  • User2103319870 posted

    You can directly attach click event handler to buttons in Panel control and then show the message accordingly

    Panel Code

     <asp:Panel ID="PNL" runat="server" CssClass="modalPopup" Height="200px" Width="300px"
                Style="display: none">
                <div class="Row">
                    <div class="Cell">
                        Are you sure you want to update the password?      
                    </div>
                </div>
                <div class="Row">
                    <div class="Cell">
                        <asp:Button ID="btnmodalpasswordedit" runat="server" Text="OK" OnClick="btnmodalpasswordedit_Click" />
                        <asp:Button ID="btnmodalpasswordeditcancel" runat="server" Text="Cancel" OnClick="btnmodalpasswordeditcancel_Click" />
                    </div>
                </div>
            </asp:Panel>
    

    Remove the CancelControlID property from Modal PopupExtender

     <ccl:ModalPopupExtender ID="ModalPopupExtenderconfirmpasswordedit" runat="server" PopupControlID="PNL"
                BackgroundCssClass="modalpopupbackground"
                TargetControlID="HiddenTargetControlForModalPopup">
            </ccl:ModalPopupExtender>

    C#:

     /// <summary>
            /// Handles the Click event of the btnmodalpasswordedit control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void btnmodalpasswordedit_Click(object sender, EventArgs e)
            {
                lblinfo.Text = "Modalpopup ok button was clicked";
            }
    
            /// <summary>
            /// Handles the Click event of the btnmodalpasswordeditcancel control.
            /// </summary>
            /// <param name="sender">The source of the event.</param>
            /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
            protected void btnmodalpasswordeditcancel_Click(object sender, EventArgs e)
            {
                lblinfo.Text = "another button was clicked";
            }
    
            protected void btnupdatepasswordedit_click(object sender, EventArgs e)
            {
                ModalPopupExtenderconfirmpasswordedit.Show();
            }

    Thursday, March 3, 2016 8:44 PM
  • User702049738 posted

    that wouldn't work because technically the label will be replaced with an update statement like this below

    protected void btnupdatepasswordedit_click(object sender, EventArgs e)
        {   
                    
                ModalPopupExtenderconfirmpasswordedit.Show();
                 //used to determine what button was clicked on
                Button clickedButton = sender as Button;
    
                AjaxControlToolkit.ModalPopupExtender temp = (AjaxControlToolkit.ModalPopupExtender)clickedButton.NamingContainer.FindControl("btnmodalpasswordedit");
               string temp = row.ToString();
              
               
               if (temp == "btnmodalpasswordedit")
               {
                   
                    
                  update table1
                  set place = "zone"
                  where id = 1
      
               }
               else
                {
    
                    lblinfo.text = "the update was canceled";
    
                 }
    
    
    
    }



    Thursday, March 3, 2016 8:56 PM
  • User-986267747 posted

    Hi olybobo,

    TargetControlID="HiddenTargetControlForModalPopup">

    AjaxControlToolkit.ModalPopupExtender temp = (AjaxControlToolkit.ModalPopupExtender)clickedButton.NamingContainer.FindControl("btnmodalpasswordedit"); string temp = row.ToString();

    <asp:Button ID="HiddenTargetControlForModalPopup" runat="server" Style="display: none" />

    If we use the above code to create a sample , we will get some problems. What is the parent control for the button? You could debug your code and check what you get when you use the above code. The control called btnmodalpasswordedit only is a button.

    Besides, your code is confusing, i suggest that you need to be clear about your logic and learn how to use ModalPopupExtender, you could look the previous replies again to get the use method of the ModalPopupExtender control.

    Best Regards,

    Klein zhang

    Monday, March 7, 2016 7:50 AM
  • User702049738 posted

    Hi Klein;

    I just sent you a sample code showing what I am trying to do. I know how to use Modalextender popup, I am only asking a question in regards to identifying which button was clicked in modal popup for learning purposes. there is a work-around solution which is to use the update button to launch the modalpopup and use onclick events to identify which button was clicked.

    Tuesday, March 8, 2016 2:18 AM