Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList control and OnSelectedIndexChanged event

Reply
Thread Tools

DropDownList control and OnSelectedIndexChanged event

 
 
=?Utf-8?B?Z2xlbm4=?=
Guest
Posts: n/a
 
      04-21-2006
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:
<aspropDownList
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 <aspropDownList 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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Z2xlbm4=?=
Guest
Posts: n/a
 
      04-21-2006
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>
<aspropDownList 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>
</aspropDownList>
<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:
> <aspropDownList
> 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 <aspropDownList 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
>

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Y2xpY2tvbg==?=
Guest
Posts: n/a
 
      04-21-2006
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>
> <aspropDownList 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>
> </aspropDownList>
> <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:
> > <aspropDownList
> > 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 <aspropDownList 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
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdownlist onselectedIndexChanged event not fires Alexander Reichman ASP .Net 3 12-01-2009 04:14 PM
dropdownlist - hooking into the onselectedindexchanged event renders onchange html attribute TS ASP .Net Building Controls 2 07-30-2007 01:46 PM
How do I keep dynamically created dropdownlist when event OnSelectedIndexChanged? utterberg@gmail.com ASP .Net 8 08-16-2005 07:50 PM
OnSelectedIndexChanged event not firing on a DropDownList within a DataList Paul L ASP .Net 1 05-06-2005 12:12 AM
OnSelectedIndexChanged event not firing on a DropDownList within a DataList Paul L ASP .Net Web Controls 0 05-05-2005 08:58 PM



Advertisments