如何在ASP .NET中的指定控件中查找所有表单项

时间:2009-11-06 20:16:37

标签: c# .net asp.net .net-3.5

我使用带有ListView的ASP .NET,在某一点上,我需要遍历每个ListViewDataItem并找到其中的所有表单项,然后从所述表单项中提取值。

这里是我想要做的一般概念:

foreach (ListViewDataItem listItem in MyList.Items) {
    foreach (FormItem formItem in listItem){
        //somehow in here i need to find the type of the item (i.e. textbox, dropdown etc)        
        //then i need to pull the value from them
    }
}

3 个答案:

答案 0 :(得分:1)

已修改以包含表单值...

也许是这样的?

    protected void Page_Load(object sender, EventArgs e)
    {
        var allFormControls = new Dictionary<Control, string>();
        GetControls(this.Controls, allFormControls);
    }

    private static void GetControls(ControlCollection controlCollection, IDictionary<Control, string> allFormControls)
    {



        foreach (Control control in controlCollection)
        {
            if (control is TextBox)
                allFormControls.Add(control, ((TextBox) control).Text);

            GetControls(control.Controls, allFormControls);
        }
    }

反对以下内容:

<asp:Table runat="server">
        <asp:TableRow runat="server">
            <asp:TableCell runat="server">
                <asp:TextBox runat="server">foo
                </asp:TextBox>
                <asp:Button runat="server" />
                <asp:Table ID="Table1" runat="server">
                    <asp:TableRow ID="TableRow1" runat="server">
                        <asp:TableCell ID="TableCell1" runat="server">
                            <asp:TextBox ID="TextBox1" runat="server">bar
                            </asp:TextBox>
                            <asp:Button ID="Button1" runat="server" />
                        </asp:TableCell>
                        <asp:TableCell>
                            <asp:Table ID="Table2" runat="server">
                                <asp:TableRow ID="TableRow2" runat="server">
                                    <asp:TableCell ID="TableCell2" runat="server">
                                        <asp:TextBox ID="TextBox2" runat="server">hello
                                        </asp:TextBox>
                                        <asp:Button ID="Button2" runat="server" />
                                    </asp:TableCell>
                                    <asp:TableCell>
                                        <asp:Table ID="Table3" runat="server">
                                            <asp:TableRow ID="TableRow3" runat="server">
                                                <asp:TableCell ID="TableCell3" runat="server">
                                                    <asp:TextBox ID="TextBox3" runat="server">world
                                                    </asp:TextBox>
                                                    <asp:Button ID="Button3" runat="server" />
                                                </asp:TableCell>
                                            </asp:TableRow>
                                        </asp:Table>
                                    </asp:TableCell>
                                </asp:TableRow>
                            </asp:Table>
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>

结果:

allFormControls
Count = 4
    [0]: {[System.Web.UI.WebControls.TextBox, foo
                    ]}
    [1]: {[System.Web.UI.WebControls.TextBox, bar
                                ]}
    [2]: {[System.Web.UI.WebControls.TextBox, hello
                                            ]}
    [3]: {[System.Web.UI.WebControls.TextBox, world
                                                        ]}

答案 1 :(得分:0)

Private Sub LoopControls(ByVal cntrl As Control)
   If cntrl.Controls.Count > 0 Then
       For Each child As Control In cntrl.Controls
            LoopControls(child)
        Next
   Else
       If TypeOf cntrl Is Textbox Then
          Dim txtBox as TextBox = CType(cntrl, TextBox)
          Dim myVal as String = txtBox.Text
       End If
   End If
End Sub

编辑:抱歉VB,但这是我的主要语言,现在没时间转换为C#,所以你必须将这个例程转换为C#,但它应该让你知道如何处理它。基本上你会调用这个函数,传入父控件,它将递归循环遍历所有子控件和他们拥有的任何子项。

答案 2 :(得分:0)

我会倾向于类似下面的例子。

protected void Page_Load(object sender, EventArgs e)
{
    foreach(var control in GetControls(Controls))
    {
        var textBox = control as TextBox;
        if ( textBox != null )
        {
            //textBox.Text;
            continue;
        }

        var dropdown = control as DropDownList;
        if ( dropdown != null )
        {
            //dropdown.SelectedValue;
            continue;
        }

        // etc...
    }
}

private static IEnumerable<Control> GetControls(ControlCollection controlCollection)
{
    foreach (Control control in controlCollection)
    {
        yield return control;

        if ( control.Controls == null || control.Controls.Count == 0 )
            continue;

        foreach (var sub in GetControls(control.Controls))
        {
            yield return sub;
        }
    }
}