formview.InsertItem

T

Trapulo

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
 
S

Steven Cheng[MSFT]

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

Trapulo

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

Steven Cheng[MSFT]

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

Trapulo

Steven Cheng said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top