如何在javascript中为文本分配文本? [英] How to assign a text to label in javascript?

查看:70
本文介绍了如何在javascript中为文本分配文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RegularExpressionValidator来验证文本框。



现在,如果文本框有效,我需要在Label Control上显示Page is Valid消息。



我是在Code Behind中做的,还是在javascript中声明标签?



在javascript中我尝试使用:

  document.getElementById (LabelId)。innerText =Your Text Here

但它显示 Microsoft JScript运行时错误


无法设置属性'innerText'的值:object为null或
undefined 。


我也试过使用innerHTML,但它显示了类似的错误。那么为标签分配值的正确方法是什么?
编辑:以下是我的aspx编码:

 <%@ Page Title =Language =C# MasterPageFile =〜/ Site.MasterAutoEventWireup =true
CodeBehind =RegularExpressionValidator.aspx.csInherits =ValidationTask.WebForm3%>
< asp:Content ID =Content1 ContentPlaceHolderID =HeadContentrunat =server>
< / asp:Content>
< asp:Content ID =Content2ContentPlaceHolderID =MainContentrunat =server>
< asp:Label ID =LabelTextrunat =serverText =Label>< / asp:Label>< br />
< script type =text / javascriptlanguage =javascript>
函数ValidatePage(){
debugger;
if(Page_IsValid){
document.getElementById('<%= LabelText.ClientID%>')。innerHTML ='Page Is Valid';
}
else {
document.getElementById('<%= LabelText.ClientID%>')。innerHTML ='Page is Invalid';
}
}
< / script>
< asp:Label ID =LabelZiprunat =serverText =输入邮政编码>< / asp:Label>
< asp:TextBox ID =TextBox1runat =server>< / asp:TextBox>< br />
< asp:Button ID =Button1runat =serverText =EnterOnClientClick =return ValidatePage()/>

< asp:RegularExpressionValidator ID =RegularExpressionValidator1ControlToValidate =TextBox1
ValidationExpression =\d {5}runat =serverErrorMessage =邮政编码必须是5个数字
位>< / asp:RegularExpressionValidator>
< / asp:Content>

提前致谢。



PS 我是新来的,这是我的第一个问题。如果我犯了任何错误,请忽略。

解决方案

生成客户ID



在浏览器中查看您的页面来源。这将为您提供有关如何生成客户端ID的良好指示。每个元素ID都应该是唯一的,并且使用母版页和控件,您和.NET框架很难产生这样的结果。如果您使用包含内容占位符的母版页,您会注意到您的元素ID是根据它们所在的内容占位符的名称生成的。



这是一个好的代码项目文章生成客户ID。



验证



在您的情况下,使用 RegularExpressionValidator 将在按下按钮时激活页面验证程序,如果有效,则会将页面发布回服务器。最好在Code Behind中添加代码。在这种情况下,类似 @Sachin ScriptManager.RegisterStartupScript 应该在按钮的事件处理程序中工作。



假设您只有一个字段:您可以将验证器转换为 CustomFieldValidator 并通过javascript激活正则表达式。如果值正确,您可以在javascript中设置标签文本而无需进行任何回发,但是一旦页面回发,您可能会丢失标签的状态。



<编辑:
jQuery



我经常使用 jQuery选择器,因为我不想麻烦生成的客户端ID。您可以使用 Ends With Selector 获取元素,如下所示:

  var name = $('input [id $ =txtName]'); //找到以txtName结尾的元素。 

请记住,如果元素在多个地方以相同名称结尾,则可能会发生冲突在生成的页面上。


I am using RegularExpressionValidator to validate a textbox.

Now if the textbox is valid,i need to display a "Page is Valid" message on Label Control.

Do I do it in Code Behind or do i declare the label in javascript itself?

In javascript I have tried using the :

document.getElementById("LabelId").innerText="Your Text Here"

but it shows Microsoft JScript runtime error:

Unable to set value of the property 'innerText': object is null or undefined".

Also I have tried using innerHTML instead,but it shows the similar error. So whats the correct way to assign value to label? Edited: Below is my aspx coding:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"        
CodeBehind="RegularExpressionValidator.aspx.cs" Inherits="ValidationTask.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="LabelText" runat="server" Text="Label"></asp:Label><br />
<script type="text/javascript" language="javascript">
   function ValidatePage() {
       debugger;
       if (Page_IsValid) {
           document.getElementById('<%= LabelText.ClientID %>').innerHTML = 'Page Is Valid';
       }
       else {
           document.getElementById('<%= LabelText.ClientID %>').innerHTML = 'Page is Invalid';
       }
   } 
</script>  
<asp:Label ID="LabelZip" runat="server" Text="Enter Zip Code"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Enter" OnClientClick="return ValidatePage()"  />

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TextBox1" 
ValidationExpression="\d{5}" runat="server" ErrorMessage="The zip code must be 5 numeric   
digits"></asp:RegularExpressionValidator>
</asp:Content>

Thanks in Advance.

P.S. I am new here and this is my first question. Please ignore if i have made any mistakes.

解决方案

Generation of Client IDs

View the source of your page in the browser. This will give you a good indication of how the Client ID is generated. Each element ID should be unique and with the use of Master Pages and Controls it would be very difficult for you and the .NET framework to produce such results. If you make use of Master Pages with Content Placeholders, you will notice that your element ID's are generated based on the names of the Content Placeholders they are in.

Here is a good Code Project article regarding the generation of client IDs.

Validation

In your case, using a RegularExpressionValidator will fire the page validator when the button is pressed and if valid resulting in the page being posted back to the server. It is probably best then to add the code in the Code Behind. In such cases something like @Sachin ScriptManager.RegisterStartupScript should work in the button's event handler.

Assuming you only have one field: You could convert your validator to a CustomFieldValidator and fire regex through javascript. If the value is correct, you can set the label text in javascript without having to do any postbacks, however you may loose the state of the label once the page has posted back.

Edit: jQuery

Oftentimes I reference my elements using jQuery selectors because I don't want to hassle with the generated client ID. You are able to get your elements using the Ends With Selector as follows:

var name = $('input[id$="txtName"]'); // Find an element that ends with "txtName".

Just remember that conflict may arise when you have an element ending with the same name in more than one place on a generated page.

这篇关于如何在javascript中为文本分配文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆