Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link OnClick code behind does not execute when set as ModalPopupExtender TargetControlID

I did a lot of searching and cannot figure this out.

I have a ModalPopupExtender pop-up that I want to display when the user clicks a link DoSomething. The pop-up has a dropdown control that I then want to populate on the fly when the user asks to open the dialogue. This needs to happen server side via the code behind. Currently I am trying to do it via an OnClick event on the link but as soon as the link is tied to the ModalPopupExtender the link OnClick code is not executed.

Code snippet:

<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server" BackgroundCssClass="modalBackground"
    DropShadow="true" PopupControlID="lnkDoSomething"
    PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething"   
    TargetControlID="lnklnkDoSomething"></asp:ModalPopupExtender>

The problem is as soon as I set the ModalPopupExtender to the link the OnClick code does not execute. This obviously is by design but it doesn't make sense to me (naive) as if the user clicks the link the OnClick code should be executed.

Any ideas why this is not supported and what the correct solution is?

like image 723
Michael Hollywood Avatar asked Feb 21 '23 18:02

Michael Hollywood


1 Answers

Attach the ModalPopupExtender to a dummy button and show the modal on the LinkButton's OnClick even from the code-behind:

Markup:

<asp:LinkButton ID="lnkDoSomething" runat="server" onClick="lnkDoSomething_Click">Do Something</asp:LinkButton>
<asp:Button id="dummyButton" runat="server" style="display:none;" />

<asp:ModalPopupExtender ID="mpelnklnkDoSomething" runat="server" 
  BackgroundCssClass="modalBackground" DropShadow="true" PopupControlID="controlToPopUpId"
  PopupDragHandleControlID="pnlDragHandlerForlnkDoSomething" 
  TargetControlID="dummyButton"></asp:ModalPopupExtender>

Code-behind:

  protected void lnkDoSomething_Click(Object sender, EventArgs e) 
  {
     //do work
     mpelnklnkDoSomething.Show();
  }
like image 197
rick schott Avatar answered Apr 26 '23 23:04

rick schott