-
How: Get value of dropdownlist in a loop?
Ok,
I can get the value of a dropdownlist buy doing this: dropdownlist1.selecteditem.value, i have left the default id for all the dropdownlists i have on the page. What I am trying to do is get the value in a loop. here is a simple example of what i am trying to do and what.
dim temp as string
for i=1 to 10
temp = dropdownlist(i).selecteditem.value
next
Obviously that didn't work so i tryed
temp = dropdownlist"+cstr(i)+".selecteditem.value
that also is not working, can anyone help.
Thank you,
Santana
-
I have enclosed a commonly used utility class ControlInfo that is very useful..
Use this as follows.
ASPX CODE
Code:
INSERT ASPX PAGE DIRECTIVE HERE!!!!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
<meta content="Visual Basic .NET 7.1" name=CODE_LANGUAGE>
<meta content=JavaScript name=vs_defaultClientScript>
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id=Form1 method=post runat="server"><asp:dropdownlist id=DropDownList1 style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 120px" runat="server" AutoPostBack="True">
<asp:ListItem Value="e">e</asp:ListItem>
<asp:ListItem Value="f">f</asp:ListItem>
</asp:dropdownlist><asp:dropdownlist id=DropDownList2 style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 160px" runat="server" AutoPostBack="True">
<asp:ListItem Value="c">c</asp:ListItem>
<asp:ListItem Value="d">d</asp:ListItem>
</asp:dropdownlist><asp:dropdownlist id=DropDownList3 style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 200px" runat="server" AutoPostBack="True">
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
</asp:dropdownlist><asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server" Text="Button"></asp:Button><asp:Button id="Button2" style="Z-INDEX: 105; LEFT: 24px; POSITION: absolute; TOP: 56px" runat="server" Text="Button"></asp:Button><asp:Label id="Label1" style="Z-INDEX: 106; LEFT: 112px; POSITION: absolute; TOP: 32px" runat="server">Label</asp:Label></FORM>
</body>
</HTML>
VB.NET code behind
Code:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
Protected WithEvents DropDownList3 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ctrlInfo As ControlInfo() = ControlUtil.TraverseControls(Me.Page, "System.Web.UI.WebControls.DropDownList")
Dim temp As String
For i As Integer = 1 To 3
For Each ctrl As ControlInfo In ctrlInfo
Dim ddl As DropDownList = CType(ctrl.Control, System.Web.UI.WebControls.DropDownList)
Dim sid As String = ddl.ID
Dim n As Integer = CInt(Mid(sid, Len("DropDownList") + 1, Len(sid) - Len("DropDownList")))
If n = i Then
temp = temp & "Drop down " & i & " has value=" & ddl.SelectedValue & "<br>"
End If
Next
Next
Label1.Text = temp
End Sub
End Class
Public Class ControlInfo
Private _ID As String
Private _type As Type
Private _control As System.Web.UI.Control
Public Sub New(ByVal id As String, ByVal type As Type, ByVal Control As Object)
_ID = id
_type = type
_control = Control
End Sub
Public Property ID() As String
Get
Return _ID
End Get
Set(ByVal Value As String)
_ID = Value
End Set
End Property
Public Property TheType() As Type
Get
Return _type
End Get
Set(ByVal Value As Type)
_type = Value
End Set
End Property
Public Property Control() As Object
Get
Return _control
End Get
Set(ByVal Value As Object)
_control = Value
End Set
End Property
Public ReadOnly Property TypeName() As String
Get
Return _type.ToString()
End Get
End Property
End Class
Public Class ControlUtil
Public Shared Function TraverseControls(ByVal root As System.Web.UI.Control, ByVal filter As Type) As ControlInfo()
If root.Controls.Count > 0 Then
Dim al As ArrayList = New ArrayList
Dim s As String = root.GetType().ToString
If Type.Equals(root.GetType(), filter) Then
al.Add(New ControlInfo(root.ID, root.GetType(), root))
End If
For Each myCtrl As System.Web.UI.Control In root.Controls
Dim tmpCtrlInfo As ControlInfo() = TraverseControls(myCtrl, filter)
If Not tmpCtrlInfo Is Nothing Then
al.AddRange(tmpCtrlInfo)
End If
Next
Return CType(al.ToArray(GetType(ControlInfo)), ControlInfo())
Else
If Type.Equals(root.GetType(), filter) Then
Return New ControlInfo() {New ControlInfo(root.ID, root.GetType(), root)}
Else
Return Nothing
End If
End If
End Function
Private Shared Function InFilter(ByVal ArrFilter() As String, ByVal Value As String) As Boolean
For i As Integer = 0 To ArrFilter.Length - 1
If Trim(LCase(ArrFilter(i))) = Trim(LCase(Value)) Then
Return True
End If
Next
Return False
End Function
Public Shared Function TraverseControls(ByVal root As System.Web.UI.Control, ByVal filterList As String) As ControlInfo()
Dim filter() As String
filter = Split(filterList, ",")
If root.Controls.Count > 0 Then
Dim al As ArrayList = New ArrayList
Dim s As String = root.GetType().ToString
If InFilter(filter, s) Then
al.Add(New ControlInfo(root.ID, root.GetType(), root))
End If
For Each myCtrl As System.Web.UI.Control In root.Controls
Dim tmpCtrlInfo As ControlInfo() = TraverseControls(myCtrl, filterList)
If Not tmpCtrlInfo Is Nothing Then
al.AddRange(tmpCtrlInfo)
End If
Next
Return CType(al.ToArray(GetType(ControlInfo)), ControlInfo())
Else
Dim s As String = root.GetType().ToString
If InFilter(filter, s) Then
Return New ControlInfo() {New ControlInfo(root.ID, root.GetType(), root)}
Else
Return Nothing
End If
End If
End Function
Public Shared Function TraverseControls(ByVal root As System.Web.UI.Control) As ControlInfo()
If root.Controls.Count > 0 Then
Dim al As ArrayList = New ArrayList
al.Add(New ControlInfo(root.ID, root.GetType(), root))
For Each myCtrl As System.Web.UI.Control In root.Controls
al.AddRange(TraverseControls(myCtrl))
Next
Return CType(al.ToArray(GetType(ControlInfo)), ControlInfo())
Else
Return New ControlInfo() {New ControlInfo(root.ID, root.GetType(), root)}
End If
End Function
End Class
-
Just a small note on this code
This seems a lot of code for such a small task - however, the general idea of iterating through the page controls recursively is a common method. The outer loop in the Page Load event is really not needed, and not too efficient, but it does what it says
-
What an interesting topic to me, I never considered such a thing before. I think I obtained a lot of knowledge from this thread towards the C# DropDown List control.
-
Great topic! I never know about drop-down list in a loop thank for tell now I should also apply this one
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|