By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,813 Members | 2,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,813 IT Pros & Developers. It's quick & easy.

DropDownList control and OnSelectedIndexChanged event

P: n/a
Hi folks,

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

I have posted an OnSelectedIndexChanged subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownList
id="ddlTo"
runat="server"
DataValueField="cname"
AutoPostBack="True"
DataSource='<%# GetCompanyNamesForThisProject() %>'

OnSelectedIndexChanged="ddlTo_SelectedIndexChanged "

/>

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

Sub ddlTo_SelectedIndexChanged(sender 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:DropDownList tag.

Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedIndex
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
Share this Question
Share on Google+
2 Replies


P: n/a
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(sender As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
End Sub 'Button_Click

Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = DropDownList1.SelectedIndex
Response.Write(indexNum)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>DropDownList Example
</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged"
AutoPostBack="True">
<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:DropDownList>
<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 OnSelectedIndexChanged subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownList
id="ddlTo"
runat="server"
DataValueField="cname"
AutoPostBack="True"
DataSource='<%# GetCompanyNamesForThisProject() %>'

OnSelectedIndexChanged="ddlTo_SelectedIndexChanged "

/>

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

Sub ddlTo_SelectedIndexChanged(sender 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:DropDownList tag.

Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedIndex
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

P: n/a
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_SelectedIndexChanged(object sender, e EventArgs)
{
DropDownList MyDDL = (DropDownList)sender;
}

MyDDL is a reference to the DropDownList control that triggered the
SelectedIndexChanged 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)sender
is something like CType(sender).

DataGrid.FindControl("DropdownListID");
"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(sender As Object, e As EventArgs)
Label1.Text = "You selected " & DropDownList1.SelectedItem.Text & "."
End Sub 'Button_Click

Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = DropDownList1.SelectedIndex
Response.Write(indexNum)
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<h3>DropDownList Example
</h3>
Select an item from the list and click the submit button.
<p>
<asp:DropDownList id="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged"
AutoPostBack="True">
<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:DropDownList>
<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 OnSelectedIndexChanged subroutine in my code with a
reference to the subroutine in my asp tag as shown here:
<asp:DropDownList
id="ddlTo"
runat="server"
DataValueField="cname"
AutoPostBack="True"
DataSource='<%# GetCompanyNamesForThisProject() %>'

OnSelectedIndexChanged="ddlTo_SelectedIndexChanged "

/>

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

Sub ddlTo_SelectedIndexChanged(sender 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:DropDownList tag.

Sub ddlTo_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim indexNum as integer
indexNum = ddlTo.SelectedIndex
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 discussion thread is closed

Replies have been disabled for this discussion.