views:

3207

answers:

6

This HyperLink syntax is not working to pass parameters to a small PopUp window:

<asp:HyperLink ID="HyperLink2" runat="server"  Text="Manage Related Items"  NavigateUrl='<%# "editRelatedItems.aspx?" + "ProductSID=" + Eval("ProductSID") + "&CollectionTypeID=" + Eval("CollectionTypeID")+ "&ProductTypeID=" + Eval("ProductTypeID") %>'  onclick="window.open('editRelatedItems.aspx?','name','height=550, width=790,toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no'); return false;) Target="_blank" />

Looks like the tag does not take the "onclick". Any ideas on how to get a popUp to fire that can get these Parameters? I'm using C#, so perhaps there is a way to build the NavigateURL string in the code behind?

Thanks for any insight you may have.

+2  A: 

From the server side code you can do

HyperLink2.Attributes.Add("OnClick", "yourstuffhere");

that will allow you to specify the OnClick items.

You could also set the NavigateUrl to be something like "javascript: window.open..." if you wanted, again, can be done from the code behind.

also

Given that a Hyperlink is just a standard run of the mill anchor tag, if you are doing all of this processing in the code behind, it might just be easier to build the whole link yourself to save some hassle.

Mitchel Sellers
Doug
A: 

The NavigateUrl overrides the onclick event of an 'a' tag.

You want to set the target attribute and drop the onclick

http://www.w3schools.com/tags/tag_a.asp

Chris Brandsma
+1  A: 

Do you need the server-side Hyperlink control? If not, I would recommend just using a standard HTML anchor tag or if you need it to show dynamically, just to generate it in the back-end using a literal tag and write the markup to that(including the onclick). The question you need to ask yourself is "Do you feel lucky?" and "Do I need a server control?"

If not familar with it, research a bit about the ItemDataBound event.

Key code to place into your DataList's ItemDataBound event:

Dim lit as Literal = CType(e.Item.FindControl("Literal1"), Literal)

lit.Text = "<a onclick='YOURJAVASCRIPT' href='#'>YOUR TEXT</a>"

This is by no means the entire code you should put in your event, like I said, research what the event can offer and go from there.

Dan Appleyard
Dan, sorry I didn't mention this fact before: The HyperLink is inside an <ItemTemplate> of a DataList - so the Hyperlink will be picking up different product attributes for each product. As far as using a Literal instead, I'm fuzzy on your suggestion - just substitute the HyperLink with Literal and make the Literal1.text = "someThingHereComingFromCodeBehind" ?
Doug
A: 

Use OnClientClick instead of OnClick.

orip
A: 

Your usage of onclick in your code is missing it's closing quote " and has an extra bracket ). That could be your problem.

onclick="window.open('editRelatedItems.aspx?','name','height=550, width=790,toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no'); return false;"

I've found that the javascript popups don't play nicely with ASP:HyperLink; Try to create the link via a normal anchor tag:

<a id="HyperLink2" 
     runat="server" 
     NavigateUrl='<%# "editRelatedItems.aspx?ProductSID=" + Eval("ProductSID") + "&CollectionTypeID=" + Eval("CollectionTypeID")+ "&ProductTypeID=" + Eval("ProductTypeID") %>'  
     onclick="window.open('editRelatedItems.aspx?','name','height=550, width=790,toolbar=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no'); return false;" 
     Target="_blank">
Manage Related Items
</a>
Gavin Miller
A: 
<asp:HyperLink 
     ID="HyperLink2" 
     runat="server" 
     Text="Manage Related Items"
     NavigateUrl="#"
     onClick='<%# "window.open('editRelatedItems.aspx" + 
                  "?ProductSID=" + Eval("ProductSID") + 
                  "&CollectionTypeID=" + Eval("CollectionTypeID")+ 
                  "&ProductTypeID=" + Eval("ProductTypeID") + 
                  ",'name','height=550, width=790,toolbar=no,
                  directories=no,status=no,
                  menubar=no,scrollbars=yes,resizable=no'); 
                  return false;%>'  
     Target="_blank" />

That should work, but I would really create a javascript method to call which would open the window.

<script type="text/javascript">
   function openRelatedItems(productSID, collectionTypeId, productTypeId) { 

       window.open('editRelatedItems.aspx" + 
                  "?ProductSID=" + productSID + 
                  "&CollectionTypeID=" + collectionTypeID + 
                  "&ProductTypeID=" + productTypeID + 
                  ",'name','height=550, width=790,toolbar=no,
                  directories=no,status=no,
                  menubar=no,scrollbars=yes,resizable=no'); 

   }
</script>
Bob
Thanks Bob - and to everyone else that answered. This code does work, but as others have mentioned, asp:HyperLink control with javascript has proven to be less than an elegant solution. I'm the code from this suggetion for now, becuase the requirements are simple. Regards, Doug
Doug