如何获取文本来自以下;输入隐藏>在code-背后? [英] How to get the text from <input hidden> in code-behind?

查看:153
本文介绍了如何获取文本来自以下;输入隐藏>在code-背后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天的第二个问题。我真是卡住了。
好吧,我必须在一个aspx文件两个领域:

 <输入类型=文本ID =tbName=服务器/>
<输入类型=隐藏ID =HFNAME=服务器/>

我们的想法是使用来自文本框的tbName文本为我的存储过程的参数。
我决定把它的文本添加到一个隐藏字段的HFNAME使用javascript:

 的document.getElementById(<%= hfName.ClientID%GT;)值=的document.getElementById(<%= tbName.ClientID%GT;)。值;
警报(的document.getElementById(<%= hfName.ClientID%GT;)值。);

它运作良好和隐藏字段从文本框,关于它的功能,需要提醒的文字。我使用隐藏域,因为这是我保存回发期间文本的唯一途径。

在code-后面我试图从hfName.Value隐藏值回文本框,但它返回空行:

  tbName.Value = hfName.Value;

因此​​,如何不失去文本框的文字,并用它作为参数?也许还有一个更简单的方法?
我不知道jquery的。


解决方案

  

在主要的Page_Load方法我把tbName.Value = hfName.Value;
  这是我尝试从隐藏字段利用价值的唯一的地方
  hfName.Value。 tbName后没有被修改,应该显示文本
  从隐藏字段HFNAME。事实并非如此。


在页面加载没有设置为文本框的文本,所以tbName和HFNAME值是空的。

您可以设置默认值来测试这两个隐藏的文本字段

 <输入类型=文本ID =tbName=服务器值=txtVal/>
<输入类型=隐藏ID =HFNAME=服务器值=hftVal/>

现在在页面加载时,你可以得到这两个控件的非空值

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    VAR txtVal = tbName.Value;
    VAR hfVal = hfName.Value;
}

My second question today. I got really stuck. Ok, I have two fields in an aspx file:

<input type="text" id="tbName" runat="server"/> 
<input type="hidden" id="hfName" runat="server"/>

The idea is to use text from textbox "tbName" as a parameter for my stored procedure. I decided to add its text to a hidden field "hfName" using javascript:

document.getElementById("<%= hfName.ClientID %>").value = document.getElementById("<%= tbName.ClientID %>").value;
alert(document.getElementById("<%= hfName.ClientID %>").value);

It works well and hidden field takes the text from the textbox, the function alerts about it. I use hidden field because this is the only way for me to save the text during postback.

In code-behind I try to get hidden value from hfName.Value back to the textbox, but it returns empty line:

tbName.Value = hfName.Value;

So how not to lose the textbox text and use it as a parameter? Maybe there is an easier way? I don't know jquery.

解决方案

In the main Page_Load procedure I put tbName.Value = hfName.Value; This is the only place where I try to use value from hidden field hfName.Value. tbName is not modified after it and should show the text from hidden field hfName. It doesn't.

when page loading nothing set as textbox text, so tbName and hfName values are empty.

you can test this by set default values to both hidden and text fields

<input type="text" id="tbName" runat="server" value ="txtVal"/> 
<input type="hidden" id="hfName" runat="server" value ="hftVal"/>

now on page load you can get non empty values of both controls

protected void Page_Load(object sender, EventArgs e)
{
    var txtVal = tbName.Value;
    var hfVal = hfName.Value;
}

这篇关于如何获取文本来自以下;输入隐藏&GT;在code-背后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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