[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webcontrols

How do I retrieve the "Sender As Object" value for a dropdownlist?

(James)

12/25/2002 10:25:00 PM

Hi,

On my web form, I've got several dropdown lists that supply a default
value when the page is rendered. When the user selects a value from
these lists (because they are required), the SelectedIndexChanged
event fires, and processes my code for the control that called the
Sub.

So, I want to create one Sub that handles all the events for the
SelectedIndexChanged event of these controls, which will look like so:

Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
ByVal e as System.EventArgs) _
Handles <dropdownlist1>.SelectedIndexChanged, _
<dropdownlist2>.SelectedIndexChanged, _
<dropdownlist3>.SelectedIndexChanged, _
<dropdownlist4>.SelectedIndexChanged

' **** Process code here *****

End Sub

In each individual index changed event, I was explicitly referencing
each dropdown by the name, like this example:

cboDDL1.Items.RemoveAt(0)

With the method like I'm wanting to use, how do I get the calling
object's name from the sender object so I can use something more like:

<sender>.Items.RemoveAt(0)

This way, since the code that processes each dropdownlist is the same,
except for the calling object (the dropdownlist), I can simply use one
sub to process all of them, and pass in the name of the dropdownlist
and be able to reference the properties (hopefully) How can I do this?

Thanks --

James
3 Answers

(Patrick C. Cole (MS))

12/12/2002 4:10:00 PM

0

James,

Here is how I hooked up the events for the DropDownLists:

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()

Dim ctrlTemp As New Control()
Dim ddlTemp As New DropDownList()
For Each ctrlTemp In Me.FindControl("Form1").Controls
If ctrlTemp.GetType.ToString =
"System.Web.UI.WebControls.DropDownList" Then
ddlTemp = CType(ctrlTemp, DropDownList)
AddHandler ddlTemp.SelectedIndexChanged, AddressOf
DropDownLists_SelectedIndexChanged
End If
Next
End Sub


Private Sub DropDownLists_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim ddlTemp As New DropDownList()
ddlTemp = CType(sender, DropDownList)
Response.Write(ddlTemp.ID.ToString)
End Sub

In the Page_Init event, I loop through the controls on the page and wireup
the events for anything that is a DropDownList. You could certainly
explicitly handle the SelectedIndexChanged event by adding the multipl
Handle conditions. Whichever one works for you best is the one you should
use. After the event is wired, cast a DropDownList object to the incoming
"sender" object, and you will then have the context of the specific
DropDownList being modified.

Hope this helps,

Patrick Cole
Microsoft Developer Communities

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved.
--------------------
| From: dragonzfang@hotmail.com (James)
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Subject: How do I retrieve the "Sender As Object" value for a
dropdownlist?
| Date: 11 Dec 2002 10:30:04 -0800
| Organization: http://groups.g...
| Lines: 40
| Message-ID: <63468c52.0212111030.7ad55074@posting.google.com>
| NNTP-Posting-Host: 192.229.17.103
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1039631404 30154 127.0.0.1 (11 Dec 2002
18:30:04 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 11 Dec 2002 18:30:04 GMT
| Path:
cpmsftngxa06!cpmsftngxa10!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de
!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-
06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06
microsoft.public.dotnet.framework.aspnet.webcontrols:7728
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi,
|
| On my web form, I've got several dropdown lists that supply a default
| value when the page is rendered. When the user selects a value from
| these lists (because they are required), the SelectedIndexChanged
| event fires, and processes my code for the control that called the
| Sub.
|
| So, I want to create one Sub that handles all the events for the
| SelectedIndexChanged event of these controls, which will look like so:
|
| Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
| ByVal e as System.EventArgs) _
| Handles <dropdownlist1>.SelectedIndexChanged, _
| <dropdownlist2>.SelectedIndexChanged, _
| <dropdownlist3>.SelectedIndexChanged, _
| <dropdownlist4>.SelectedIndexChanged
|
| ' **** Process code here *****
|
| End Sub
|
| In each individual index changed event, I was explicitly referencing
| each dropdown by the name, like this example:
|
| cboDDL1.Items.RemoveAt(0)
|
| With the method like I'm wanting to use, how do I get the calling
| object's name from the sender object so I can use something more like:
|
| <sender>.Items.RemoveAt(0)
|
| This way, since the code that processes each dropdownlist is the same,
| except for the calling object (the dropdownlist), I can simply use one
| sub to process all of them, and pass in the name of the dropdownlist
| and be able to reference the properties (hopefully) How can I do this?
|
| Thanks --
|
| James
|

(Patrick C. Cole (MS))

12/13/2002 4:10:00 PM

0

James,

Glad you have a resolution to the issue.

You are correct, VB.NET will allow the compile without issuing the CType()
command on the "sender" object. I code more often in C#, which would not
allow the compile if you do not have the cast correct for the object. If
the "sender" object did not have a definition for the property/method you
need to use, it would error.

Let me know if I can help any further,

Patrick Cole
Microsoft Developer Communities

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. © 2002 Microsoft Corporation. All rights
reserved.
--------------------
| From: dragonzfang@hotmail.com (James)
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Subject: Re: How do I retrieve the "Sender As Object" value for a
dropdownlist?
| Date: 12 Dec 2002 13:38:30 -0800
| Organization: http://groups.g...
| Lines: 138
| Message-ID: <63468c52.0212121338.3bc470@posting.google.com>
| References: <63468c52.0212111030.7ad55074@posting.google.com>
<XIFGGCfoCHA.1008@cpmsftngxa09>
| NNTP-Posting-Host: 192.44.136.103
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1039729110 30827 127.0.0.1 (12 Dec 2002
21:38:30 GMT)
| X-Complaints-To: groups-abuse@google.com
| NNTP-Posting-Date: 12 Dec 2002 21:38:30 GMT
| Path:
cpmsftngxa06!cpmsftngxa10!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de
!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-xit-
09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06
microsoft.public.dotnet.framework.aspnet.webcontrols:7764
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Pat,
|
| I did finally get my idea to work -- it was much easier than I
| thought. I simply used:
|
| Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
| ByVal e as System.EventArgs) _
| Handles <dropdownlist1>.SelectedIndexChanged, _
| <dropdownlist2>.SelectedIndexChanged, _
| <dropdownlist3>.SelectedIndexChanged, _
| <dropdownlist4>.SelectedIndexChanged
|
| sender.Items.RemoveAt(0)
|
| End Sub
|
| This worked quite well -- apparently the editor doesn't know at design
| time what properties and methods are available on the object until at
| runtime. Neverthess, I have to admit that I like your method better. I
| will probably implement it later in my project.
|
| Thanks!
|
| James
|
|
| patcole@online.microsoft.com (Patrick C. Cole [MSFT]) wrote in message
news:<XIFGGCfoCHA.1008@cpmsftngxa09>...
| > James,
| >
| > Here is how I hooked up the events for the DropDownLists:
| >
| > 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()
| >
| > Dim ctrlTemp As New Control()
| > Dim ddlTemp As New DropDownList()
| > For Each ctrlTemp In Me.FindControl("Form1").Controls
| > If ctrlTemp.GetType.ToString =
| > "System.Web.UI.WebControls.DropDownList" Then
| > ddlTemp = CType(ctrlTemp, DropDownList)
| > AddHandler ddlTemp.SelectedIndexChanged, AddressOf
| > DropDownLists_SelectedIndexChanged
| > End If
| > Next
| > End Sub
| >
| >
| > Private Sub DropDownLists_SelectedIndexChanged(ByVal sender As
Object,
| > ByVal e As System.EventArgs)
| > Dim ddlTemp As New DropDownList()
| > ddlTemp = CType(sender, DropDownList)
| > Response.Write(ddlTemp.ID.ToString)
| > End Sub
| >
| > In the Page_Init event, I loop through the controls on the page and
wireup
| > the events for anything that is a DropDownList. You could certainly
| > explicitly handle the SelectedIndexChanged event by adding the multipl
| > Handle conditions. Whichever one works for you best is the one you
should
| > use. After the event is wired, cast a DropDownList object to the
incoming
| > "sender" object, and you will then have the context of the specific
| > DropDownList being modified.
| >
| > Hope this helps,
| >
| > Patrick Cole
| > Microsoft Developer Communities
| >
| > This posting is provided "AS IS" with no warranties, and confers no
rights.
| > You assume all risk for your use. © 2002 Microsoft Corporation. All
rights
| > reserved.
| > --------------------
| > | From: dragonzfang@hotmail.com (James)
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | Subject: How do I retrieve the "Sender As Object" value for a
| > dropdownlist?
| > | Date: 11 Dec 2002 10:30:04 -0800
| > | Organization: http://groups.g...
| > | Lines: 40
| > | Message-ID: <63468c52.0212111030.7ad55074@posting.google.com>
| > | NNTP-Posting-Host: 192.229.17.103
| > | Content-Type: text/plain; charset=ISO-8859-1
| > | Content-Transfer-Encoding: 8bit
| > | X-Trace: posting.google.com 1039631404 30154 127.0.0.1 (11 Dec 2002
| > 18:30:04 GMT)
| > | X-Complaints-To: groups-abuse@google.com
| > | NNTP-Posting-Date: 11 Dec 2002 18:30:04 GMT
| > | Path:
| >
cpmsftngxa06!cpmsftngxa10!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de
| >
!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-
| > 06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| > | Xref: cpmsftngxa06
| > microsoft.public.dotnet.framework.aspnet.webcontrols:7728
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi,
| > |
| > | On my web form, I've got several dropdown lists that supply a default
| > | value when the page is rendered. When the user selects a value from
| > | these lists (because they are required), the SelectedIndexChanged
| > | event fires, and processes my code for the control that called the
| > | Sub.
| > |
| > | So, I want to create one Sub that handles all the events for the
| > | SelectedIndexChanged event of these controls, which will look like so:
| > |
| > | Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
| > | ByVal e as System.EventArgs) _
| > | Handles <dropdownlist1>.SelectedIndexChanged, _
| > | <dropdownlist2>.SelectedIndexChanged, _
| > | <dropdownlist3>.SelectedIndexChanged, _
| > | <dropdownlist4>.SelectedIndexChanged
| > |
| > | ' **** Process code here *****
| > |
| > | End Sub
| > |
| > | In each individual index changed event, I was explicitly referencing
| > | each dropdown by the name, like this example:
| > |
| > | cboDDL1.Items.RemoveAt(0)
| > |
| > | With the method like I'm wanting to use, how do I get the calling
| > | object's name from the sender object so I can use something more like:
| > |
| > | <sender>.Items.RemoveAt(0)
| > |
| > | This way, since the code that processes each dropdownlist is the same,
| > | except for the calling object (the dropdownlist), I can simply use one
| > | sub to process all of them, and pass in the name of the dropdownlist
| > | and be able to reference the properties (hopefully) How can I do this?
| > |
| > | Thanks --
| > |
| > | James
| > |
|

(James)

12/25/2002 10:25:00 PM

0

Pat,

I did finally get my idea to work -- it was much easier than I
thought. I simply used:

Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
ByVal e as System.EventArgs) _
Handles <dropdownlist1>.SelectedIndexChanged, _
<dropdownlist2>.SelectedIndexChanged, _
<dropdownlist3>.SelectedIndexChanged, _
<dropdownlist4>.SelectedIndexChanged

sender.Items.RemoveAt(0)

End Sub

This worked quite well -- apparently the editor doesn't know at design
time what properties and methods are available on the object until at
runtime. Neverthess, I have to admit that I like your method better. I
will probably implement it later in my project.

Thanks!

James


patcole@online.microsoft.com (Patrick C. Cole [MSFT]) wrote in message news:<XIFGGCfoCHA.1008@cpmsftngxa09>...
> James,
>
> Here is how I hooked up the events for the DropDownLists:
>
> 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()
>
> Dim ctrlTemp As New Control()
> Dim ddlTemp As New DropDownList()
> For Each ctrlTemp In Me.FindControl("Form1").Controls
> If ctrlTemp.GetType.ToString =
> "System.Web.UI.WebControls.DropDownList" Then
> ddlTemp = CType(ctrlTemp, DropDownList)
> AddHandler ddlTemp.SelectedIndexChanged, AddressOf
> DropDownLists_SelectedIndexChanged
> End If
> Next
> End Sub
>
>
> Private Sub DropDownLists_SelectedIndexChanged(ByVal sender As Object,
> ByVal e As System.EventArgs)
> Dim ddlTemp As New DropDownList()
> ddlTemp = CType(sender, DropDownList)
> Response.Write(ddlTemp.ID.ToString)
> End Sub
>
> In the Page_Init event, I loop through the controls on the page and wireup
> the events for anything that is a DropDownList. You could certainly
> explicitly handle the SelectedIndexChanged event by adding the multipl
> Handle conditions. Whichever one works for you best is the one you should
> use. After the event is wired, cast a DropDownList object to the incoming
> "sender" object, and you will then have the context of the specific
> DropDownList being modified.
>
> Hope this helps,
>
> Patrick Cole
> Microsoft Developer Communities
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
> You assume all risk for your use. © 2002 Microsoft Corporation. All rights
> reserved.
> --------------------
> | From: dragonzfang@hotmail.com (James)
> | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
> | Subject: How do I retrieve the "Sender As Object" value for a
> dropdownlist?
> | Date: 11 Dec 2002 10:30:04 -0800
> | Organization: http://groups.g...
> | Lines: 40
> | Message-ID: <63468c52.0212111030.7ad55074@posting.google.com>
> | NNTP-Posting-Host: 192.229.17.103
> | Content-Type: text/plain; charset=ISO-8859-1
> | Content-Transfer-Encoding: 8bit
> | X-Trace: posting.google.com 1039631404 30154 127.0.0.1 (11 Dec 2002
> 18:30:04 GMT)
> | X-Complaints-To: groups-abuse@google.com
> | NNTP-Posting-Date: 11 Dec 2002 18:30:04 GMT
> | Path:
> cpmsftngxa06!cpmsftngxa10!tkmsftngp01!newsfeed00.sul.t-online.de!t-online.de
> !news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-
> 06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
> | Xref: cpmsftngxa06
> microsoft.public.dotnet.framework.aspnet.webcontrols:7728
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
> |
> | Hi,
> |
> | On my web form, I've got several dropdown lists that supply a default
> | value when the page is rendered. When the user selects a value from
> | these lists (because they are required), the SelectedIndexChanged
> | event fires, and processes my code for the control that called the
> | Sub.
> |
> | So, I want to create one Sub that handles all the events for the
> | SelectedIndexChanged event of these controls, which will look like so:
> |
> | Private Sub DropDownLists_SelectedIndexChanged(ByVal sender as Object,
> | ByVal e as System.EventArgs) _
> | Handles <dropdownlist1>.SelectedIndexChanged, _
> | <dropdownlist2>.SelectedIndexChanged, _
> | <dropdownlist3>.SelectedIndexChanged, _
> | <dropdownlist4>.SelectedIndexChanged
> |
> | ' **** Process code here *****
> |
> | End Sub
> |
> | In each individual index changed event, I was explicitly referencing
> | each dropdown by the name, like this example:
> |
> | cboDDL1.Items.RemoveAt(0)
> |
> | With the method like I'm wanting to use, how do I get the calling
> | object's name from the sender object so I can use something more like:
> |
> | <sender>.Items.RemoveAt(0)
> |
> | This way, since the code that processes each dropdownlist is the same,
> | except for the calling object (the dropdownlist), I can simply use one
> | sub to process all of them, and pass in the name of the dropdownlist
> | and be able to reference the properties (hopefully) How can I do this?
> |
> | Thanks --
> |
> | James
> |