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

Discussion in 'ASP .Net' started by daniel.hedz@gmail.com, Jan 16, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Tim Mackey Guest

    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

    <> wrote in message
    news:...
    >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!!!
    >
     
    Tim Mackey, Jan 16, 2007
    #2
    1. Advertising

  3. Guest

    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
    >
    > <> wrote in message
    > news:...
    > >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, 2007
    #3
  4. Do you generate the UserControl in a page with a MasterPage?
    Would you cut/paste more lines of your code?

    <> wrote in message
    news:...
    >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!!!
    >
     
    Saber Soleymani, Jan 17, 2007
    #4
  5. dcherna Guest

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


    Saber Soleymani wrote:
    > Do you generate the UserControl in a page with a MasterPage?
    > Would you cut/paste more lines of your code?
    >
    > <> wrote in message
    > news:...
    > >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!!!
    > >
     
    dcherna, Jan 17, 2007
    #5
  6. dcherna Guest

    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!


    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!!!
     
    dcherna, Jan 17, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laura
    Replies:
    4
    Views:
    159
    Joe Iano
    Jul 24, 2003
  2. Catherine Lynn Smith

    Type Mismatch using Format()

    Catherine Lynn Smith, Nov 3, 2003, in forum: ASP General
    Replies:
    4
    Views:
    144
    Aaron Bertrand - MVP
    Nov 7, 2003
  3. Michael Hubbard
    Replies:
    0
    Views:
    84
    Michael Hubbard
    Oct 23, 2007
  4. Peter Jamieson

    Data type mismatch warning using DBI

    Peter Jamieson, Sep 5, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    147
    Peter Jamieson
    Sep 10, 2008
  5. programmingzeal
    Replies:
    0
    Views:
    1,129
    programmingzeal
    May 6, 2012
Loading...

Share This Page