I ma using ASP .NET with a ListView, at one point, I need to go through each ListViewDataItem and find all of the form items within it, then pull the values from said form items.
here is a general idea of what i want to do:
foreach (ListViewDataItem listItem in MyList.Items) {
foreach (FormItem formItem in listItem){
//somehow in here i need t开发者_如何学Co find the type of the item (i.e. textbox, dropdown etc)
//then i need to pull the value from them
}
}
Edited to include form value...
Maybe something like this?
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);
}
}
Ran against the following:
<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>
Result:
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
]}
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
EDIT: Sorry about the VB, but that is my primary language and don't have the time right now to convert to C#, so you would have to convert this routine to C#, but it should give you an idea of how to handle that. Basically you would call this function, passing in the parent control and it would recursively loop through all child controls and any children they had.
I would lean toward something like the following example.
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;
}
}
}
精彩评论