2

所以,我有一个元素:

<input id="loginEmail" type="email" class="form-control" name="email" placeholder="email" />

我可以使用它Request.Form["email"]

当我使用以下内容时:

<input id="loginEmail" runat="server" type="email" class="form-control" name="email" placeholder="email" />

注意:我添加runat="server"它不起作用。如何在不使用的情况下检索数据loginEmail.Value

我想这样做的原因是在方法运行后保留输入值,就像这样(如果提交不成功,我会显示一条错误消息让用户编辑表单的值):

loginEmail.Value = Request.Form["email"];

谢谢你们。

4

2 回答 2

1

如果它不是服务器控件,则按名称选择输入控件的值。如果它是服务器控件,则需要按 Id 选择它。

Request.Form["loginEmail"]

用于Request.Form.AllKeys检查发送的内容以及发送方式。

于 2014-05-28T20:25:36.500 回答
1

Although @Schadensbegrenzer answer might be of your use but conceptually it is wrong. Request.Form will only give you value if name is passed.

Mentioned answer is working for you because ASP.Net will automatically add name attribute with the value same as id if you don't specify it. So here id = name and you are picking is based on name only.

You can just have name that is different from id with runat = server and try to pick value based on id, it will give you null.

The answer is you question is "It is impossible".

于 2017-03-28T08:12:04.480 回答