FormView.ChangeMode and DataBind

J

J055

Hi

I have a FormView control within a UserControl. After I DataBind I need to
change the CurrentMode depending on a value from the ObjectDataSource Select
method. When I do this the FormView DataBinds again. Also in the first
DataBound event the controls are null so I have to check for null even
though I know they are there.

I would prefer not to call my select method twice. Also the first databound
event seems pointless.

What is the recommended approach here?
Thanks
Andrew
 
G

Guest

Hi Andrew,

FormView control uses templates to define UI for different modes. If you
start with one mode and call data bind controls from current template are
bound to data source. If you change the mode after first bidning you also
discard current template with all bounded values and data binding has to be
called again to fill in controls from template for new mode.

For second question about null controls. When exactly do you bind fot the
first time?

Regards,
Ladislav
 
W

Walter Wang [MSFT]

Hi Andrew,

For your first question about ChangeMode will cause the data re-bound, I
think is by design behavior. Whenever there's any property that is changed,
the data needs to be re-bound. Internally the data bound control will use a
property called RequiresDataBinding to track this. This is how DataSource
controls are designed to work magically without you manually call
DataBind(). You may also noted that whenver you change GridView.EditIndex,
it will also cause the GridView to rebind.

I'm not very clear about your second question "in the first DataBound event
the controls are null", would you please depict more about it? Thanks.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

J055

Hi

Thanks for the information. My problem with the null controls problem
happens as follows:

When I change the ChangeMode to FormViewMode.ReadOnly in the DataBound event
the controls in the FormView ItemTemplate are null. It looks like these are
not created when the ChangeMode is changed after the DataBind. When the
DataBound event fires again they are not null again.

protected void fvPub_DataBound(object sender, EventArgs e)
{
if (Status == Status.Published)
{
fvPub.ChangeMode(FormViewMode.ReadOnly);
}
Label myLabel = (Label)fvPub.FindControl("myLabel");
myLabel.Text = "stuff"; // NullReferenceException
}

I can't change the FormViewMode until I have the called the select methos so
I wonder what the best approach is to avoid a double databind and the
problems with null controls in the first DataBound event. I know I can test
for null for each control first but I think this would be fixing a problem
which shouldn't exist in the first place.

Thanks
Andrew
 
W

Walter Wang [MSFT]

Hi Andrew,

Since you have to rely on the data returned from ObjectDataSource's
selected value to change the mode, I don't think there's a way to avoid
binding twice, unless you first query the data manually to get the flag --
but I think it's still querying the data twice so it's probably no use.

For the null reference issue, I believe this is a common issue that caused
by the fact that FindControl only finds a child control in the direct
children collection. For controls in a template, they're in another
container and cannot be found using FormView.FindControl.

protected void FormView1_DataBound(object sender, EventArgs e)
{
TextBox txt = (TextBox)FindControlRecursive(FormView1, "IdTextBox");
}

public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}

return null;
}


Above code should help you find any child controls in the hierarchy.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Andrew,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top