Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > FormView.ChangeMode and DataBind

Reply
Thread Tools

FormView.ChangeMode and DataBind

 
 
J055
Guest
Posts: n/a
 
      08-13-2007
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



 
Reply With Quote
 
 
 
 
=?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
Guest
Posts: n/a
 
      08-14-2007
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


"J055" wrote:

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

 
Reply With Quote
 
 
 
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      08-14-2007
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 ((E-Mail Removed), 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.

 
Reply With Quote
 
J055
Guest
Posts: n/a
 
      08-15-2007
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


 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      08-15-2007
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 ((E-Mail Removed), 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.

 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      08-20-2007
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 ((E-Mail Removed), 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie with asp .net, databind and code behind Carlos ASP .Net 1 12-07-2004 08:22 AM
DataBind CheckBoxList and setting checked value Rik ASP .Net 0 09-08-2004 09:09 AM
DataBind and SqlDataReader Gawel ASP .Net 4 05-22-2004 10:31 AM
Databind() and flexibility? me ASP .Net 2 03-03-2004 11:05 AM
BUG in DataBind? After .DataBind there are more DataGrid Items than DataSet Rows! Michael ASP .Net Datagrid Control 0 12-29-2003 07:47 PM



Advertisments