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

  2. Paul where are you doing that?
    Patrick
    **It seems it can't find the control
     
    Patrick.O.Ige, Feb 25, 2006
    #2
    1. Advertisements

  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
     
    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, Feb 25, 2006
    #5
  6. Paul Aspinall

    Nick Guest

    What was it Paul?

    I get the same error.

    Cheers,

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

    I've emailed you.

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

    cdutoit

    Joined:
    Apr 3, 2007
    Messages:
    1
    Likes Received:
    0
    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
    Likes Received:
    0
    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
    Likes Received:
    0
    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
    Likes Received:
    0
    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
    Likes Received:
    0
    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
    Likes Received:
    0
    Forgot to say that i am using C#
     
    Magenta, Oct 27, 2008
    #13
  14. Paul Aspinall

    Haseeb

    Joined:
    Apr 24, 2009
    Messages:
    1
    Likes Received:
    0
    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
    Likes Received:
    0
    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. Advertisements

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 (here). After that, you can post your question and our members will help you out.