473,467 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1801
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.