使用 C# 迭代同名的表单字段

发布于 2024-08-27 23:09:24 字数 650 浏览 4 评论 0原文

我需要以与表单结果的其余部分不同的方式处理表单的一部分。在需要特殊处理的部分中,我需要迭代 3 个具有相同名称的表单字段。他们必须有相同的名字,我无法改变它。我所指的表单部分看起来像这样:

<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>

使用 C# 我尝试这样的操作:

我尝试像这样处理它:

int i;

for (i = 1; i <= Request.Form["Color"][i]; i++)
{
    colorName.Text += Request.Form["Color"];
}

这会导致以下异常:

System.NullReferenceException:对象引用未设置为对象的实例。

我应该如何处理同名的表单字段?

I have a section of a form that I need to handle differently from the rest of form results. In the section that needs special handling I need to iterate over 3 form fields that have the same name. They have to have the same name, I can't change it. The section of the form I am referring to looks something like this:

<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>

Using C# I try something like this:

I try to handle it like this:

int i;

for (i = 1; i <= Request.Form["Color"][i]; i++)
{
    colorName.Text += Request.Form["Color"];
}

Which leads to the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.

How should I be handling form fields with the same name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在深巷 2024-09-03 23:09:24

你不需要做任何劈叉或其他特殊魔法;您可以简单地从 ASP.NET 获取字符串数组:

string[] value = Request.Form.GetValues("Color");

You don't need to do any splits or other special magic; you can simply get a string array from ASP.NET:

string[] values = Request.Form.GetValues("Color");

呆头 2024-09-03 23:09:24

我建议将每个表单添加到列表中并使用 ForEach 运算符,尽管它比您已经尝试做的事情有点冗长。

例如

private List<Request.Form> newList = new List<Request.Form>();

newList.Add(FormName1);
newList.Add(FormName2);
newList.Add(FormName3);

foreach(Request.Form form in newList)
{
   //perform logic
}

注意:我假设 Request.Form 是表单本身的类名。

我自己没有测试过,所以可能会有一些错误,希望它能在一定程度上有所帮助。

I would suggest adding each form to a list and using the ForEach operator instead, although it's a bit more longwinded than what you're already trying to do.

e.g.

private List<Request.Form> newList = new List<Request.Form>();

newList.Add(FormName1);
newList.Add(FormName2);
newList.Add(FormName3);

foreach(Request.Form form in newList)
{
   //perform logic
}

Note: I'm assuming that Request.Form is the class name for the Form itself.

I've not tested this myself so there might be a few errors in it, hope it helps to a degree anyway.

﹏雨一样淡蓝的深情 2024-09-03 23:09:24

重要的是要记住,发布的表单值只不过是名称/值集合,其中值是一个简单的字符串。因此,拥有多个同名的表单字段没有多大意义。我什至不知道它是否真的被允许。

但正如你所说,你无法改变这一点。我在 IE 和 Chrome 中进行了快速测试,至少在这些浏览器中,它们似乎发送了与逗号分隔字符串同名的多个表单字段。您可能需要进行更多测试,以确保此行为在浏览器中保持一致。

考虑到这一点,您可以说:

string colorValues = Request.Form["Color"];
string [] colors = colorValues.Split(',');

颜色数组中的每个元素现在对应于发布的每个输入元素的值。

It is important to remember that the form values posted is nothing more than a name/value collection, where the value is a simple string. So it does not make much sense to have multiple form fields with the same name. I don't even know whether it is actually allowed.

But as you say, you cannot change that. I performed a quick test in IE and Chrome, and at least in these browsers, it seems that they send multiple form fields with the same name as a comma-separated string. You might want to test some more to make sure this behaviour is consistent across browsers.

With that in mind, you could say:

string colorValues = Request.Form["Color"];
string [] colors = colorValues.Split(',');

Each element in the colors array now corresponds to the value of each input element that was posted.

远昼 2024-09-03 23:09:24

Request.Form["Color"] 为 null,或者在 aspx 页面上找不到 colorName。根据你的代码我无法判断是哪个。否则,它看起来是正确的。 “Color”是什么类型的 html 元素?如果它只是一个 html 元素,请确保它具有 id 和 name 属性。

Either Request.Form["Color"] is null, or colorName is not found on the aspx page. I can't tell which based on your code. Otherwise, it looks correct. What type of html element is "Color?" If it's just an html element, make sure that you have both id and name attributes on it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文