More ParseControl()

Discussion in 'ASP .Net' started by Jeremy McPeak, Dec 22, 2003.

  1. I am playing around with ParseControl(), and I have a question. First here
    is the code I am using:

    Control MyButton1 = Page.ParseControl("<asp:button id='MyButton'
    text='Click here!' runat='server' />");
    Form1.Controls.Add(MyButton1);

    This adds the button to the form; however, I cannot access the button using
    it's ID (and I have declared it in the class). I can, however, access it
    through the Controls[] collection of Form1. The drawback is that I cannot
    access any of the Button class' properties. I have tried casting it:

    Button MyButton2 = (Button) MyButton1;

    But I get an invalid cast error at runtime.

    My question is this: Is there a way where I can access the control, as a
    Button, that was parsed through ParseControl other than the Controls[]
    collection of its parent?

    Thanks.
    Jeremy McPeak, Dec 22, 2003
    #1
    1. Advertising

  2. I found the solution. It seems that I had to cast using the FindControl()
    method:

    Button MyButton1 = (Button)Form1.FindControl("MyButton");

    "Jeremy McPeak" <http://www.wdonline.com/contact.aspx> wrote in message
    news:...
    > I am playing around with ParseControl(), and I have a question. First

    here
    > is the code I am using:
    >
    > Control MyButton1 = Page.ParseControl("<asp:button id='MyButton'
    > text='Click here!' runat='server' />");
    > Form1.Controls.Add(MyButton1);
    >
    > This adds the button to the form; however, I cannot access the button

    using
    > it's ID (and I have declared it in the class). I can, however, access it
    > through the Controls[] collection of Form1. The drawback is that I cannot
    > access any of the Button class' properties. I have tried casting it:
    >
    > Button MyButton2 = (Button) MyButton1;
    >
    > But I get an invalid cast error at runtime.
    >
    > My question is this: Is there a way where I can access the control, as a
    > Button, that was parsed through ParseControl other than the Controls[]
    > collection of its parent?
    >
    > Thanks.
    >
    >
    Jeremy McPeak, Dec 22, 2003
    #2
    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. Matthias Lohrer

    Page.ParseControl drives me crazy

    Matthias Lohrer, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,742
    Matthias Lohrer
    Jul 8, 2003
  2. Matthias Lohrer

    Using Validation Controls with Page.ParseControl

    Matthias Lohrer, Jul 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,592
    Satish Appasani
    Jul 29, 2003
  3. Chris Simeone

    .NET ParseControl() method problem

    Chris Simeone, Dec 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,347
    Chris Simeone
    Dec 10, 2003
  4. Jeremy McPeak

    ParseControl()

    Jeremy McPeak, Dec 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,915
    Rick Strahl [MVP]
    Dec 21, 2003
  5. Chris Simeone

    Is this a ParseControl bug?

    Chris Simeone, Dec 22, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    639
    Patrice Scribe
    Dec 23, 2003
Loading...

Share This Page