开发者

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.


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.


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

try like that


set for your client ClientIDMode="Static"

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


id advise not forcing static and limiting C# ASP scope of its own controls, id rather advise escaping special characters like so

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

if needed be put it in a loop and iterate an each function elementsArr and change it to

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

but the best solution would be using sizzle (jquery) search for all elements that has an id starting with ctl00, asp elements always has ctl00 prepended by default.

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

//use the this function call the id to a variable, then split on underscores (_)

//push the last value into an array... ie the last item after all the underscores is always the real id. and using sizzle in this way you only work with asp elements thanks to the awesome selector it is lol - code less , do more. });

I realy apologize for the shorthand, but its latenight, ive done this before and its the most stable option i know, you don't potentialy cause any conflicts by modifying standard specifics. (ALWAYS BAD PRACTISE TO DO SO)


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜