FormView - FindControl

P

Paul Aspinall

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
 
P

Paul Aspinall

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
 
C

chris

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
 
P

Paul Aspinall

Doh - Fixed it...

Thanks anyway

Paul

Paul Aspinall said:
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
 
Joined
Feb 4, 2008
Messages
2
Reaction score
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
 
Joined
May 2, 2008
Messages
2
Reaction score
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.
 
Joined
Sep 10, 2008
Messages
2
Reaction score
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
 
Joined
Oct 27, 2008
Messages
2
Reaction score
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
 
Joined
Apr 24, 2009
Messages
1
Reaction score
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
 
Joined
Aug 20, 2011
Messages
1
Reaction score
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.

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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top