Javascript 在 ASP.NET C# 中获取 .ClientID 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7147906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:03:24  来源:igfitidea点击:

Issue getting .ClientID in ASP.NET C#

javascriptasp.netajaxclientid

提问by Ben Drury

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

我在 AJAX 工具包的 AsyncFileUpload 的 uploadError javascript 函数中有以下内容:

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

Unfortunately the ClientIDcall returns Null, so the javascript errors.

不幸的是,ClientID调用返回Null,因此 javascript 错误。

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

我还注意到,一旦页面加载,我的控件都没有通常的 .NET 格式:EG:

<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.

我认为这是同一个问题,但不知道为什么会这样。

回答by Kelsey

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

问题是您使用的<%#语法仅在绑定(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.

有关 asp.net 内联语法的更多信息的参考。

Data-binding Syntax

数据绑定语法

Inline Syntax

内联语法

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

编辑:请注意,如果不是拼写错误,,您的作业中innerText也有问题。

回答by furkanozdemir

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

try like that

试试看

回答by Muhammad Akhtar

set for your client ClientIDMode="Static"

为您的客户设置 ClientIDMode="Static"

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

或在页面级别设置 <%@ Page ClientIDMode="Static"

回答by LokizFenrir

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

id 建议不要强制静态并限制其自己控件的 C# ASP 范围,id 建议像这样转义特殊字符

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

如果需要,将其放入循环中并迭代每个函数 elementsArr 并将其更改为

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.

但最好的解决方案是使用 sizzle (jquery) 搜索所有 id 以 ctl00 开头的元素,默认情况下 asp 元素总是预先加上 ctl00。

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

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

//使用this函数调用id到一个变量,然后用下划线(_)分割

//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.});

//将最后一个值推入数组...即所有下划线后的最后一项始终是真实的ID。并且以这种方式使用 sizzle 您只能使用 asp 元素,这要归功于很棒的选择器,它是大声笑 - 代码更少,做得更多。});

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)

我真的为速记表示歉意,但它深夜,我以前做过这件事,这是我知道的最稳定的选择,你不会通过修改标准细节潜在地引起任何冲突。(这样做总是不好的做法)

回答by James Johnson

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

您可以将 ClientID 模式设置为静态,也可以尝试使用 UniqueID 而不是 ClientID。