DropDownList control and OnSelectedIndexChanged event

G

Guest

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
 
G

Guest

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_SelectedIndexChanged"
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>
 
G

Guest

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");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top