如何从asp.net代码中访问html表单输入?

时间:2021-11-10 21:19:02

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表单很好,看起来一切都很好。但是在我的操作页面上,我似乎无法从后面的代码中访问输入元素。我尝试过使用请求范围,但在这种方法上没有找到。还有其他的建议吗?

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页面。

6 个解决方案

#1


60  

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 post)提交给服务器。这通常意味着您的表单定义将如下所示(我的内存即将耗尽,请确保检查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和电话数据:

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.)

当然,只有当表单被“发布”、“提交”或通过“回邮”完成时,才会有效。(也就是说,有人点击了“Save”按钮,或者这是通过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和名称是不同的。

#2


21  

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;

#3


9  

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.

例如,如果您有,那么您可以使用Request。表单["email"]访问其值。

#4


2  

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()。

#5


0  

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.

然后可以引用后面代码中的对象。

#6


-1  

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.

你说你正在动态地创建一个表单——你真的是说一个

和它的内容吗?因为asp.net在一个页面上有多个表单,并且它已经为你创建了一个uberform。

#1


60  

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 post)提交给服务器。这通常意味着您的表单定义将如下所示(我的内存即将耗尽,请确保检查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和电话数据:

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.)

当然,只有当表单被“发布”、“提交”或通过“回邮”完成时,才会有效。(也就是说,有人点击了“Save”按钮,或者这是通过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和名称是不同的。

#2


21  

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;

#3


9  

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.

例如,如果您有,那么您可以使用Request。表单["email"]访问其值。

#4


2  

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()。

#5


0  

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.

然后可以引用后面代码中的对象。

#6


-1  

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.

你说你正在动态地创建一个表单——你真的是说一个

和它的内容吗?因为asp.net在一个页面上有多个表单,并且它已经为你创建了一个uberform。