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

type mismatch when using FindControl("...")

P: n/a
I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!

Jan 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
hi daniel,
are you trying to find the control after a postback? you know you have to
recreate the dynamic control for every postback?

tim

<da*********@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
>I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!
Jan 16 '07 #2

P: n/a
Hi Tim!

I am trying to find the control after I click a LinkButton. I put a
break point in the Paage_Load and when I click the LinkButton the
Page_Load is never reached. The debugger goes straight to the
LinkButton_Click event. And then I am not able to find the control. It
keeps telling me this:

Object reference not set to an instance of an object.

This morning I deleted the usercontrol from where I am trying to find
myUC and redo it to see if it had something to do with the environment,
but no luck. Thank you for you help, but that did not solve my problem,
and any other suggestions are welcome.


Tim Mackey wrote:
hi daniel,
are you trying to find the control after a postback? you know you have to
recreate the dynamic control for every postback?

tim

<da*********@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!
Jan 17 '07 #3

P: n/a
Do you generate the UserControl in a page with a MasterPage?
Would you cut/paste more lines of your code?

<da*********@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
>I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!

Jan 17 '07 #4

P: n/a
Hi there!

I do generate the usercontrol in a page with a masterpage. I generate
usercontrols of type A dynamically in a usercontrol called B and then
call B in a page with a MasterPage.

//generating usercontrol
//This function is call in the Page_Load, and the usercontrol that I am
generating dynamically
//consists of a checkbox and two dropdownlists

void createUC()
{

TableRow trItem = new TableRow();//New <tr>
TableCell tdItem = new TableCell();//New <td>
ableCell tdItem2 = new TableCell();//New <td>

for(int i=0; i<10; i++)
{

usercontrols.fooditem.fooditem myUC =
(usercontrols.fooditem.fooditem)LoadControl("userc ontrols\fooditem\fooditem.ascx");

myUC.ID = "123|" + i.ToString() + "*"; //Just a name for myUC

//in the HMTL code I have an asp table named offeredMenustable in side
a view, which it is hold by a multiview
//so, I have multiview>view>asp:table
//<asp:Table runat="server" ID="offeredMenusTable"
Width="720px"></asp:Table>
//then I just add the myUC to the offeredMenusTable

tdItem.Controls.Add(ckBox);
trItem.Cells.Add(tdItem);
offeredMenusTable.Rows.Add(trItem);// Add row to the table.
//offeredMenusTable.Controls.Add(trItem);//I tried controls too, but no
luck

_arrlstFoodItemIDs.Add(myUC.ID);//this is an arraylist to hold the
names of the usercontrols

}
Session["UC_IDs"] = arrlstFoodItemIDs;

}

//Then I have a linkButton in the usercontrol B to check all
dynamically generated usercontrols

LinkButton_Click(object sender, EventArgs e)
{
arrlstFoodItemIDs = (ArrayList) Session["UC_IDs"];

for(int i=0; i<arrlstFoodItemIDs.Count; i++)
{

usercontrols.fooditem.fooditem myUC =
(usercontrols.fooditem.fooditem)LoadControl("userc ontrols\fooditem\fooditem.ascx");

myUC = (usercontrols.fooditem.fooditem)
FindControl(arrlstFoodItemIDs[i].ToString());

//here is where I tried all the variations like:
//myUC = FindControl(arrlstFoodItemIDs[i].ToString()) as
usercontrols.fooditem.fooditem;

//checkedvalue is a property in my usercontrol
//not that is important since the myUC will be null

if(myUC.checkedvalue == false)
{
myUC.checkedvalue = true;
}

}

}

As I said I have been working in this for 3 days now. And I ran out of
ideas, so any suggestions are appreciated. thank you !!!
Saber Soleymani wrote:
Do you generate the UserControl in a page with a MasterPage?
Would you cut/paste more lines of your code?

<da*********@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!
Jan 17 '07 #5

P: n/a
I just looked at the HTML code and I notice that the name of my
usercontrol is longer than it should be.

I name my usercontrol (ID): 2514|0*

but I get this in the HTML:
ctl00_ContentPlaceHolder1_svimenuselection1_2514|0 *__CheckBox1

I also checked it in the codebehind:

System.Diagnostics.Debug.WriteLine(myUC.ClientID);

and I get the same as the HTML code. So, how do I get to my
usercontrol.

remember I have an asp:table and then the usercontrols. Thank you for
all your help!
da*********@gmail.com wrote:
I am generating a usercontrol dynamically successfully, but when I try
to find that usercontrol I get a type mismatch. This is what I am
doing:

//Loading my usercontrol
MyWebApp.Folder.Folder.MyUsercontrol myUC =
(MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx");
//Locating my usercontrol 1
myUC = Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 2
myUC = (MyWebApp.Folder.Folder.MyUsercontrol)
Findcontrol("myUCID");//Not working

//Locating my usercontrol 3
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
MyWebApp.Folder.Folder.MyUsercontrol;//Not working

//Locating my usercontrol 4
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) myUC.GetType();//Not
allowed

//Locating my usercontrol 5
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Type =
myUC.GetType();//Not allowed

//Locating my usercontrol 6
myUC = (MyWebApp.Folder.Folder.MyUsercontrol) Findcontrol("myUCID") as
((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") );//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID");//Not allowed

//Locating my usercontrol 7
myUC = ((MyWebApp.Folder.Folder.MyUsercontrol)
LoadControl("~/Folder/Folder/MyUsercontrol.ascx") )
Findcontrol("myUCID") as MyWebApp.Folder.Folder.MyUsercontrol;//Not
allowed

//When I try form 1 and 2 I get a type mismatch (inspecting locals in
vs 2005):

myUC = (Value) {ASP.Foler_Folder_MyUserControl_ascx}, (Type)
MyWebApp.Folder.Folder.MyUsercontrol
{ASP.Foler_Folder_MyUserControl_ascx}

//andwhen I look for the usercontrol I only get this:
// Object reference not set to an instance of an object.
myUC = (Value) null, (Type) MyWebApp.Folder.Folder.MyUsercontrol

//notice how the "{ASP.Foler_Folder_MyUserControl_ascx}" don't show in
the type.

//As you can see I tried many things and I haven't been able to make
this piece of code work. So, any help is appreciated. Thank you!!!
Jan 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.