C# 如何从后面的asp.net代码访问html表单输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/389149/
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
How to access html form input from asp.net code behind
提问by
I have a basic HTML form that gets inserted into a server side div tag based on how many records exist in the database. This HTML form comes out just fine, and everything looks good. But on my action page I cannot seem to access the input elements from the code behind. I have tried using the Request scope but have come up empty on that approach. Any other suggestions?
我有一个基本的 HTML 表单,它根据数据库中存在的记录数插入到服务器端 div 标签中。这个 HTML 表单很好,一切看起来都很好。但是在我的操作页面上,我似乎无法从后面的代码访问输入元素。我曾尝试使用 Request 范围,但在该方法上却是空的。还有其他建议吗?
All of the below suggestions are great, and normally that is what I would do. But these forms are being built on the fly after the page is being compiled, so runat='server' did not do anything for me. It just passed that along to the html page.
以下所有建议都很棒,通常这就是我会做的。但是这些表单是在页面编译后即时构建的,所以 runat='server' 没有为我做任何事情。它只是将其传递给 html 页面。
采纳答案by Bryan Rehbein
If you are accessing a plain HTML form, it has to be submitted to the server via a submit button (or via javascript post). This usually means that your form definition will look like this (I'm going off of memory, make sure you check the html elements are correct):
如果您正在访问纯 HTML 表单,则必须通过提交按钮(或通过 javascript 帖子)将其提交到服务器。这通常意味着您的表单定义将如下所示(我的内存不足,请确保您检查 html 元素是否正确):
<form method="POST" action="page.aspx">
<input id="customerName" name="customerName" type="Text" />
<input id="customerPhone" name="customerPhone" type="Text" />
<input value="Save" type="Submit" />
</form>
You should be able to access the customerName and customerPhone data like this:
您应该能够像这样访问 customerName 和 customerPhone 数据:
string n = String.Format("{0}", Request.Form["customerName"]);
If you have method="GET"
in the form (not recommended, it messes up your URL space), you will have to access the form data like this:
如果你有method="GET"
表单(不推荐,它会弄乱你的 URL 空间),你将不得不像这样访问表单数据:
string n = String.Format("{0}", Request.QueryString["customerName"]);
This of course will only work if the form was 'Posted', 'Submitted', or done via a 'Postback'. (i.e. somebody clicked the 'Save' button, or this was done programatically via javascript.)
这当然只有在表单被“发布”、“提交”或通过“回发”完成时才有效。(即有人点击了“保存”按钮,或者这是通过 javascript 以编程方式完成的。)
Also, keep in mind that accessing these elements in this manner can only be done when you are not using server controls (i.e. runat="server"
), with server controls the id and name are different.
另外,请记住,以这种方式访问这些元素只能在您不使用服务器控件(即runat="server"
)时完成,服务器控件的 id 和名称是不同的。
回答by Gant
It should normally be done with Request.Form["elementName"]
.
通常应该用Request.Form["elementName"]
.
For example, if you have <input type="text" name="email" />
then you can use Request.Form["email"]
to access its value.
例如,如果你有<input type="text" name="email" />
那么你可以使用它Request.Form["email"]
来访问它的值。
回答by annakata
Edit: thought of something else.
编辑:想到了别的东西。
You say you're creating a form dynamically - do you really mean a <form> and its contents 'cause asp.net takes issue with multiple forms on a page and it's already creating one uberform for you.
你说你正在动态地创建一个表单——你真的是说一个 <form> 及其内容吗?因为asp.net 对页面上的多个表单有问题,它已经为你创建了一个 uberform。
回答by rlb.usa
Simplest way IMO is to include an ID and runat server tag on all your elements.
IMO 最简单的方法是在所有元素上包含 ID 和 runat 服务器标记。
<div id="MYDIV" runat="server" />
Since it sounds like these are dynamically inserted controls, you might appreciate FindControl().
由于听起来这些是动态插入的控件,您可能会喜欢 FindControl()。
回答by rlb.usa
What I'm guessing is that you need to set those input elements to runat="server".
我猜你需要将这些输入元素设置为runat="server"。
So you won't be able to access the control
所以你将无法访问控件
<input type="text" name="email" id="myTextBox" />
But you'll be able to work with
但你将能够与
<input type="text" name="email" id="myTextBox" runat="server" />
And read from it by using
并使用它从中读取
string myStringFromTheInput = myTextBox.Value;
回答by Eppz
Since you're using asp.net code-behind, add an id to the element and runat=server.
由于您使用的是 asp.net 代码隐藏,请向元素和 runat=server 添加一个 id。
You can then reference the objects in the code behind.
然后您可以在后面的代码中引用这些对象。