DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Mon, 13 Aug 2007 17:41:44 +0100,    posted on: microsoft.public.dotnet.framework.aspnet        back       

Thread Index
  1    J055 am
          2    Ladislav Mrnka
          3    (Walter Wang [MSFT])
                 4    J055 am
                        5    (Walter Wang [MSFT])
                        6    (Walter Wang [MSFT])


FormView.ChangeMode and DataBind   
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
Date:Mon, 13 Aug 2007 17:41:44 +0100   Author:  

RE: FormView.ChangeMode and DataBind   
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
> 
> 
> 
> 
Date:Mon, 13 Aug 2007 23:42:00 -0700   Author:  

RE: FormView.ChangeMode and DataBind   
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 (wawang@online.microsoft.com, 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.
Date:Tue, 14 Aug 2007 07:25:16 GMT   Author:  

Re: FormView.ChangeMode and DataBind   
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
Date:Wed, 15 Aug 2007 09:11:06 +0100   Author:  

Re: FormView.ChangeMode and DataBind   
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 (wawang@online.microsoft.com, 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.
Date:Wed, 15 Aug 2007 09:34:50 GMT   Author:  

Re: FormView.ChangeMode and DataBind   
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 (wawang@online.microsoft.com, 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.
Date:Mon, 20 Aug 2007 02:43:19 GMT   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us