0

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.

Jason
  • 11,435
  • 24
  • 77
  • 131
  • This has been asked before, but because it's about non alpha numeric characters search is *even* harder. – ChrisF Jul 21 '10 at 16:25
  • 1
    see this question: http://stackoverflow.com/questions/160097/whats-the-difference-between-and – M4N Jul 21 '10 at 16:30
  • What are they called? I know, trying to google it is useless. – Jason Jul 21 '10 at 16:30

5 Answers5

3

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
  • 4,203
  • 1
  • 25
  • 26
  • This works. But I still don't understand why my code doesn't work. – Jason Jul 21 '10 at 16:51
  • 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 Jul 21 '10 at 17:07
1

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
  • 101,701
  • 37
  • 181
  • 258
  • 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. – Jason Jul 21 '10 at 16:59
0

'<%=fileInput1.ClientID%>'

Update:

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

MCain
  • 465
  • 2
  • 12
0

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
  • 101,701
  • 37
  • 181
  • 258
  • href="javascript:$('#<%= fileInput1.ClientID%>').uploadifyUpload()" is what gets rendered to the final page for the first one. – Jason Jul 21 '10 at 16:33
  • The second method renders this: href="<%=%20GetUploadString()%20%>" – Jason Jul 21 '10 at 16:36
-1

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
  • 14,942
  • 16
  • 80
  • 128
  • 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. 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. – Jason Jul 21 '10 at 16:47