【问题标题】:How to display form variables based on variable name如何根据变量名显示表单变量
【发布时间】:2013-08-13 16:50:05
【问题描述】:

我有一个变量名列表,比如说:

List<string> list = new List<string>();
    list.Add("size");
    list.Add("width");
    list.Add("name");
    list.Add("zip");

我有具有这些确切名称的文本控件,有没有办法循环遍历列表并只显示我要求的文本控件的值,比如让我们说这样的话:

foreach (string txtctl in list) 
    {
        Response.Write(Request.Form[txtctl]);
    }

当我运行此代码时,该值仅显示为空白。有什么建议吗?

【问题讨论】:

  • 你考虑过System.ComponentModel.DataAnnotations中的属性吗?如果您创建了一个包含成员的类并应用了 DisplayAttribute,那么您的输出代码就可以使用这些属性来抽出正确的标签。

标签: c# asp.net forms controls


【解决方案1】:
string str = "";
foreach (string item in list)
{
    TextBox txt = (TextBox)Form.FindControl(item);
    if (txt != null)
    {
        str += item+":"+ txt.Text+"<br>";
    }
}
Response.Write(str);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-04
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    相关资源
    最近更新 更多