473,856 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DropDownList control and OnSelectedIndex Changed event

Hi folks,

I am trying to determine which item in a DropDownList Web control has been
selected.

I have posted an OnSelectedIndex Changed subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownLi st
id="ddlTo"
runat="server"
DataValueField= "cname"
AutoPostBack="T rue"
DataSource='<%# GetCompanyNames ForThisProject( ) %>'

OnSelectedIndex Changed="ddlTo_ SelectedIndexCh anged"

/>

My subroutine has a Response.Write inside to see if the event is fired as
shown here:

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Response.Write ("your dropdownlist event has fired")
End Sub

I successfully have fired the event when I select an item in the
dropdownlist control where the text "your dropdownlist event has fired"
writes to the screen.

Unfortunately, when I attempt to get the index number of the item I
selected, I get an error stating that ddlTo is not declared. ddlTo is the id
that was set in the <asp:DropDownLi st tag.

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedI ndex
Response.Write( indexNum )
End Sub

It seems to me that ddlTo was declared in the asp tag as the id. How else
should I be declaring ddlTo?

Thanks for any help on this...
Glenn

Apr 21 '06 #1
2 16105
Hello again folks,

Another thought I had is that this DropDownList control is in a DataGrid
control and is not visible until an Edit link (in the same row is clicked).
Therefore, the control is written out instead of double clicking it in design
mode that automatically writes the code for the Sub and End Sub statements.

Not sure if this changes things but I have tested a small sample with code
shown here. Oddly enough, the sample code runs without any problem. Go
figure ??

<%@ Page Language="vb" %>
<script runat="server">

Sub Button_Click(se nder As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.S electedItem.Tex t & "."
End Sub 'Button_Click

Sub DropDownList1_S electedIndexCha nged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = DropDownList1.S electedIndex
Response.Write( indexNum)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>DropDownLis t Example
</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownLi st id="DropDownLis t1" runat="server"
OnSelectedIndex Changed="DropDo wnList1_Selecte dIndexChanged"
AutoPostBack="T rue">
<asp:ListItem Value="Item 1">Item 1</asp:ListItem>
<asp:ListItem Value="Item 2">Item 2</asp:ListItem>
<asp:ListItem Value="Item 3">Item 3</asp:ListItem>
<asp:ListItem Value="Item 4">Item 4</asp:ListItem>
</asp:DropDownLis t>
<br />
<br />
<asp:Button id="Button1" onclick="Button _Click" runat="server"
Text="Submit"></asp:Button>
<br />
<br />
<asp:Label id="Label1" runat="server"> </asp:Label>
</p>
</form>
</body>
</html>


"glenn" wrote:
Hi folks,

I am trying to determine which item in a DropDownList Web control has been
selected.

I have posted an OnSelectedIndex Changed subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownLi st
id="ddlTo"
runat="server"
DataValueField= "cname"
AutoPostBack="T rue"
DataSource='<%# GetCompanyNames ForThisProject( ) %>'

OnSelectedIndex Changed="ddlTo_ SelectedIndexCh anged"

/>

My subroutine has a Response.Write inside to see if the event is fired as
shown here:

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Response.Write ("your dropdownlist event has fired")
End Sub

I successfully have fired the event when I select an item in the
dropdownlist control where the text "your dropdownlist event has fired"
writes to the screen.

Unfortunately, when I attempt to get the index number of the item I
selected, I get an error stating that ddlTo is not declared. ddlTo is the id
that was set in the <asp:DropDownLi st tag.

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedI ndex
Response.Write( indexNum )
End Sub

It seems to me that ddlTo was declared in the asp tag as the id. How else
should I be declaring ddlTo?

Thanks for any help on this...
Glenn

Apr 21 '06 #2
Controls within a DataGrid only have a scope within that datagrid, to access
the control outside of that scope you have to use the find control method of
the parent control and cast the return value corect type for the child
control. In you case though because you want to reference the DropDownList
from within it's own event handler you could use the sender object within the
event handler.

In C# you would do something like the following

protected void ddlTo_SelectedI ndexChanged(obj ect sender, e EventArgs)
{
DropDownList MyDDL = (DropDownList)s ender;
}

MyDDL is a reference to the DropDownList control that triggered the
SelectedIndexCh anged event and you can treat it like any other DDL. Sorry i
don't really speak VB, but i think the Vb equivelent of (DropDownList)s ender
is something like CType(sender).

DataGrid.FindCo ntrol("Dropdown ListID");
"glenn" wrote:
Hello again folks,

Another thought I had is that this DropDownList control is in a DataGrid
control and is not visible until an Edit link (in the same row is clicked).
Therefore, the control is written out instead of double clicking it in design
mode that automatically writes the code for the Sub and End Sub statements.

Not sure if this changes things but I have tested a small sample with code
shown here. Oddly enough, the sample code runs without any problem. Go
figure ??

<%@ Page Language="vb" %>
<script runat="server">

Sub Button_Click(se nder As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.S electedItem.Tex t & "."
End Sub 'Button_Click

Sub DropDownList1_S electedIndexCha nged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = DropDownList1.S electedIndex
Response.Write( indexNum)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>DropDownLis t Example
</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownLi st id="DropDownLis t1" runat="server"
OnSelectedIndex Changed="DropDo wnList1_Selecte dIndexChanged"
AutoPostBack="T rue">
<asp:ListItem Value="Item 1">Item 1</asp:ListItem>
<asp:ListItem Value="Item 2">Item 2</asp:ListItem>
<asp:ListItem Value="Item 3">Item 3</asp:ListItem>
<asp:ListItem Value="Item 4">Item 4</asp:ListItem>
</asp:DropDownLis t>
<br />
<br />
<asp:Button id="Button1" onclick="Button _Click" runat="server"
Text="Submit"></asp:Button>
<br />
<br />
<asp:Label id="Label1" runat="server"> </asp:Label>
</p>
</form>
</body>
</html>


"glenn" wrote:
Hi folks,

I am trying to determine which item in a DropDownList Web control has been
selected.

I have posted an OnSelectedIndex Changed subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownLi st
id="ddlTo"
runat="server"
DataValueField= "cname"
AutoPostBack="T rue"
DataSource='<%# GetCompanyNames ForThisProject( ) %>'

OnSelectedIndex Changed="ddlTo_ SelectedIndexCh anged"

/>

My subroutine has a Response.Write inside to see if the event is fired as
shown here:

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Response.Write ("your dropdownlist event has fired")
End Sub

I successfully have fired the event when I select an item in the
dropdownlist control where the text "your dropdownlist event has fired"
writes to the screen.

Unfortunately, when I attempt to get the index number of the item I
selected, I get an error stating that ddlTo is not declared. ddlTo is the id
that was set in the <asp:DropDownLi st tag.

Sub ddlTo_SelectedI ndexChanged(sen der As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedI ndex
Response.Write( indexNum )
End Sub

It seems to me that ddlTo was declared in the asp tag as the id. How else
should I be declaring ddlTo?

Thanks for any help on this...
Glenn

Apr 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1782
by: PK9 | last post by:
Please help: I have a DropdownList control that has a server-side onSelectedIndexChanged event handler. Before this server-side event handler is run, I'd like a client-side javascript confirmation box to appear for the user to select whether or not to continue. I've tried adding an onselectedindexchanged event handler in the Page.Load, I've tried Page.RegisterStartupScripts, but I can't figure it out. Here's my code: ASPX Page:
2
17039
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of the columns of the data grid contains a DropDownlist. I managed to create this datagrid control as follows.
2
2051
by: Ali | last post by:
I am adding a DropDownList control to my DataGrid footer template. I load my DropDownList using a function that returns an arrayList. I can see all my items in the DropDownList, but when I select an item from the list, I am unable to trap the DropDownList SelectedIndexChanged event. This is a snippet of my DataGrid: <asp:TemplateColumn> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "State") %>
6
912
by: Roshawn | last post by:
Hi, I have a DDL server control on my web form. I'm having difficulty trying to fire the OnSelectedIndexChanged event. I've specified a procedure to handle the event. Whenever I changed the selected index of the DDL server control, the event doesn't fire at all. Below are the settings for the DDL server control: <asp:dropdownlist autopostback="true" id="Dlist" runat="server" onselectedindexchanged="Selection_Changed" />
2
8643
by: Alexander Reichman | last post by:
Hi All, I have 22 years of over all experience in programming but just recently started to learn WEB applications, and started to write my first application in ASP.NET(VB) using VISUAL Studio .NET 2003
1
4457
by: Paul L | last post by:
Hi, I have an issue with the OnSelectedIndexChanged event not firing for a DropDownList control which is in the ItemTemplate of a DataList. I have made an exact copy of the DropDownList control, and placed it outside of the DataList and it fires the event just fine. So it's definitely to do with it being in a template. I've managed to reproduce the issue on a small test app, the code and html is below:
8
3396
by: utterberg | last post by:
Hi I'm adding two dropdownlist in my webform by clicking a button. I fill the first dropdown with some values from my DB and the other one when I choose from the first dropdown. But since theese dropdownlists are created by clicking a button they no longer exists on my webform. How do I keep my dynamically created dropdown when using OnSelectedIndexChanged? Can anyone help me solve this problem? Or is there a better way of doing this?
15
3142
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
2
3308
by: jnoody | last post by:
The problem I am having is with the SelectedIndexChanged event not always firing or the SelectedIndex property not being correct when the event does fire. The code is below, but here are some details first. The DropDownList is actually a custom control called DropDownListWithCommandEvent that inherits from DropDownList. The reason I have created this is to create a DropDownList that will bubble a Command event to the containing...
0
9918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10786
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10389
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9532
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4578
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4175
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.