【问题标题】:How to get the "ID" of element by the name with asp.net如何通过 asp.net 的名称获取元素的“ID”
【发布时间】:2013-11-06 21:29:37
【问题描述】:

我有一个通用代码,可以在帖子值上创建一个循环。我需要获取发布数据的元素的 id。

例如,如果 Html 源是:

<form id="form1" runat="server">
   <input id="Name" type="text" name="Full Name" runat="server" />
   <input id="Email" type="text" name="Email Address" runat="server" />
   <input id="Phone" type="text" name="Phone Number" runat="server" />
</form>

C#代码:

for (int i = 1; i < Request.Form.Count; ++i)
{
    string Value = Request.Form[i];
    if (Value != "")
    {
        string ControlName = Request.Form.Keys[i];
        string ControlId = ""; // Here I need to find the Id
    }
}

如果 ControlName 是:“Full Name”,在 ControlId 中我需要获取:“Name”等。

欢迎任何建议。

【问题讨论】:

标签: c# asp.net forms post


【解决方案1】:

Request.Form 只返回 NameValueCollection

如果要将表单内的控件检索为服务器控件,则需要使用 Page.Form

foreach (var control in Page.Form.Controls)
{
    if (control is HtmlInputControl)
    {
        var htmlInputControl = control as HtmlInputControl;
        string controlName = htmlInputControl.Name;
        string controlId = htmlInputControl.ID;
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-27
    • 2013-03-30
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多