Error getting .ClientID in ASP.NET C #

I have a javascript uploadError 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, calling ClientID returns Null , so javascript errors.

I also noticed that none of my controls have the usual .NET format after loading the page: EG:

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

It will look like this:

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

But with this file it looks like:

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

I assume this is the same problem, but I don’t know why this is happening.

+2
source share
5 answers

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

You must use the syntax <%= , which will always be executed.

For instance:

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

Link for more information on inp.net inline syntax.

Data Binding Syntax

Inline syntax

EDIT: note that you had , in your destination, innerText , which would also be a problem if it is not a typo.

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

try to do it

+1
source

for your client ClientIDMode="Static"

OR set to page level <%@ Page ClientIDMode="Static"

+1
source

id advises not to force the static and bounding region of C # ASP with its own controls, ID rather advises escaping special characters like this

 document.getElementById('/</%= uploadResult.ClientID /%/>').innertext = "xyz or whatever"; 

if necessary, put it in a loop and iterate over each elementsArr function and change it to

 document.getElementById('/</%= '+ elementsArr[x] +'.ClientID /%/>').innertext = "xyz or whatever"; 

but the best solution would be to use sizzle search (jquery) for all elements that have an id starting with ctl00, asp elements always have ctl00 added by default.

 $("id^=ctl00").each(){ 

// use this function by calling the identifier of the variable, then divide by underscores (_)

// push the last value into the array ... , i.e. the last element after all underscores are always the real identifier. and using hiss in this way, you only work with asp elements thanks to the amazing selector, which is smaller than the lol code, to do more. });

I really apologize for the shorthand, but its latency, I did it before and its most stable version, which I know, you can not cause any conflicts by changing the standard specification. (ALWAYS LEGAL PRACTICE SHOULD BE SO)

+1
source

You can either set the ClientID mode to static, or try using UniqueID instead of ClientID.

0
source

All Articles