ASP的设置Text属性:标签在Javascript中有道 [英] Set Text property of asp:label in Javascript PROPER way

查看:122
本文介绍了ASP的设置Text属性:标签在Javascript中有道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格上的一系列文本框。当用户插入数字到这些文本框,计算由和< ASP:标签> 控件通过JavaScript更新,以反映这些计算:

 的document.getElementById('<%= TotalLoans.ClientID%GT;')。innerHTML的= TotalLoans;

这正确地更新UI。然而,当我尝试在codebehind访问值时,文本属性为空。这是有道理的我想,因为我是通过JavaScript的更新的innerHTML 属性。

  // TotalLoans.Text将始终等于在这种情况下
双bTotalLoans = string.IsNullOrEmpty(TotalLoans.Text)
                   ? 0.00
                   :Convert.ToDouble(TotalLoans.Text);

如何更新的文本属性< ASP:;以这样的方式通过JavaScript标签&gt我可以读取codebehind财产?

更新

这是一个包含41标签,每个显示一些计算用户的结果大表上的一个小问题。以FishBasketGordo的意见,我将我的< ASP:标签> 来禁用< ASP:文本框> 。我设置了新的文本框的值,例如:

 的document.getElementById('<%= TotalLoans.ClientID%GT;')。值= TotalLoans;

再次在codebehind, TotalLoans.Text 的值总是等于



我不介意改变我如何处理这一点,但这里是问题的症结所在。

我使用JavaScript来操纵某些控件的属性值。我需要能够从当提交被点击后面的code访问这些操纵值。

任何意见,我怎么能去呢?

更新2

关于通过@詹姆斯 - 约翰逊的回答,我不能作为建议检索使用 .innerText 属性的值。我有的EnableViewState 设置为true的< ASP:标签> 。有没有别的我丢失的东西吗?

我不明白为什么,当我在一个文本框输入并提交表单,我可以在codebehind访问的价值,但是当我编程由JavaScript的方式改变一个文本框或标签的文本,我无法访问新的价值。


解决方案

将HiddenField控件在窗体中。

 < ASP:HiddenField ID =隐藏=服务器/>

创建形式的属性

 保护字符串LabelProperty
{
    得到
    {
        返回hidden.Value;
    }
    组
    {
        hidden.Value =价值;
    }
}

更新从JavaScript的隐藏字段值

 <脚本>
   功能UpdateControl(){
            的document.getElementById('<%= hidden.ClientID%GT;')。值='12';
   }
< / SCRIPT>

现在,你可以在整个回发直接访问该物业。在标签控制更新的值将在回传的情况下,会丢失它被直接用于code后面。

I have a series of textboxes on a form. When the user inserts numbers into these textboxes, calculations are made and <asp:Label> controls are updated via JavaScript to reflect these calculations:

document.getElementById('<%=TotalLoans.ClientID %>').innerHTML = TotalLoans;

This correctly updates the UI. However, when I try to access the value in the codebehind, the Text property is empty. This makes sense I guess, since I was updating the innerHTML property via the JavaScript.

//TotalLoans.Text will always be equal to "" in this scenario
double bTotalLoans = string.IsNullOrEmpty(TotalLoans.Text) 
                   ? 0.00 
                   : Convert.ToDouble(TotalLoans.Text);

How do I update the Text property of the <asp:Label> via JavaScript in such a way that I can read the property in the codebehind?

Update

This is a small problem on a large form that contains 41 labels, each of which displays the results of some calculation for the user. Taking the advice of FishBasketGordo I converted my <asp:Label> to a disabled <asp:TextBox>. I'm setting the value of the new textbox as such:

    document.getElementById('<%=TotalLoans.ClientID %>').value = TotalLoans;

Again, in the codebehind, the value of TotalLoans.Text is always equal to "".


I don't mind changing how I approach this, but here's the crux of the matter.

I am using JavaScript to manipulate the property values of some controls. I need to be able to access these manipulated values from the code behind when 'Submit' is clicked.

Any advice how I can go about this?

Update 2

Regarding the answer by @James Johnson, I am not able to retrieve the value using .innerText property as suggested. I have EnableViewState set to true on the <asp:Label>. Is there something else I am missing?

I don't understand why, when I type in a textbox and submit the form, I can access the value in the codebehind, but when I programmatically change the text of a textbox or label by way of JavaScript, I cannot access the new value.

解决方案

Place HiddenField Control in your Form.

<asp:HiddenField ID="hidden" runat="server" />

Create a Property in the Form

protected String LabelProperty
{
    get
    {
        return hidden.Value;
    }
    set
    {
        hidden.Value = value;
    }
}

Update the Hidden Field value from JavaScript

<script>
   function UpdateControl() {
            document.getElementById('<%=hidden.ClientID %>').value = '12';
   }
</script>

Now you can access the Property directly across the Postback. The Label Control updated value will be Lost across PostBack in case it is being used directly in code behind .

这篇关于ASP的设置Text属性:标签在Javascript中有道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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