FormView.ChangeMode and DataBind

Discussion in 'ASP .Net' started by J055, Aug 13, 2007.

  1. J055

    J055 Guest

    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
     
    J055, Aug 13, 2007
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
     
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=, Aug 14, 2007
    #2
    1. Advertising

  3. 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 (, 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.
     
    Walter Wang [MSFT], Aug 14, 2007
    #3
  4. J055

    J055 Guest

    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
     
    J055, Aug 15, 2007
    #4
  5. 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 (, 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.
     
    Walter Wang [MSFT], Aug 15, 2007
    #5
  6. 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 (, 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.
     
    Walter Wang [MSFT], Aug 20, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J055
    Replies:
    1
    Views:
    12,089
    Walter Wang [MSFT]
    Aug 10, 2006
  2. Trev

    Formview ChangeMode Problem

    Trev, Feb 8, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    614
  3. R Bandeira
    Replies:
    0
    Views:
    424
    R Bandeira
    Sep 6, 2007
  4. rodchar

    formview changeMode

    rodchar, Dec 13, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    rodchar
    Dec 17, 2007
  5. David Thielen

    FormView.ChangeMode(FormViewMode.Edit) not working

    David Thielen, Mar 2, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    682
    David Thielen
    Mar 4, 2007
Loading...

Share This Page