FormView - FindControl

Discussion in 'ASP .Net' started by Paul Aspinall, Feb 25, 2006.

  1. Hi
    I'm having trouble trying to use FindControl with a FormView, but can't seem
    to find what I'm doing wrong....

    I have a TextBox in my ItemTemplate, and I want to assign a value to it, in
    code...

    I'm trying:

    ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";



    The error reported is 'Object reference not set to an instance of an
    object.'



    Any ideas?



    Thanks
    Paul Aspinall, Feb 25, 2006
    #1
    1. Advertising

  2. Paul where are you doing that?
    Patrick
    **It seems it can't find the control


    "Paul Aspinall" <> wrote in message
    news:UHVLf.54378$...
    > Hi
    > I'm having trouble trying to use FindControl with a FormView, but can't
    > seem to find what I'm doing wrong....
    >
    > I have a TextBox in my ItemTemplate, and I want to assign a value to it,
    > in code...
    >
    > I'm trying:
    >
    > ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";
    >
    >
    >
    > The error reported is 'Object reference not set to an instance of an
    > object.'
    >
    >
    >
    > Any ideas?
    >
    >
    >
    > Thanks
    >
    >
    Patrick.O.Ige, Feb 25, 2006
    #2
    1. Advertising

  3. I've tried it in a few places, prerender, load, databinding etc.

    No joy anywhere.

    Has anyone actually managed to do this in C#??

    Thanks

    "Patrick.O.Ige" <> wrote in message
    news:%...
    > Paul where are you doing that?
    > Patrick
    > **It seems it can't find the control
    >
    >
    > "Paul Aspinall" <> wrote in message
    > news:UHVLf.54378$...
    >> Hi
    >> I'm having trouble trying to use FindControl with a FormView, but can't
    >> seem to find what I'm doing wrong....
    >>
    >> I have a TextBox in my ItemTemplate, and I want to assign a value to it,
    >> in code...
    >>
    >> I'm trying:
    >>
    >> ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";
    >>
    >>
    >>
    >> The error reported is 'Object reference not set to an instance of an
    >> object.'
    >>
    >>
    >>
    >> Any ideas?
    >>
    >>
    >>
    >> Thanks
    >>
    >>

    >
    >
    Paul Aspinall, Feb 25, 2006
    #3
  4. Paul Aspinall

    chris Guest

    This may help.

    protected override void OnInit(EventArgs e)
    {
    FormView1.ItemCreated += new
    EventHandler(FormView1_ItemCreated);
    base.OnInit(e);
    }

    void FormView1_ItemCreated(object sender, EventArgs e)
    {
    TextBox t = (TextBox)FormView1.Row.FindControl("TextBox1");
    t.Text = "foo";
    }

    Chris
    chris, Feb 25, 2006
    #4
  5. Doh - Fixed it...

    Thanks anyway

    Paul

    "Paul Aspinall" <> wrote in message
    news:lXYLf.69245$...
    > I've tried it in a few places, prerender, load, databinding etc.
    >
    > No joy anywhere.
    >
    > Has anyone actually managed to do this in C#??
    >
    > Thanks
    >
    > "Patrick.O.Ige" <> wrote in message
    > news:%...
    >> Paul where are you doing that?
    >> Patrick
    >> **It seems it can't find the control
    >>
    >>
    >> "Paul Aspinall" <> wrote in message
    >> news:UHVLf.54378$...
    >>> Hi
    >>> I'm having trouble trying to use FindControl with a FormView, but can't
    >>> seem to find what I'm doing wrong....
    >>>
    >>> I have a TextBox in my ItemTemplate, and I want to assign a value to it,
    >>> in code...
    >>>
    >>> I'm trying:
    >>>
    >>> ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";
    >>>
    >>>
    >>>
    >>> The error reported is 'Object reference not set to an instance of an
    >>> object.'
    >>>
    >>>
    >>>
    >>> Any ideas?
    >>>
    >>>
    >>>
    >>> Thanks
    >>>
    >>>

    >>
    >>

    >
    >
    Paul Aspinall, Feb 25, 2006
    #5
  6. Paul Aspinall

    Nick Guest

    What was it Paul?

    I get the same error.

    Cheers,

    Nick

    "Paul Aspinall" <> wrote in message
    news:k7%Lf.69977$...
    > Doh - Fixed it...
    >
    > Thanks anyway
    >
    > Paul
    >
    > "Paul Aspinall" <> wrote in message
    > news:lXYLf.69245$...
    >> I've tried it in a few places, prerender, load, databinding etc.
    >>
    >> No joy anywhere.
    >>
    >> Has anyone actually managed to do this in C#??
    >>
    >> Thanks
    >>
    >> "Patrick.O.Ige" <> wrote in message
    >> news:%...
    >>> Paul where are you doing that?
    >>> Patrick
    >>> **It seems it can't find the control
    >>>
    >>>
    >>> "Paul Aspinall" <> wrote in message
    >>> news:UHVLf.54378$...
    >>>> Hi
    >>>> I'm having trouble trying to use FindControl with a FormView, but can't
    >>>> seem to find what I'm doing wrong....
    >>>>
    >>>> I have a TextBox in my ItemTemplate, and I want to assign a value to
    >>>> it, in code...
    >>>>
    >>>> I'm trying:
    >>>>
    >>>> ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";
    >>>>
    >>>>
    >>>>
    >>>> The error reported is 'Object reference not set to an instance of an
    >>>> object.'
    >>>>
    >>>>
    >>>>
    >>>> Any ideas?
    >>>>
    >>>>
    >>>>
    >>>> Thanks
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Nick, Feb 25, 2006
    #6
  7. Hi Nick,

    I've emailed you.

    Cheers
    "Nick" <> wrote in message
    news:O%...
    > What was it Paul?
    >
    > I get the same error.
    >
    > Cheers,
    >
    > Nick
    >
    > "Paul Aspinall" <> wrote in message
    > news:k7%Lf.69977$...
    >> Doh - Fixed it...
    >>
    >> Thanks anyway
    >>
    >> Paul
    >>
    >> "Paul Aspinall" <> wrote in message
    >> news:lXYLf.69245$...
    >>> I've tried it in a few places, prerender, load, databinding etc.
    >>>
    >>> No joy anywhere.
    >>>
    >>> Has anyone actually managed to do this in C#??
    >>>
    >>> Thanks
    >>>
    >>> "Patrick.O.Ige" <> wrote in message
    >>> news:%...
    >>>> Paul where are you doing that?
    >>>> Patrick
    >>>> **It seems it can't find the control
    >>>>
    >>>>
    >>>> "Paul Aspinall" <> wrote in message
    >>>> news:UHVLf.54378$...
    >>>>> Hi
    >>>>> I'm having trouble trying to use FindControl with a FormView, but
    >>>>> can't seem to find what I'm doing wrong....
    >>>>>
    >>>>> I have a TextBox in my ItemTemplate, and I want to assign a value to
    >>>>> it, in code...
    >>>>>
    >>>>> I'm trying:
    >>>>>
    >>>>> ((TextBox)FormView1.FindControl("TextBox1")).Text = "Sample value";
    >>>>>
    >>>>>
    >>>>>
    >>>>> The error reported is 'Object reference not set to an instance of an
    >>>>> object.'
    >>>>>
    >>>>>
    >>>>>
    >>>>> Any ideas?
    >>>>>
    >>>>>
    >>>>>
    >>>>> Thanks
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Paul Aspinall, Feb 26, 2006
    #7
  8. Paul Aspinall

    cdutoit

    Joined:
    Apr 3, 2007
    Messages:
    1
    Hi,

    I'm having the same problem. Could you post the solution here?

    Thanks
    Chris
    cdutoit, Apr 3, 2007
    #8
  9. Paul Aspinall

    nick5454

    Joined:
    Feb 4, 2008
    Messages:
    2
    And then answer is...

    FormView1.Row.FindControl("XXX");

    For those of you who are wondering. Or simply if you find an answer post it, don't hide it
    nick5454, Feb 4, 2008
    #9
  10. Paul Aspinall

    deverdits

    Joined:
    May 2, 2008
    Messages:
    2
    I'm having the same problem but usin VB, would anyone have a solution to this in VB? I've never done anything in C# so I don't know how to even start converting it.

    Much appreciated
    John.
    deverdits, May 11, 2008
    #10
  11. Paul Aspinall

    topherzee

    Joined:
    Sep 10, 2008
    Messages:
    2
    Solution to formview1.findcontrol problem

    None of the above suggestions worked for me.
    What did work was when I moved the call to the DataBound event.
    In VB..

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim txtUserID As TextBox = FormView1.FindControl("user_idTextBox")
    txtUserID.Text = 34

    -topherzee
    topherzee, Sep 10, 2008
    #11
  12. Paul Aspinall

    Magenta

    Joined:
    Oct 27, 2008
    Messages:
    2
    hi
    I am having a similar problem.

    I am trying to pass a value from a textbox outside a formview into a textbox in the InsertItemTemplate and have been using the findcontrol...
    here's my code:

    TextBox test = (TextBox)FormView1.FindControl("EidTextBox");
    EidTextBox.text = Textbox1.text;

    Am I wrongly using FindControl. Does anyone know how to make this work?
    I am very new to programming in asp.net. please help me
    Magenta, Oct 27, 2008
    #12
  13. Paul Aspinall

    Magenta

    Joined:
    Oct 27, 2008
    Messages:
    2
    Forgot to say that i am using C#
    Magenta, Oct 27, 2008
    #13
  14. Paul Aspinall

    Haseeb

    Joined:
    Apr 24, 2009
    Messages:
    1
    Hi, i am having the same problem... please provide help... how do u find a control... i tried getting controls for the whole form but i can't pick up my controls in formview... i searched alot for it but couldn't find anything... Nick and Paul seems to have the answer... plz help... any one provide help
    Haseeb, Apr 24, 2009
    #14
  15. Paul Aspinall

    Joined:
    Aug 20, 2011
    Messages:
    1
    even after 3 years,nobody reply? Just do like this

    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
    <EditItemTemplate>
    <asp:Label ID="Label1" runat="server" />
    </EditItemTemplate>
    </asp:FormView>


    Protected Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
    Dim oFormView As FormView
    oFormView = CType(sender, FormView)
    If Not oFormView.Visible Then Exit Sub

    Select Case oFormView.CurrentMode
    Case FormViewMode.Edit
    DIM L AS LABEL=CTYPE(FORMVIEW1.FINDCONTROL("Label1"),Label)
    L.text="find u"
    Case FormViewMode.ReadOnly

    End Select
    End Sub
    , Aug 20, 2011
    #15
    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. Zarn Bou via DotNetMonster.com

    FindControl in FormView

    Zarn Bou via DotNetMonster.com, Mar 16, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    2,889
    Zarn Bou via DotNetMonster.com
    Mar 16, 2005
  2. sck10
    Replies:
    2
    Views:
    5,040
    sck10
    Aug 28, 2006
  3. jobs
    Replies:
    0
    Views:
    409
  4. Mr. SweatyFinger

    findcontrol in a formview

    Mr. SweatyFinger, Dec 1, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    438
    Mr. SweatyFinger
    Dec 1, 2006
  5. SteveT
    Replies:
    1
    Views:
    758
    SteveT
    Jun 7, 2007
Loading...

Share This Page