简体   繁体   English

在ASP.NET c#代码隐藏中没有获得HTML输入类型的价值

[英]Not getting value of HTML input type in ASP.NET c# code-behind

I got the process of getting the value of input field in c# in here: 我在这里获得了在c#中获取输入字段值的过程:

Get value from input html in codebehind c# 从codebehind c#中的输入html获取值

I have a hidden input field in my aspx page like this: 我的aspx页面中有一个隐藏的输入字段,如下所示:

<input type="hidden" id="lblCountry_val" runat="server" />

Where the value of the hidden field is put through jquery: 隐藏字段的值通过jquery放置的位置:

<script type="text/javascript">
$(function () {
        BindUserInfo();
    })


function BindUserInfo()
{
 document.getElementById('lblCountry_val').value = window.strcountry;
 }
</script>
<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>

But When I am trying to get the value in Page_Load event in code behind with this: Response.Write(lblCountry_val.Value); 但是当我试图在代码后面的Page_Load事件中获取值时: Response.Write(lblCountry_val.Value);

Nothing is being printed. 什么都没打印出来。 How come? 怎么会?

EDIT I have done this by changing the hidden input field to an invisible textbox and then putting "name" attribute in the tag. 编辑我通过将隐藏的输入字段更改为不可见的文本框,然后在标记中添加“name”属性来完成此操作。

<input type="text" id="lblCountry_val" name="lblCountry_val" runat="server" style="display:none" />

And in the code behind: 并在代码背后:

var txt=Request.Form["lblCountry_val"];

Though I have not a clear idea how it was done. 虽然我不清楚它是如何完成的。

You should write 你应该写

document.getElementById('<%=lblCountry_val.ClientID%>')

This happens because in the most cases the serve side Id of a control is different from its clientId. 发生这种情况是因为在大多数情况下,控件的服务端ID与其clientId不同。 The way to take it is the above. 采取它的方法是上述。

First Method - In aspx, When you set a value to html field using Java script, Field's value doesn't appear in code behind file(aspx.cs). 第一种方法 - 在aspx中,当您使用Java脚本将值设置为html字段时,Field的值不会出现在文件后面的代码(aspx.cs)中。 So you have to do additional page post back for set a value to hidden field and then you can able to catch the value in code behind file. 因此,您必须执行其他页面回发以将值设置为隐藏字段,然后您才能捕获文件后面的代码中的值。

Second Method - Using tag, submit hidden field data to relevant aspx page.Then you can catch the value using Request.Form["lblCountry_val"] array. 第二种方法 - 使用标记,将隐藏的字段数据提交给相关的aspx页面。然后使用Request.Form [“lblCountry_val”]数组捕获值。

Try this... 尝试这个...

JavaScript

<script>
    $(document).ready(function () {
        var test = "1";
        $("<%=hdn_audio_length.ClientID%>").val(test);
    });
</script>

Html 

<asp:HiddenField runat="server" ID="hdn_audio_length" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" Text="Click" OnClick="button1_Click" />

C#

protected void button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = hdn_audio_length.Value;
}

Here's an example setting hidden fields on submit click. 这是一个在提交点击时设置隐藏字段的示例。

`<script>
    $(document).ready(function () {
        $("#submit").click(function () {
            $("#<%= ccnum.ClientID%>").val($("#cc-num").val());
            $("#<%= expdate.ClientID%>").val($("#cc-exp").val());
            $("#<%= cvc.ClientID%>").val($("#cc-cvc").val());
        });
    });
</script>`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM