views:

109

answers:

5

You can see what I am trying to do below. Two ways I have tried to do the same thing, but neither of these work. What is the fundamental here that I don't get?

    <asp:HyperLink ID="HyperLink1" runat="server" 
     NavigateUrl="javascript:$('#<%= fileInput1.ClientID%>').uploadifyUpload()">
    </asp:HyperLink>

OR

    <asp:HyperLink ID="HyperLink1" runat="server" 
          NavigateUrl='<%= GetJavascriptString()%>'>
    </asp:HyperLink>

public void GetJavascriptString()
{
     return "javascript:$('#" + fileInput1.ClientID + "').uploadifyUpload();";
}

There have been several answers that work and don't work. I think that James Curran has what I was looking for though. The reason, although I'm not sure why my code doesn't work AND a fix for it. Thanks for all your answers.

A: 

'<%=fileInput1.ClientID%>'

Update:

NavigateUrl="javascript:'<%=fileInput1.ClientID%>'.uploadifyUpload()">

MCain
Thats nice. But how do I get the js around it?
Blankasaurus
check this update
MCain
@MCain: but the "$('#" part is important (he's trying to call jQuery on it)
James Curran
This is basically what I was doing. And it wasn't executing the script tags.
Blankasaurus
A: 

As far as I can tell, both should do what you want. (Assuming that GetJavascriptString() is actually in the codebehind file or at least in a <% %> block.)

What error are you getting?

James Curran
href="javascript:$('#<%= fileInput1.ClientID%>').uploadifyUpload()" is what gets rendered to the final page for the first one.
Blankasaurus
The second method renders this: href="<%=%20GetUploadString()%20%>"
Blankasaurus
+2  A: 

Here is a pretty good run down of different script tags.

http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx

(Found by searching on Google for asp.net script tags)

Update:

One way to accomplish what you are trying to do is:

<script type="text/javascript">
    function GetJavascriptString() {
        return $('#<%= fileInput1.ClientID %>').uploadifyUpload();
    }
</script>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="javascript:GetJavascriptString()" />

This simply calls a javascript function from your hyperlink that will run the jquery that you are trying to do.

Another, and arguably better way, to accomplish this would be to use codebehind or inline script to set the navigate url property:

<%  HyperLink2.NavigateUrl = "javascript:$('#" + fileInput1.ClientID + "').uploadifyUpload();";
%>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="javascript:GetJavascriptString()" />
sgriffinusa
This works. But I still don't understand why my code doesn't work.
Blankasaurus
I found a post that describes pretty well why you ran into errors with your examples. http://forums.asp.net/t/1002543.aspx, specifically the comment here http://forums.asp.net/t/1002543.aspx#1324224.
sgriffinusa
A: 

I think you can accomplish what you're looking for like this :

 <asp:HyperLink ID="HyperLink1" runat="server" 
     NavigateUrl="myURL" 
     OnClientClick="javascript: if(document.getElementById('ctl00_myHTMLelementID').value.length > 0 ){ $get('ctl00_myHTMLelementID').uploadifyUpload()" >
 </asp:HyperLink>

We use the OnClientClick (it fires BEFORE anything else, so if 'False' is returned, it does not proceed with any NaviateURLs or OnClick) and first make sure the element exists so that we don't encounter any javascript errors that freeze up the page. 'ctl00_myHTMLelementID' is obtained by looking at the source of your code in your browser and finding what ASP.NET named the control in your HTML.

.

.

. As per # and % ...

ASP.NET comments look much like HTML comments ( <!-- comment --> )

 <%-- <asp:TextBox ID="uxTextBox1" runat="server" Text="Howdy!" /> --%>]

ASP.NET has special data-binding code, it looks like this, and it can only be used inside of a Repeater or a GridView

  <asp:Label ID="uxActiveLbl" runat="server" Text=’<%# DataBinder.Eval(Container.DataItem, "ClientActive").ToString() == "1" ? "Yes" : "No"%>’ />

And, you can run ASP.NET code inside of HTML by simply starting the code block, like this:

    <% String myvariable="foobar" %>
rlb.usa
I can just hardcode it in there if I look at the source and find the control id. Thats what i was doing, but as the page is changed by other people, the controlID changes and the page breaks. <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="javascript:$('#ctrl001_whatever').uploadifyUpload()"> works just fine, as long as the control name never changes. Thats why I want to get fileInput1.ClientID instead of just looking up the rendered control name in the source.
Blankasaurus
+1  A: 

I think the problem is that ASP.NET doesn't like inserting text via <%= %> into server controls.

However, why bother with a server control for something as simple as a hyperlink? What you really want is just:

<a id="HyperLink1" 
     href="javascript:$('#<%=fileInput1.ClientID%>').uploadifyUpload()"> 
</a>

Try that.

James Curran
I think this is the answer I am looking for. That seems to be the issue. It doesn't want to easily insert stuff via script tags into a server control. Works btw.
Blankasaurus