formview.InsertItem

Discussion in 'ASP .Net Web Controls' started by Trapulo, Apr 4, 2007.

  1. Trapulo

    Trapulo Guest

    I need to call formview.insertitem. If an error occurs, I handle my
    objectdataSource.Inserted event
    and then set e.ExceptionHandled = True

    The problem is that after InsertItem, my formview is in readonly mode. Why?
    I need to switch in readonly mode (or, in fact, in editmode) after an insert
    succeed, but the control may remain in insertmode if an error occurs... :(



    thanks
     
    Trapulo, Apr 4, 2007
    #1
    1. Advertising

  2. Hi Trapulo,

    As for the FormView control, when you perform Insert Operation on it, if
    you haven't explicitly specify FormView's new mode, it will always change
    it to the DefaultMode you set on the FormView(generally it is the
    "ReadOnly" mode). To make the Formview remain "InsertMode" in your case,
    you can use either of the following means:

    #1 FormViewInsertedEventArgs has a "KeepInInsertMode" property which can
    help make the FormView remain "InsertMode" after inserted event

    ========================
    protected void FormView1_ItemInserted(object sender,
    FormViewInsertedEventArgs e)
    {
    if (e.Exception != null)
    {
    Response.Write("<br/>Exception: " + e.Exception.Message);

    e.ExceptionHandled = true;
    e.KeepInInsertMode = true;
    }

    }
    ===============


    #2 Or you can manually call "FormView.ChangeMode" to set the FormView's
    sequential Mode(this means is not limited to Inserting case only):

    protected void ObjectDataSource1_Inserted(object sender,
    ObjectDataSourceStatusEventArgs e)
    {

    if (e.Exception != null)
    {
    Response.Write("<br/>Exception: " + e.Exception.Message);

    e.ExceptionHandled = true;

    FormView1.ChangeMode(FormViewMode.Insert);
    }

    }
    ==============================


    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Apr 5, 2007
    #2
    1. Advertising

  3. Trapulo

    Trapulo Guest

    Hello Steven,
    I think it's better your second suggestion, so I can manage only one
    inserted event handler (I need to handle DataSource_Inserted because I need
    to access e.returnValue, so if I have only one code is better).
    However, I've tried it, but there is a problem.
    I start the process from here:
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnSave.Click

    Select Case Me.fv.CurrentMode

    Case FormViewMode.Edit

    Me.fv.UpdateItem(True)

    Case FormViewMode.Insert

    Me.fv.InsertItem(True)

    End Select

    End Sub

    However, I call changeMode from datasource_Inserted, but after fv.InsertItem
    I have that currentMode is ReadOnly. Why?

    Meanwhile, I solved with the other solution, thaks.




    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Trapulo,
    >
    > As for the FormView control, when you perform Insert Operation on it, if
    > you haven't explicitly specify FormView's new mode, it will always change
    > it to the DefaultMode you set on the FormView(generally it is the
    > "ReadOnly" mode). To make the Formview remain "InsertMode" in your case,
    > you can use either of the following means:
    >
    > #1 FormViewInsertedEventArgs has a "KeepInInsertMode" property which can
    > help make the FormView remain "InsertMode" after inserted event
    >
    > ========================
    > protected void FormView1_ItemInserted(object sender,
    > FormViewInsertedEventArgs e)
    > {
    > if (e.Exception != null)
    > {
    > Response.Write("<br/>Exception: " + e.Exception.Message);
    >
    > e.ExceptionHandled = true;
    > e.KeepInInsertMode = true;
    > }
    >
    > }
    > ===============
    >
    >
    > #2 Or you can manually call "FormView.ChangeMode" to set the FormView's
    > sequential Mode(this means is not limited to Inserting case only):
    >
    > protected void ObjectDataSource1_Inserted(object sender,
    > ObjectDataSourceStatusEventArgs e)
    > {
    >
    > if (e.Exception != null)
    > {
    > Response.Write("<br/>Exception: " + e.Exception.Message);
    >
    > e.ExceptionHandled = true;
    >
    > FormView1.ChangeMode(FormViewMode.Insert);
    > }
    >
    > }
    > ==============================
    >
    >
    > Hope this helps.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
     
    Trapulo, Apr 5, 2007
    #3
  4. Hi Trapulo,

    As for the following behavior you mentioned:

    >>>>>>>>>>>>>>>

    However, I call changeMode from datasource_Inserted, but after
    fv.InsertItem
    I have that currentMode is ReadOnly. Why?
    <<<<<<<<<<<<<

    do you mean you still get the ReadOnly state even after you call
    "ChangeMode" of FormView? If so, would tell me where did you put the
    ChangeMode and where did you found that the Mode is still readonly?


    BTW, below is the disassembled code from reflector, the "HandleInsert"
    method is used when we call FormView.InsertItem, and it will register a
    Callback function named "HandleInsertCallback" that was called after
    inserting has been finished. There in the "HandleInsertCallback" function,
    you will find that FormView will always set the state to "DefaultMode" if
    we haven't manually specifiy "KeepInInsertMode":

    ====================================
    private void HandleInsert(string commandArg, bool causesValidation)
    {
    if ((!causesValidation || (this.Page == null)) || this.Page.IsValid)
    {
    if (this.Mode != FormViewMode.Insert)
    {
    throw new
    HttpException(SR.GetString("DetailsViewFormView_ControlMustBeInInsertMode",
    new object[] { "FormView", this.ID }));
    }
    DataSourceView data = null;
    bool isBoundUsingDataSourceID = base.IsBoundUsingDataSourceID;
    if (isBoundUsingDataSourceID)
    {
    data = this.GetData();
    if (data == null)
    {
    throw new
    HttpException(SR.GetString("View_DataSourceReturnedNullView", new object[]
    { this.ID }));
    }
    }
    FormViewInsertEventArgs e = new FormViewInsertEventArgs(commandArg);
    if (isBoundUsingDataSourceID)
    {
    this.ExtractRowValues(e.Values, true);
    }
    this.OnItemInserting(e);
    if (!e.Cancel && isBoundUsingDataSourceID)
    {
    this._insertValues = e.Values;
    data.Insert(e.Values, new
    DataSourceViewOperationCallback(this.HandleInsertCallback));
    }
    }
    }


    .....................

    private bool HandleInsertCallback(int affectedRows, Exception ex)
    {
    FormViewInsertedEventArgs e = new
    FormViewInsertedEventArgs(affectedRows, ex);
    e.SetValues(this._insertValues);
    this.OnItemInserted(e);
    this._insertValues = null;
    if ((ex != null) && !e.ExceptionHandled)
    {
    return false;
    }
    if (!e.KeepInInsertMode)
    {
    FormViewModeEventArgs args2 = new
    FormViewModeEventArgs(this.DefaultMode, false);
    this.OnModeChanging(args2);
    if (!args2.Cancel)
    {
    this.Mode = args2.NewMode;
    this.OnModeChanged(EventArgs.Empty);
    base.RequiresDataBinding = true;
    }
    }
    return true;
    }

    ===================================

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Apr 6, 2007
    #4
  5. Trapulo

    Trapulo Guest

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Trapulo,
    >
    > As for the following behavior you mentioned:
    >
    >>>>>>>>>>>>>>>>

    > However, I call changeMode from datasource_Inserted, but after
    > fv.InsertItem
    > I have that currentMode is ReadOnly. Why?
    > <<<<<<<<<<<<<
    >
    > do you mean you still get the ReadOnly state even after you call
    > "ChangeMode" of FormView?


    yes

    >If so, would tell me where did you put the
    > ChangeMode and where did you found that the Mode is still readonly?


    I put it in myDatasource_Inserted event handler. However now I've changed
    code to avoid the problem, so I cannot paste it "as was".
    I found it using VS debugger, testing the property after the call to
    InsertItem.

    > BTW, below is the disassembled code from reflector, the "HandleInsert"
    > method is used when we call FormView.InsertItem, and it will register a
    > Callback function named "HandleInsertCallback" that was called after
    > inserting has been finished. There in the "HandleInsertCallback" function,
    > you will find that FormView will always set the state to "DefaultMode" if
    > we haven't manually specifiy "KeepInInsertMode":


    That's clear, thank you
     
    Trapulo, Apr 10, 2007
    #5
    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. =?Utf-8?B?c2NrMTA=?=

    FormView Control

    =?Utf-8?B?c2NrMTA=?=, Oct 1, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,012
    Steven Cheng[MSFT]
    Oct 18, 2004
  2. Daves
    Replies:
    1
    Views:
    464
    Brock Allen
    Apr 25, 2005
  3. Chris

    Formview within Formview

    Chris, May 19, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    568
    Chris
    May 19, 2007
  4. Weston Weems

    DataGrid, Seperate template for InsertItem

    Weston Weems, Oct 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    125
    JDP@Work
    Oct 18, 2004
  5. David Thielen

    FormView/Repeater/ Eval(FormView data)

    David Thielen, Jun 16, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    681
    David Thielen
    Jun 16, 2006
Loading...

Share This Page