如何从asp.net code访问HTML表单输入后面 [英] How to access html form input from asp.net code behind

查看:115
本文介绍了如何从asp.net code访问HTML表单输入后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被基于多条记录在数据库中是如何存在的插入服务器端div标签基本的HTML表单。这个HTML表单出来就好了,一切看起来不错。但我的操作页面上的我似乎无法从后面code访问输入元素。我已经使用Request范围尝试,但都拿出了空,对于这种做法。任何其他建议?

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?

所有的以下建议是巨大的,通常这是我会怎么做。但这些形式正在兴建动态页面被编译后,所以RUNAT =服务器并没有为我做任何事。它只是传递沿到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.

推荐答案

如果您要访问一个普通的HTML形式,它必须通过一个提交按钮提交给服务器(或通过JavaScript后)。这通常意味着你的表单定义看起来就像这样(我会关闭内存,请务必检查HTML元素是正确的):

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

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

您应该能够访问这样的客户名称和customerPhone数据:

You should be able to access the customerName and customerPhone data like this:

string n = String.Format("{0}", Request.Form["customerName"]);

如果您有方法=GET的形式(不推荐,它搅乱了你的URL空间),你将不得不像这样访问表单数据:

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:

string n = String.Format("{0}", Request.QueryString["customerName"]);

这当然如果形式发布,提交,或通过回传做只会工作。 (即有人点击了保存按钮,或者这是通过JavaScript编程完成的。)

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

另外,请记住,在这种方式访问​​这些元素只能当你不使用服务器控件来完成(即 =服务器),与服务器控制ID和名称是不同的。

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.

这篇关于如何从asp.net code访问HTML表单输入后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆