C# 获取代码隐藏中文本框的值(在 asp.net 页面中使用 HTML 声明)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19086274/
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
Get the value of a textbox in codebehind (declared using HTML in an asp.net page)
提问by RookieRoll
I want to get the value of my input
tag into my C#.
我想将我的input
标签的值放入我的 C# 中。
<div id="datetimepicker2" class="input-append">
<input data-format="MM/dd/yyyy HH:mm:ss PP" type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar">
</i>
</span>
</div>// what should we use here?
The value is set by the user of the page.
该值由页面的用户设置。
采纳答案by Ishan Jain
I did't understand which control value you want to get. But If you want to get input
element value into the code behind, you need to set runat="server"
attribute, because this is a simple html element not a Asp
control.
我不明白你想得到哪个控制值。但是如果你想input
在后面的代码中获取元素值,你需要设置runat="server"
属性,因为这是一个简单的html元素而不是一个Asp
控件。
Add runat="server" and id="your_id" and you should have access to them.
添加 runat="server" 和 id="your_id",您应该可以访问它们。
for example:
例如:
<input type="text" value="Username" class="input-text autoclear"
runat="server" id="myTextBox" />
than you can simply get value of input box like this:
比你可以简单地获得输入框的值是这样的:
string myStringFromTheInput = myTextBox.Value;
回答by Don Vincent Preziosi
If what you posted is your actual code, you have an extra space in your closing tag
如果您发布的是实际代码,则结束标记中有一个额外的空间
</asp: TextBox>
Should be
应该
</asp:TextBox>
and then txt_todata.text should work
然后 txt_todata.text 应该可以工作
回答by Ramesh Rajendran
Try this
尝试这个
Add name for your input type
为您的输入类型添加名称
<input data-format="MM/dd/yyyy HH:mm:ss PP" name="txtBox1" type="text"></input>
and try this way for get value in codebehind
并尝试这种方式在代码隐藏中获取价值
string value=Request.Form["txtBox1"];
回答by Shahaji Gole
You can access all your submitted form data at server side by looking into the form Request object.
您可以通过查看表单请求对象在服务器端访问所有提交的表单数据。
Ex. Request.Form["txtDate"]
OR Request["txtDate"]
.
前任。Request.Form["txtDate"]
或Request["txtDate"]
。
Naming the html elements makes easier to look into form collection for specified element.
命名 html 元素可以更容易地查看指定元素的表单集合。