• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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
0
shuittny
Asked:
shuittny
  • 4
  • 2
  • 2
  • +1
1 Solution
 
william007Commented:
in presentation file
        <asp:DropDownList id="drop1" runat="server" OnSelectedIndexChanged="callPro" AutoPostBack=True>
                <asp:ListItem Value="dd" Selected=True></asp:ListItem>
                <asp:ListItem Value="dd1"></asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList id="drop2" runat="server" OnSelectedIndexChanged="callPro" AutoPostBack=True>
                <asp:ListItem Value="dd" Selected=True></asp:ListItem>
                <asp:ListItem Value="dd1"></asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList id="drop3" runat="server" OnSelectedIndexChanged="callPro" AutoPostBack=True>
                <asp:ListItem Value="dd" Selected=True></asp:ListItem>
                <asp:ListItem Value="dd1"></asp:ListItem>
            </asp:DropDownList>

in codebehindfile

    Public Sub callPro(ByVal sender As Object, ByVal e As EventArgs)
        Response.Write(sender.id)
    End Sub
0
 
s_sansanwalCommented:
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
0
 
william007Commented:
s_sansanwal, Why we need to cast to dropdownlist? any advantage?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
s_sansanwalCommented:
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
0
 
william007Commented:
Hi!~s_sansanwal,
But actually I have tested out and it works, what do you think of?
I like to hear your comment..
0
 
b1xml2Commented:
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.
0
 
william007Commented:
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:)
0
 
b1xml2Commented:
LOL =))
0
 
Jeff CertainCommented:
Just one more minor detail... you can save yourself declaring a purely temporary variable by using this code:

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now