问题在ASP.NET C#得到.ClientID问题、ASP、ClientID、NET

2023-09-11 01:08:11 作者:香港马子社会狗。

我在uploadError时javascript函数从AJAX工具包AsyncFileUpload如下:

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>";
}

不幸的是,客户端ID 调用返回,所以JavaScript错误。

Unfortunately the ClientID call returns Null, so the javascript errors.

我也注意到,没有我的控制有通常的.NET格式一旦加载页面:如:

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>

我presume这是同样的问题,但不知道为什么它的发生。

I presume this is the same issue, but don't know why it's happening.

推荐答案

现在的问题是,你正在使用的&LT;%#语法,只执行约束力(evals )。

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

您应该使用&LT;%= 语法,这将永远执行

You should be using <%= syntax which will always execute.

例如:

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

Reference关于asp.net在线语法的更多信息。

数据绑定语法

内联语法

编辑:请注意你有提交作业的innerText 这也将是一个问题,如果它不是一个错字。

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