I have 15 drop downs on a page - Name01 to Name15.
I'm trying to loop through each one, get the value of it, put it in avariable and then pass that to a function that will update a field in adatabase.
Can't get my head around looping through and getting the value out of the drop downs and putting in a variable to be passed.
Thanks,
A nice feature about .NET is that each control is a child to the Page control
This article nicely explains looping through the controls inside a Pagehttp://aspalliance.com/642
You can use this piece of code to loop thru all the dropdownlists on your page:
private void LoopThru(){foreach (Control ctrin Page.Controls) {if (ctris DropDownList) {string selValue = ((DropDownList)(ctr)).SelectedValue; } }}Your solution will be something like this:
private void GetDropDownValue(){foreach (Control ctrlin Page.Controls) {if (ctrlis DropDownList) { DropDownList d = (DropDownList)ctrl;switch(d.ID) {case"ID1":break;case"ID2":break; } } }}
hth
(This solution is very similar to the link that datagrid posted)
Using VB...I've put in the following code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Name_temp As String = ""
For Each ctrl As Control In Page.Controls
If TypeOf ctrl Is DropDownList Then
Dim ddl As DropDownList = ctrl
If Not ddl.SelectedValue Is Nothing Then
Select Case ctrl.ID
Case "Name01"
Name_temp = Name01.SelectedValue
Case "Name02"
Name_temp = Name02.SelectedValue
Case "Name03"
Name_temp = Name03.SelectedValue
End Select
End If
If Name_temp <> "" Then
do something with Name_temp
End If
End If
Next
End Sub
However, while looping through the controls, it never recognizes any dropdownlists. It only finds a bunch of LiteralControls. Any thoughts?
What is the markup of your aspx.page?
<%@. Page Language="VB" AutoEventWireup="false" CodeFile="PlayerUpdate.aspx.vb" Inherits="PlayerUpdate" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Update Games<br />
<br />
Team:
<asp:DropDownList ID="DropDownListTeam" runat="server" AutoPostBack="True">
<asp:ListItem>Team1</asp:ListItem>
<asp:ListItem>Team2</asp:ListItem>
<asp:ListItem>Team3</asp:ListItem>
<asp:ListItem>Team4</asp:ListItem>
<asp:ListItem>Team5</asp:ListItem>
<asp:ListItem>Team6</asp:ListItem>
</asp:DropDownList><br />
<br />
<br />
Players<br />
<asp:DropDownList ID="Name01" runat="server" DataSourceID="SqlDataSourcePlayer" DataTextField="Last_name"
DataValueField="Last_name" Width="201px">
</asp:DropDownList><br />
<asp:DropDownList ID="Name02" runat="server" DataSourceID="SqlDataSourcePlayer" DataTextField="Last_name"
DataValueField="Last_name" Width="201px">
</asp:DropDownList><br />
<asp:DropDownList ID="Name03" runat="server" DataSourceID="SqlDataSourcePlayer" DataTextField="Last_name"
DataValueField="Last_name" Width="201px">
</asp:DropDownList><br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Update" /><br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label" Width="213px"></asp:Label><br />
</div>
<asp:SqlDataSource ID="SqlDataSourcePlayer" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Player_id], [Last_name], [First_name] FROM [Players_07] WHERE Team = @.Team">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListTeam" Name="Team" PropertyName="SelectedValue" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Instead of Page.Controls, try Form.Controls
That did the trick, thanks.
I just replaced Page.Controls to Form.Controls
Here is a working code snippet
ProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.ClickDim Name_tempAsString =""
ForEach ctrlAs ControlIn Form.ControlsIfTypeOf ctrlIs DropDownListThen
Dim ddlAs DropDownList = ctrlIfNot ddl.SelectedValueIsNothingThen
SelectCase ctrl.IDCase"Name01"
Name_temp = Name01.SelectedValue
Case"Name02"
Name_temp = Name02.SelectedValue
Case"Name03"
Name_temp = Name03.SelectedValue
EndSelect
EndIf
If Name_temp <>""Then
Response.Write("test")EndIf
EndIf
Next
EndSub
aspx page that I used:
<asp:ButtonID="Button1"runat="server"/>
<asp:DropDownListID="Name01"runat="server">
<asp:ListItemText="1"Value="1"></asp:ListItem><asp:ListItemText="2"Value="2"Selected="True"></asp:ListItem>
<asp:ListItemText="3"Value="3"></asp:ListItem></asp:DropDownList>
<asp:DropDownListID="Name02"runat="server">
<asp:ListItemText="1"Value="1"></asp:ListItem><asp:ListItemText="2"Value="2"Selected="True"></asp:ListItem>
<asp:ListItemText="3"Value="3"></asp:ListItem></asp:DropDownList>
<asp:DropDownListID="Name03"runat="server">
<asp:ListItemText="1"Value="1"></asp:ListItem><asp:ListItemText="2"Value="2"Selected="True"></asp:ListItem>
<asp:ListItemText="3"Value="3"></asp:ListItem></asp:DropDownList>
0 comments:
Post a Comment