【问题标题】:Get all controls with attributes from Request.Form?从 Request.Form 获取所有具有属性的控件?
【发布时间】:2017-03-16 09:07:58
【问题描述】:

我想获取回发事件的所有输入控件。

这是我的示例控制:

 <input name="ctl00$ContentBody$dt_62f6f44864ec4c4892ac074da0209ff4
 type="text" value="11.06.2014"
 id="ContentBody_dt_62f6f44864ec4c4892ac074da0209ff4"  
 class="m-wrap span12 date form_datepicker form-control" 
 data-pagetype="main"
 data-groupname="group_DATE"
 data-rowindex="0" data-objtype="Datepicker"
 data-columnname="DATE_FROM" style="width:50px;">

处理所有键

public Collection<ActionContainer.RequestFormParameter> GetFormParameters()
{
   System.Collections.IEnumerator e2 = Request.Form.GetEnumerator();

   while (e2.MoveNext())
   {
       ActionContainer.RequestFormParameter params_;
       String xkey = (String)e2.Current; // output "ContentBody_dt_62f6f44864ec4c4892ac074da0209ff4"
       String xval = Request.Form.Get(xkey); // output "11.06.2014"
       String AttrCollection = ??

       // I try to find control by id but it didn't work for me
   }
}

【问题讨论】:

  • 这些输入标签是从客户端动态生成的,还是在设计时作为 TextBox 服务器控件已经在 ASPX 页面中?
  • name 看来,它们似乎已经在页面上。如果不是我的回答将无济于事......

标签: c# asp.net postback


【解决方案1】:

属性不包含在 Request.Form 集合中。因此,您将不得不使用 FindControl 来查找 Control 并访问它的属性。但由于您所拥有的只是控件名称 (key),因此您必须创造性地获取实际的 ID

假设您有一个 ID 为 dt_62f6f44864ec4c4892ac074da0209ff4 的文本框。在表单帖子中,它将类似于ctl00$ContentPlaceHolder1$dt_62f6f44864ec4c4892ac074da0209ff4,因此我们可以基于此再次提取ID

使用 ID 使用 FindControl 查找控件,将其转换为正确的控件类型并循环它的属性。

//loop all the items in the form collection
foreach (string key in Request.Form.Keys)
{
    //check if the key contains a $, in which case it is probably an aspnet control
    if (key.Contains("$"))
    {
        //split the control name
        string[] keyArrar = key.Split('$');

        //get the last part in the array, which should be the ID of the control
        string controlID = keyArrar[keyArrar.Length - 1];

        //try to find the control with findcontrol, in this case with master pages
        object control = this.Master.FindControl("mainContentPane").FindControl(controlID) as object;

        //check if the control exist and if it is a textbox
        if (control != null && control is TextBox)
        {
            //cast the object to the actual textbox
            TextBox tb = control as TextBox;

            //loop all the attributes of the textbox
            foreach (string attr in tb.Attributes.Keys)
            {
                //get the key and value of the attribute
                Response.Write(attr + ": " + tb.Attributes[attr] + "<br>");
            }
        }
    }
}

输出

data-pagetype: main
data-groupname: group_DATE
data-rowindex: 0
data-objtype: Datepicker
data-columnname: DATE_FROM

aspx 页面上的演示文本框。

<asp:TextBox ID="dt_62f6f44864ec4c4892ac074da0209ff4" runat="server"
    data-pagetype="main"
    data-groupname="group_DATE"
    data-rowindex="0"
    data-objtype="Datepicker"
    data-columnname="DATE_FROM">
</asp:TextBox>

如您所见,这是可以做到的,但是这是一种非常复杂的方式...

【讨论】:

  • 那里有问题对不起我通过了,我动态创建了控件,所以我不能使用 findcontrol
  • 没关系,您仍然可以将 FindControl 用于动态生成的按钮,只要它们在每个 PostBack 上重新生成并且在使用我的 sn-p 之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多