Get ID/Value in one Sub handling multiple SelectedIndexChanged events?

M

MLecrone

Please pardon my ignorance if this is an obvious question to you more
experienced .Netters! I'm sure it only has to do with referencing the
right class (or containing object?), but I've been unsuccessful thus
far.

I have a single subroutine that handles multiple SelectedIndexChanged
events:

Private Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ddlEmployeeType.SelectedIndexChanged,
ddlEmployeeName.SelectedIndexChanged, ddlGroups.SelectedIndexChanged,
ddlWorkCenter.SelectedIndexChanged, lstBuyers.SelectedIndexChanged

Inside this Sub I would then like to discover which control raised the
event along with the "selected index" of the control so that I can
then manipulate a dynamic SQL statement to return appropriate data.

The closest I've gotten so far is to grab sender.GetType.Name, but
obviously I don't care that the sender was a drop down list, I already
know that.

Any help or pointing me to an appropriate reference is much
appreciated.
 
K

Ken Cox [Microsoft MVP]

Hi Mel,

Although you have the Sender, you don't at that point know that it is a
dropdownlist. Therefore, you can't get much information about it. The trick
is to make an instance of a ddl and then get all kinds of info that you
want. I've put a sample below.

Let us know if this helps?

Ken
Microsoft MVP[ASP.NET]
Toronto


(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged, _
DropDownList2.SelectedIndexChanged, _
DropDownList3.SelectedIndexChanged
Dim ddlTester As DropDownList
ddlTester = sender
Label1.Text = "Postback by: " & _
ddlTester.ID & "<br>SelectIndex=" & _
ddlTester.SelectedIndex.ToString & _
"<br>Value=" & _
ddlTester.SelectedItem.Value.ToString
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="red">red</asp:ListItem>
<asp:ListItem Value="blue">blue</asp:ListItem>
<asp:ListItem Value="green">green</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:DropDownList id="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem Value="Top">Top</asp:ListItem>
<asp:ListItem Value="Middle">Middle</asp:ListItem>
<asp:ListItem Value="Bottom">Bottom</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:DropDownList id="DropDownList3" runat="server" AutoPostBack="True">
<asp:ListItem Value="Left">Left</asp:ListItem>
<asp:ListItem Value="Centre">Centre</asp:ListItem>
<asp:ListItem Value="Right">Right</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:Label id="Label1" runat="server">Label</asp:Label></P>
</form>
 
M

MLecrone

Ken, that looks to be exactly what I'm looking for! Thank you so much.
I really didn't want to create a separate sub to address each drop
down's SelectedIndexChanged events and this will ensure I don't have
to! Thanks again for your time and information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top