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

Hi,

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:

if

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

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 asp.net and now I'm stuck. Am I missing something or FormView doesn't support this?

Any help?

0
kasim
3/5/2007 5:44:27 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

6 Replies
941 Views

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: http://fredrik.nsquared2.com/viewpost.aspx?PostID=195


/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

ASPInsiders
My Blog
0
Fredrik
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? 

 

0
kasim
3/5/2007 7:49:28 PM

Hi,

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

0
kasim
3/12/2007 5:52:56 PM

Hi

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.

0
Nick
3/13/2007 2:28:54 AM

Hi,

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

0
kasim
3/30/2007 4:59:53 PM

Thank You !!

0
xlancer
4/15/2008 6:42:53 PM
Reply: