C# 服务器端 Javascript (aspx.cs) Attributes.Add 代码来更改标签的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/844275/
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
Server-side Javascript (aspx.cs) Attributes.Add Code to Change a Label's text
提问by
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#?
有谁知道正确的代码?另外,这种类型的代码指的是什么?它只是 JavaScript 或 C# 中的 JavaScript 还是有特定名称?最后,是否有书籍或在线资源列出了用于 C# 的 control.attributes.add("event", "syntax") 代码的选择?
采纳答案by Guffa
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(除非您更改为 ASP.NET 以外的平台,在该平台上您实际使用 Javascript 作为服务器语言)。你所做的是给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
总是包含了ID,您可以使用访问从Javascript的元素,所以最后的代码始终工作不管,如果控制在一个命名容器或没有。
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。
回答by Russ Cam
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.NETfor 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,您需要Label2
通过其 ClientID进行引用,并且需要编写一些 JavaScript 来更改标签的文本值(我认为 ASP.NET 标签会呈现为<span>
标签)。