views:

657

answers:

2

I have the below error message in my asp.net website when i replaced the textbox and put dropdown and named the dropdown id same as textbox id.

The value from the drop down should be taken and put in a email body and will be received as mail when i click on the submit button.

Below is the error i am getting when i click on the submit button.

System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.TextBox'. at WebApplication1._Default.collectEmailBodyText() in C:\v1.5_production_05June09\Default.aspx.vb:line 213

My code behind file is as follows :

''//------------------------------------------------------------------------------
''// <auto-generated>
''//     This code was generated by a tool.
''//     Runtime Version:2.0.50727.3053
''//
''//     Changes to this file may cause incorrect behavior and will be lost if
''//     the code is regenerated.
''// </auto-generated>
''//------------------------------------------------------------------------------

Option Strict On
Option Explicit On

Partial Public Class _Default

    Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

    Protected WithEvents MultiView1 As Global.System.Web.UI.WebControls.MultiView

    Protected WithEvents View0 As Global.System.Web.UI.WebControls.View

    Protected WithEvents lbl_viewTitle0 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents lbl_view0_firstName As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_firstName As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents Validator_FirstName As Global.System.Web.UI.WebControls.RequiredFieldValidator

    Protected WithEvents lbl_view0_surname As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txtSurName As Global.System.Web.UI.WebControls.TextBox    

    Protected WithEvents Validator_Surname As Global.System.Web.UI.WebControls.RequiredFieldValidator

    Protected WithEvents lbl_view0_ContactNum As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_contactNum As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents Validator_ContactNumber As Global.System.Web.UI.WebControls.RequiredFieldValidator

    Protected WithEvents lbl_view0_typeOfRequest As Global.System.Web.UI.WebControls.Label

    Protected WithEvents ddl_view0_typeOfRequest As Global.System.Web.UI.WebControls.DropDownList

    Protected WithEvents lbl_view0_workUnitLevel As Global.System.Web.UI.WebControls.Label

    Protected WithEvents ddl_view0_workUnitLevel As Global.System.Web.UI.WebControls.DropDownList

    Protected WithEvents btn_view0_forward As Global.System.Web.UI.WebControls.Button

    Protected WithEvents View1 As Global.System.Web.UI.WebControls.View

    Protected WithEvents lbl_viewTitle1 As Global.System.Web.UI.WebControls.Label  

    Protected WithEvents lbl_view1_firstName As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_view1_firstname As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents lbl_view1_surname As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_view1_surname As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents lbl_view1_userID As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_view1_userID As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents lbl_view1_workUnit As Global.System.Web.UI.WebControls.Label

    Protected WithEvents ddl_view1_workunit As Global.System.Web.UI.WebControls.DropDownList

    Protected WithEvents panview0 As Global.System.Web.UI.WebControls.Panel

    Protected WithEvents lbl_panView0_label1 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_panview0_input1 As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents panview2 As Global.System.Web.UI.WebControls.Panel

    Protected WithEvents lbl_panview2_label1 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_panview2_input1 As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents panview1 As Global.System.Web.UI.WebControls.Panel

    Protected WithEvents lbl_panview1_label1 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_panview1_input1 As Global.System.Web.UI.WebControls.TextBox   

    Protected WithEvents panview3 As Global.System.Web.UI.WebControls.Panel

    Protected WithEvents lbl_panview3_label1 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_panview3_input1 As Global.System.Web.UI.WebControls.DropDownList

    Protected WithEvents lbl_panview3_label2 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_panview3_input2 As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents btn_view1_back As Global.System.Web.UI.WebControls.Button

    Protected WithEvents btn_view1_forward As Global.System.Web.UI.WebControls.Button

    Protected WithEvents View2 As Global.System.Web.UI.WebControls.View

    Protected WithEvents lbl_viewTitle2 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents lbl_view2_ManagersEmailAddress As Global.System.Web.UI.WebControls.Label

    Protected WithEvents txt_view2_ManagersEmailAddress As Global.System.Web.UI.WebControls.TextBox

    Protected WithEvents rfv_view2_managersEmail As Global.System.Web.UI.WebControls.RequiredFieldValidator

    Protected WithEvents rev_view2_managersEmail As Global.System.Web.UI.WebControls.RegularExpressionValidator

    Protected WithEvents btn_view2_back As Global.System.Web.UI.WebControls.Button  

    Protected WithEvents btn_view02_forward As Global.System.Web.UI.WebControls.Button

    Protected WithEvents View3 As Global.System.Web.UI.WebControls.View

    Protected WithEvents lbl_viewTitle3 As Global.System.Web.UI.WebControls.Label

    Protected WithEvents lit_preview As Global.System.Web.UI.WebControls.Literal

    Protected WithEvents btn_view3_submit As Global.System.Web.UI.WebControls.Button

    Protected WithEvents btn_view3_back As Global.System.Web.UI.WebControls.Button
End Class

Please help me !

A: 

In your code behind, it looks like in your FindControls you're trying to handle a DropDownList as a TextBox. You should verify that the object definitions in your .aspx and code behind correspond in their declarations and in FindControls.

Darth Continent
Could you elaborate how to do this?
Well, for example, if in your .aspx page you have the following objects defined: <asp:DropDownList id="SomeDropDown" /> <asp:TextBox id="SomeTextBox" />Then in the code behind you would want declarations which are proper according to the object types: protected DropDownList SomeDropDown; protected TextBox SomeTextBox;And in your FindControls method, you would want something like: SomeDropDown = (DropDownList)this.Master.FindControl("ContentBody").FindControl("SomeDropDown"); SomeTextBox = (TextBox)this.Master.FindControl("ContentBody").FindControl("SomeTextBox");
Darth Continent
Yes this is my code anything wrong below tempPanelLabel = form1.FindControl("lbl_" + panelUsed + "_label" + counter.ToString()) tempPanelInputBox = form1.FindControl("txt_" + panelUsed + "_input" + counter.ToString())
txt_" + panelUsed + "_input" + counter.SelectedItem.ToString())is this fine?
Yeah based on your code that looks like it should result in the proper object name for FindControls to acquire the control.
Darth Continent
A: 

I would look for the control in the code behind file and change the:

Global.System.Web.UI.WebControls.TextBox

To:

Global.System.Web.UI.WebControls.DropDownList

What is the name of the control?

orandov
lbl_panview3_label1
Thats a label control. Not a dropdownlist or a textbox.Am I missing something?
orandov
sorry its txt_panview3_input1
The code behind file looks fine. I don't know what is causing the problem. Try closing everything (even VS) and reopening it.
orandov
Oh, didn't realize this is a coding issue and not an issue with the controls and the designer.
orandov