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