Click here to Skip to main content
16,016,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to pull a value out of a Drop Down List on my ASP page.

Here is the markup for the page itself:

ASP.NET
<asp:DropDownList ID="ddlCurrentIncidentStatus" runat="server" Height="37px" SelectedValue='<%# Bind("Current_Incident_Status") %>' Width="347px">
                                    <asp:ListItem>Please select one...</asp:ListItem>
                                    <asp:ListItem>Contained</asp:ListItem>
                                    <asp:ListItem>Uncontained</asp:ListItem>
                                    <asp:ListItem>Investigating</asp:ListItem>
                                    <asp:ListItem>Other (Explained in Incident Description)</asp:ListItem>
                                </asp:DropDownList>


Here is the Code Behind for the control:

C#
DropDownList CurrentIncidentStatus = (DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus");
                string currentIncidentStatus = CurrentIncidentStatus.Text.ToString();


I'm getting this error:

Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'
Posted

Hi,

I don't think a DropDownList has a Text property, modify it to SelectedValue as per below.

C#
DropDownList CurrentIncidentStatus = (DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus");
string currentIncidentStatus = CurrentIncidentStatus.SelectedValue;


Hope it helps.
 
Share this answer
 
Comments
DamithSL 25-Jun-14 11:51am    
this is not the reason for the error. All List controls having Text property. check the documentation
(DropDownList)FormView1.FindControl("ddlCurrentIncidentStatus"); will not throw
Quote:
Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'

Error is on some other code, check all placess where you find controls and casting. for example, you may have search for DropDownList and casting to TextBox like (TextBox)FormView1.FindControl("ddl....
Find and correct the costing.
 
Share this answer
 
v2
Comments
Branden Coker 25-Jun-14 12:04pm    
Ok. I will search through my code for the casting. Thanks for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900