Link to home
Start Free TrialLog in
Avatar of shuittny
shuittny

asked on

displaying the id of a dropdownlist on a page

Is it possible to create a procedure (OnSelectedIndexChanged) so that 5 or more dropdowns can call it and for the procedure to know which dropdown is calling it

ex.

<asp:DropDownList id="drop1" runat="server" OnSelectedIndexChanged="callPro">

<asp:DropDownList id="drop2" runat="server" OnSelectedIndexChanged="callPro">

<asp:DropDownList id="drop3" runat="server" OnSelectedIndexChanged="callPro">

...etc......

sub callPro(sender As Object, e As EventArgs)
      'need to display what the id of the dropdownlist is.......
end sub
ASKER CERTIFIED SOLUTION
Avatar of william007
william007

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_sansanwal
s_sansanwal

You need to type cast the sender object to the dropdownlist object, see the code below:

Public Sub callPro(ByVal sender As Object, ByVal e As EventArgs)
       Dim selectedList as DropDownList  = CType(Sender, DropDownList )
      Response.Write(selectedList .ID)
End Sub

Cheers,
S Sansanwal
s_sansanwal, Why we need to cast to dropdownlist? any advantage?
because you could not use Sender.Id, as at this stage complier don't know what type of object sender is. The data type of sender at this stage is "object" and object type don't have an ID property.
Hope this helps.

Cheers,
S Sansanwal
Hi!~s_sansanwal,
But actually I have tested out and it works, what do you think of?
I like to hear your comment..
In VB.NET, if you do not set Option Strict to True, you can get away with blue murder like what william007 has done for inline scripts. Against every OO principle and an argument against not using VB.NET because it actually allows you to be sloppy.
At first I also feel quite strange after reminding by s_sansanwal as this do not comply with the OO concept that I learn in Java, now I know what is going on. Thks to b1xml2:)
LOL =))
Just one more minor detail... you can save yourself declaring a purely temporary variable by using this code:

Response.Write(CType(Sender, DropDownList ).ID)