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

D

daniel.hedz

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!!!
 
T

Tim Mackey

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
 
D

daniel.hedz

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.
 
S

Saber Soleymani

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

dcherna

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("usercontrols\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("usercontrols\fooditem\fooditem.ascx");

myUC = (usercontrols.fooditem.fooditem)
FindControl(arrlstFoodItemIDs.ToString());

//here is where I tried all the variations like:
//myUC = FindControl(arrlstFoodItemIDs.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 !!!
 
D

dcherna

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top