This is a migrated thread and some comments may be shown as answers.

findcontrol in form1 template

7 Answers 245 Views
Form
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 15 May 2007, 04:23 PM
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.

7 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 15 May 2007, 05:35 PM
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
0
Matt
Top achievements
Rank 1
answered on 15 May 2007, 06:10 PM
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.
0
Kevin
Top achievements
Rank 1
answered on 15 May 2007, 06:21 PM
Matt,

Can you paste your FormView ASPX declaration and where your TextBox is located? This will surely provide additional clues.
0
Matt
Top achievements
Rank 1
answered on 15 May 2007, 06:41 PM


<
InsertItemTemplate>

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

</InsertItemTemplate>

 

0
Kevin
Top achievements
Rank 1
answered on 15 May 2007, 06:47 PM
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
0
Matt
Top achievements
Rank 1
answered on 15 May 2007, 06:53 PM
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?
0
Kevin
Top achievements
Rank 1
answered on 15 May 2007, 08:47 PM
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 


Tags
Form
Asked by
Matt
Top achievements
Rank 1
Answers by
Peter
Telerik team
Matt
Top achievements
Rank 1
Kevin
Top achievements
Rank 1
Share this question
or