服务器端Javascript(aspx.cs)Attributes.Add代码更改标签的文本

时间:2022-03-23 15:37:13

I am trying to change a label's text by using server-side JavaScript (onclick) and C# within the page_load event. For example, I would like to write something like the following:

我试图通过在page_load事件中使用服务器端JavaScript(onclick)和C#来更改标签的文本。例如,我想写下面的内容:

Label1.Attributes.Add("onclick", "Label2.text='new caption'")

Does anyone know the correct code for this? Also, what is this type of code referred to; is it just JavaScript or JavaScript in C# or is there a specific name? Lastly, does a book or online resource exist that lists the choices of control.attributes.add("event", "syntax") code to use with C#?

有谁知道这个正确的代码?此外,这种类型的代码是指什么;它只是C#中的JavaScript或JavaScript还是有特定的名称?最后,是否存在列出用于C#的control.attributes.add(“event”,“syntax”)代码选项的书籍或在线资源?

2 个解决方案

#1


There is no server-side Javascript (unless you change to a platform other than ASP.NET where you actually use Javascript as server language). What you are doing is adding an attribute to the html tag, and the code will be executed entirely on the client side.

没有服务器端Javascript(除非您更改为实际使用Javascript作为服务器语言的ASP.NET以外的平台)。你正在做的是在html标签中添加一个属性,代码将完全在客户端执行。

First, let's look at how it's done in HTML without the server side code and server side controls:

首先,让我们看一下如何在没有服务器端代码和服务器端控件的情况下完成HTML:

<span onclick="document.getElementById('Label2').innerHTML='Thank you';">Click me</span>
<span id="Label2"></span>

To use Label controls instead, setting the onclick attribute from server side code, you would do like this:

要使用Label控件,从服务器端代码设置onclick属性,您可以这样做:

Label1.Attributes.Add("onclick", "document.getElementById('Label2').innerHTML='Thank you';");

This will work as long as the controls are not inside a naming container. If they are, the id of the controls are prepended with the name of the container to keep them unique, so you need to use the ClientID property to find out what their final id is:

只要控件不在命名容器中,这就可以工作。如果是,则控件的id前缀为容器的名称以使它们保持唯一,因此您需要使用ClientID属性来找出它们的最终ID:

Label1.Attributes.Add("onclick", "document.getElementById('" + Label2.ClientID + "').innerHTML='Thank you';");

The ClientID always contains the id that you can use to access the element from Javascript, so the last code always works regardless if the control is in a naming container or not.

ClientID始终包含可用于从Javascript访问元素的ID,因此无论控件是否在命名容器中,最后一个代码始终有效。

To find out what attributes you can use, you should look at the HTML documentation, for example the Internet Explorer documentation for the span element. When looking at the documetation for a specific feature, notice the Standards Information, as that will tell you if it works in any browser or just in Internet Explorer.

要找出可以使用的属性,您应该查看HTML文档,例如span元素的Internet Explorer文档。查看特定功能的文档时,请注意标准信息,因为它会告诉您它是在任何浏览器中工作还是仅在Internet Explorer中工作。

#2


The code above adds JavaScript to a server control rendered on the client. Take a look at this MSDN article - Using JavaScript Along with ASP.NET for more information.

上面的代码将JavaScript添加到客户端上呈现的服务器控件。看一下这篇MSDN文章 - 使用JavaScript和ASP.NET一起获取更多信息。

IIRC, you will need to reference Label2 by its ClientID and will need to write some JavaScript to change the label's text value (I think ASP.NET labels get rendered as <span> tags).

IIRC,您需要通过其ClientID引用Label2,并且需要编写一些JavaScript来更改标签的文本值(我认为ASP.NET标签会呈现为标签)。

#1


There is no server-side Javascript (unless you change to a platform other than ASP.NET where you actually use Javascript as server language). What you are doing is adding an attribute to the html tag, and the code will be executed entirely on the client side.

没有服务器端Javascript(除非您更改为实际使用Javascript作为服务器语言的ASP.NET以外的平台)。你正在做的是在html标签中添加一个属性,代码将完全在客户端执行。

First, let's look at how it's done in HTML without the server side code and server side controls:

首先,让我们看一下如何在没有服务器端代码和服务器端控件的情况下完成HTML:

<span onclick="document.getElementById('Label2').innerHTML='Thank you';">Click me</span>
<span id="Label2"></span>

To use Label controls instead, setting the onclick attribute from server side code, you would do like this:

要使用Label控件,从服务器端代码设置onclick属性,您可以这样做:

Label1.Attributes.Add("onclick", "document.getElementById('Label2').innerHTML='Thank you';");

This will work as long as the controls are not inside a naming container. If they are, the id of the controls are prepended with the name of the container to keep them unique, so you need to use the ClientID property to find out what their final id is:

只要控件不在命名容器中,这就可以工作。如果是,则控件的id前缀为容器的名称以使它们保持唯一,因此您需要使用ClientID属性来找出它们的最终ID:

Label1.Attributes.Add("onclick", "document.getElementById('" + Label2.ClientID + "').innerHTML='Thank you';");

The ClientID always contains the id that you can use to access the element from Javascript, so the last code always works regardless if the control is in a naming container or not.

ClientID始终包含可用于从Javascript访问元素的ID,因此无论控件是否在命名容器中,最后一个代码始终有效。

To find out what attributes you can use, you should look at the HTML documentation, for example the Internet Explorer documentation for the span element. When looking at the documetation for a specific feature, notice the Standards Information, as that will tell you if it works in any browser or just in Internet Explorer.

要找出可以使用的属性,您应该查看HTML文档,例如span元素的Internet Explorer文档。查看特定功能的文档时,请注意标准信息,因为它会告诉您它是在任何浏览器中工作还是仅在Internet Explorer中工作。

#2


The code above adds JavaScript to a server control rendered on the client. Take a look at this MSDN article - Using JavaScript Along with ASP.NET for more information.

上面的代码将JavaScript添加到客户端上呈现的服务器控件。看一下这篇MSDN文章 - 使用JavaScript和ASP.NET一起获取更多信息。

IIRC, you will need to reference Label2 by its ClientID and will need to write some JavaScript to change the label's text value (I think ASP.NET labels get rendered as <span> tags).

IIRC,您需要通过其ClientID引用Label2,并且需要编写一些JavaScript来更改标签的文本值(我认为ASP.NET标签会呈现为标签)。