Issue getting .ClientID in ASP.NET C#

旧街凉风 提交于 2019-12-06 08:16:06

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!