Html 如何在 ASP.NET 的服务器端获取输入(类型文本)的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11720465/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:55:50  来源:igfitidea点击:

How to obtain a value of an input (type text) on server side in ASP.NET?

asp.nethtml

提问by Konrad Viltersten

Using the HTML markup

使用 HTML 标记

<form id="form" runat="server">
  <input id="donkey" type="text" placeholder="monkey" runat="server" />
</form>

I hoped to get the entered value in code behind by

我希望在后面的代码中获得输入的值

String s = Request.Form["donkey"];

but it only produces null value. When I investigate the data structure I get something like $ctl00$main$donkeyso I know it's there. After a while, I simply switched to

但它只产生空值。当我调查数据结构时,我会得到类似的信息,$ctl00$main$donkey所以我知道它就在那里。过了一会儿,我只是切换到

<form id="form" runat="server">
  <asp:TextBox id="donkey" type="text" runat="server"></asp:TextBox>
</form>

but I still wonder how to reference the object from server-side if I for some reason won't switch to ASP-component.

但我仍然想知道如果我出于某种原因不会切换到 ASP 组件,如何从服务器端引用对象。

回答by M. Mennan Kara

If you want to access to the value using request.form, add name attribute to input tag and remove runat attribute.

如果要使用 request.form 访问该值,请将 name 属性添加到 input 标记并删除 runat 属性。

<input id="donkey" name="donkey" type="text" />

Otherwise use

否则使用

<asp:TextBox ID="donkey" type="text" runat="server"></asp:TextBox>

and on cs

并在 cs

string s = donkey.Text;

回答by user1102001

if you want to get value of input use like this

如果你想像这样获得输入使用的价值

  String s = donkey.value;

回答by Yograj Gupta

Just donkey.Valuewill return the value from text input which should have runat="server". It will create an object of System.Web.UI.HtmlControls.HtmlInputText.

只需donkey.Value将返回从里面应该有文字输入的值runat="server"。它将创建一个对象System.Web.UI.HtmlControls.HtmlInputText

回答by scunliffe

I'm not sure about ASP.net but for a regular form field to submit properly it should have a nameattribute. That would be the key that you could then lookup the form value.

我不确定 ASP.net,但对于要正确提交的常规表单字段,它应该有一个name属性。这将是您随后可以查找表单值的关键。