472,976 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

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

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
5 1776
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dmgauntt2002 | last post by:
I have some javascript ASP code that sets the expiry date on a cookie to five years from now. The code worked until today, when I got the following error message: Microsoft JScript runtime...
2
by: Andrew Proctor | last post by:
Hello This is not a big problem, but I was hoping someone a bit more knowlegable than I could explain something to me. I have a simple function behind a form which writes changed values to a...
1
by: melda | last post by:
I am a real beginnner in ASP. Due to increasing demands on dynamic website, I've been working on ASP website now. I use a ready to use CMS program and right now I've been trying to combine a calendar...
1
by: ApexData | last post by:
Access2k I WAS getting the following error when using the MID function: Run Time Error '13' Type Mismatch I have a new single form, unbound with 1-button on it that uses the following code:...
5
by: Davros9 | last post by:
Trying to get Regular Expressions working....... ---------------- Public Function SepString(InField As String) As String ''seperates on space and comma Dim RE As New RegExp Dim Matches As...
17
imrosie
by: imrosie | last post by:
I've tried this string to Sum up payments, moving brackets and paren's different ways, I still get error 'type mismatch'....not sure why. Can anyone point me in the right direction?? thanks ...
10
by: dstorms | last post by:
Hi, I'm trying to create a button on a form that: 1. Takes the ComputerID from the form linked to Table 1, 2. Checks Table 2 for a matching ComputerID, and 3. Opens the query qryEditData, and...
3
by: martin DH | last post by:
I would definitely appreciate any help with this problem as soon as possible. Thanks! I have a report that should display the results of a query based on two tables (tblClient and tblResult,...
2
DonRayner
by: DonRayner | last post by:
This one has me stumped. I'm getting a "Type Mismatch" error on one of my forms when it's being opened. It's hapening before the forms "On Open" event, I stuck a msgbox in there to check and I'm...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.