开发者

Simple ASP.Net Question <%# and <%= not working!

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.


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()" />


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.


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?


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" %>


'<%=fileInput1.ClientID%>'

Update:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜