formview - accessing and setting controls in formview's edit mode


I have a formview with a number of controls. I have been trying to set some controls after the page loads with no luck. Here is an example of what I'm doing:


(FormView1.CurrentMode == FormViewMode.Insert){
  TextBox idTextBox = (TextBox)FormView1.FindControl("TextBox1");
  idTextBox.Text = Session[

I've added the code to the Page_Load and Form_Load but it doesn't work. I've even tried to just focus the textbox using idTextBox.Focus() but that won't work. I made a test page not using formview and and things work fine.

The FormView is one of the main reasons I choose and now I'm stuck. Am I missing something or FormView doesn't support this?

Any help?

3/5/2007 5:44:27 PM 72751 articles. 3 followers. Follow

6 Replies

Similar Articles

[PageSpeed] 0

You can try to set the value in the PreRender event of the Page. If you bind the FromView to a datasource, the data binding can replace the value of your controls. Take a look at the following post:

/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

My Blog
3/5/2007 5:50:42 PM

Thanks for replying so quickly Fredrik!

I found your information to be very useful but it didn't fully work for me. I keep getting the following error (using PreRender and DataBound events):

"System.NullReferenceException: Object reference not set to an instance of an object."

...maybe because of the MasterPages I'm using...I tried to make a new test formview without the MasterPage and it works if I check for the formview mode first...i.e. if (FormView1.CurrentMode == FormViewMode.Insert) {then your code here}.

I checked to see if the FindControl was working using  if (idTextBox == null) {Response.Write("hi");} and surely it is null.

So basically, it is not finding the idTextBox control that resides inside the FormView1 which is in the MasterPage. I have previously found controls inside MasterPage but this time it is also inside the FormView and I can't seem to know how to do that. 

Any ideas? 


3/5/2007 7:49:28 PM


I still need to figure out how to access a control inside the formview which is itself inside a MasterPage. Can anyone help??

If I don't use the MasterPage then I can access a control inside the FormView. Also if I just use the MasterPage but not the FormView I can access the control. However if I use both the MasterPage and the FormView then the FindControl doesn't seem to work for me...I guess I don't know how to do it.

Can anyone help please??

 Thanks, Kasim

3/12/2007 5:52:56 PM


Please let me know if you find the solution to this problem I am also facing the same problem of NULLException when finding the listbox control.

3/13/2007 2:28:54 AM


I finally found the solution and hopefully it will work for everyone else:

Instead of using the PreRender for the page as mentioned above I used it for the FormView...also make sure you check which mode you are my case I want to find the control in the Insert mode.

if (FormView1.CurrentMode == FormViewMode.Insert) {

   TextBox test2 = (TextBox)FormView1.FindControl("TextBox1");

   test2.Text = "I found you!";


Hope that helps.

3/30/2007 4:59:53 PM

Thank You !!

4/15/2008 6:42:53 PM