findcontrol in form1 template

8 posts, 0 answers
  1. Matt
    Matt avatar
    4 posts
    Member since:
    May 2007

    Posted 15 May 2007 Link to this post

    I am trying to assign a value to a textbox control on a form and use the following code.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim txtusername As TextBox

    txtusername = FormView1.FindControl("username")

    txtusername.Text = getIdentity()

    End Sub

    Public Function getIdentity() As String

    getIdentity = (User.Identity.Name)

    End Function


    The function works fine but I get the error - Object reference not set to an instance of an object when I try to run it.

    This will work fine if I drag a textbox onto the page but just not when it is in a form template.

    I am extremely new to vb.net and am using vwd 2005.

    Any help would be greatly appreciated.
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 15 May 2007 Link to this post

    Hi Matt,

    Please try replacing this line of code:

    txtusername = FormView1.FindControl("username")

    with

    txtusername = FormView1.TemplateName.FindControl("username")

    where TemplateName is the form template control that you mention bellow.

    Let me know if this helps.

    Best wishes,
    Peter
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Matt
    Matt avatar
    4 posts
    Member since:
    May 2007

    Posted 15 May 2007 Link to this post

    No luck. When I add "InsertItemTemplate" which is the name of the template , the only option available in intellisense is InstantiateIn.
    I also tried me.form1.findcontrol. No errors pop up but I still get the same result.
  5. Kevin
    Kevin avatar
    549 posts
    Member since:
    Nov 2006

    Posted 15 May 2007 Link to this post

    Matt,

    Can you paste your FormView ASPX declaration and where your TextBox is located? This will surely provide additional clues.
  6. Matt
    Matt avatar
    4 posts
    Member since:
    May 2007

    Posted 15 May 2007 Link to this post


    <
    InsertItemTemplate>

    username:<asp:TextBox ID="username" runat="server" Width="100px" OnDataBinding="Page_Load"></asp:TextBox><br />

    </InsertItemTemplate>

     

  7. Kevin
    Kevin avatar
    549 posts
    Member since:
    Nov 2006

    Posted 15 May 2007 Link to this post

    I've just tried this setup and it worked fine. Maybe you have not set the DefaultMode property of the FormView to "Insert" ?

     <asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
                <InsertItemTemplate>
                    username:<asp:TextBox ID="username" runat="server" Width="100px"></asp:TextBox><br />
                </InsertItemTemplate>            
            </asp:FormView>
     
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim textBox As TextBox = CType(FormView1.FindControl("username"), TextBox)
            textBox.Text = "Some Name"
        End Sub
  8. Matt
    Matt avatar
    4 posts
    Member since:
    May 2007

    Posted 15 May 2007 Link to this post

    That worked. Thanks a lot.

    If I wanted to keep the page where the edit template shows first and then if the insert  button is clicked it will take you to the insertitem template and have the same function happen, would that be possible?
  9. Kevin
    Kevin avatar
    549 posts
    Member since:
    Nov 2006

    Posted 15 May 2007 Link to this post

    I guess you can do this in the ForeView.ModeChanged event, checking for the current mode of the ForView, e.g.

      Protected Sub FormView1_ModeChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles FormView1.ModeChanged 
            If FormView1.CurrentMode = FormViewMode.Insert Then 
                Dim textBox As TextBox = CType(FormView1.FindControl("username"), TextBox) 
                textBox.Text = "Some Name" 
            End If 
        End Sub 


Back to Top
UI for WinForms is Visual Studio 2017 Ready