Issue getting .ClientID in ASP.NET C#

这一生的挚爱 提交于 2019-12-10 11:07:37

问题


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.


回答1:


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.




回答2:


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

try like that




回答3:


set for your client ClientIDMode="Static"

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




回答4:


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)




回答5:


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



来源:https://stackoverflow.com/questions/7147906/issue-getting-clientid-in-asp-net-c-sharp

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