Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue getting .ClientID in ASP.NET C#

I have the following in the uploadError javascript function for AsyncFileUpload from AJAX toolkit:

function uploadError(sender, args) {
    document.getElementById("<%# uploadResult.ClientID %>").innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
}

Unfortunately the ClientID call returns Null, so the javascript errors.

I have also noticed that none of my controls have the usual .NET format once the page is loaded: E.G.:

<asp:Label runat="server" Text="Select an image to upload it to this stock item...." ID="uploadResult" /> 

Would usually render like this:

<span id="ctl00_ContentPlaceHolder1_uploadResult">Choose a webstock file to upload...</span>

But with this file it is rendering as:

<span id="uploadResult">Select an image to upload it to this stock item....</span>

I presume this is the same issue, but don't know why it's happening.

like image 831
Ben Drury Avatar asked Feb 06 '26 15:02

Ben Drury


2 Answers

The problem is you are using the <%# syntax which is only executed on binding (evals).

You should be using <%= syntax which will always execute.

Eg:

function uploadError(sender, args)
{
    document.getElementById('<%= uploadResult.ClientID %>').innerText = 
        args.get_fileName() + "<span style='color:red;'>" + 
        args.get_errorMessage() + "</span>";
}

Reference for more information on asp.net inline syntax.

Data-binding Syntax

Inline Syntax

EDIT: Note you had , in your assignment to innerText which would also be an issue if it is not a typo.

like image 97
Kelsey Avatar answered Feb 09 '26 04:02

Kelsey


function uploadError(sender, args) {
    document.getElementById("<%= uploadResult.ClientID %>").innerText = args.get_fileName(), "<span style='color:red;'>" + args.get_errorMessage() + "</span>";

try like that

like image 38
furkanozdemir Avatar answered Feb 09 '26 05:02

furkanozdemir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!