7

How can I set TragetContriID to a HyperLink that is inside a GridView?

I tried this :

<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
                        PopupControlID="Panel1" 
                        CancelControlID="btnCancel" 
                        OnCancelScript="HideModalPopup()"
                        TargetControlID="GridView1$HyperLink1">
</asp:ModalPopupExtender>

But I have an error: that there is no GridView1$HyperLink1

DavRob60
  • 3,517
  • 7
  • 34
  • 56
Fatemeh M
  • 83
  • 1
  • 1
  • 3

1 Answers1

8

Setting the TargetControlID of the ModalPopupExtender basically trigger the client side Show function of that ModalPopup when the control is clicked. So you need to wire up the controls yourself.

First, since the ModalPopupExtender need a TargetControlID, you should add a dummy control to link the modal popup to :

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

And link the ModalPopupExtender TargetControlID to it

<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
                        PopupControlID="Panel1" 
                        CancelControlID="btnCancel" 
                        OnCancelScript="HideModalPopup()"  
                        TargetControlID="HiddenTargetControlForModalPopup">
</asp:ModalPopupExtender>

So the ModalPopupExtender now has a target that do nothing. Now we now need to do the target's job. You need a javascript function to show the ModalPopup from client side.

<script type="text/javascript">
     var ModalPopup='<%= ModalPopupExtender1.ClientID %>';

     function ShowModalPopup() {
         //  show the Popup     
         $find(ModalPopup).show();
     }
</script>   

Then you should map the OnClientClick event of the control in your gridview to this javascript function. From your code, I see that you use a asp:HyperLink, I don't think it support the OnClientClick event, so you probably need to switch it to a asp:LinkButton.

<asp:LinkButton ID="LinkButton1" runat="server" 
                OnClientClick="ShowModalPopup()" />
DavRob60
  • 3,517
  • 7
  • 34
  • 56
  • Hi I am using your code, but I am getting an error.. Microsoft JScript runtime error: Sys.ArgumentNullException: Value cannot be null. Parameter name: handler – software Apr 15 '13 at 14:48
  • works fine for me, well tested solution. Your error might be somewhere else. Try to debug it to see where the error occurs. – DavRob60 Apr 15 '13 at 14:55
  • some how got the error fixed. but It is not triggering Pop up(Message Box). – software Apr 15 '13 at 17:27
  • 1
    @software Well, if that helped you, an up-vote on both the question and this answer would be appreciated. – DavRob60 Apr 15 '13 at 18:34
  • This is great, I just seem to get a postback though when I click the new button so the popup works but then page gets reloaded... AutoPostBack property doesn't seem to be available, any thoughts? Thanks! – ebooyens Jul 14 '16 at 15:05
  • @ebooyens If you get a postback when you click the button, you did something wrong. You could try to test it in an isolated page. – DavRob60 Jul 14 '16 at 21:34
  • @DavRob60 I think it's being caused by the Command Buttons in my Gridview, need to retain these so still trying to figure out if I can make another plan – ebooyens Jul 15 '16 at 09:12
  • 1
    @DavRob60 It seems placing the Gridview inside an UpdatePanel but keeping the pop-up outside of it does the trick – ebooyens Jul 15 '16 at 09:19