FormView Insert

A

AG

ASP.NET 2.0 aspx page with a FormView bound to an ObjectDataSource to view,
add and edit records.

Can anyone point me to a sample of how to retain the user entered control
values when an insert fails?
In other words, when inserting a new record, user enters data in the
controls, but the insert fails.
When the page posts back all the controls are empty or at their default
values.
I would like to retain the user enter values so they can correct their
errors.

TIA
 
M

Masudur

Hi,,,


protected void FormView1_ItemInserting(object sender,
FormViewInsertEventArgs e)
{
if (FormView1.DefaultMode == FormViewMode.Insert)
{
TextBox txtName =
(TextBox)FormView1.FindControl("controlid");
}
}

this is for item inserting event...
you can also use it in item inserted event of the form view

Thanks
Masudur
www.kaz.com.bd
 
S

Steven Cheng[MSFT]

Thanks for Masudur's informative input.

Hi AG,

As Masudur suggested, you can use FormView1.FindControl to get reference
to those sub controls(TextBox or dropdownlist or ...) in the certain
template(ItemTemplate, Edittemplate or InsertItemTemplate...), and the
"ItemInserting" event is dedicated to this usage. If the control structure
in your FormView's template is abit complex(such as have nested control
hierarchy), you may need to call FindControl multiple times from super
level to downlevel so as to get the underlying control reference.

BTW, for simple databinding and control template scenario, you can use the
two-way databinding support in ASP.NET 2.0. You can use the "Bind"
expression to declere the certain control's property to be mapped to insert
parameter at inserting time. for example, in the following case, the
FormView's insertItemtemplate use "bind" expression to map two control
field to the insert command parameters

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

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETTestDBConnectionString %>"
....... InsertCommand="INSERT INTO [RVTable] ([name], [description])
VALUES (@name, @description)">
................
<InsertParameters>
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>


<InsertItemTemplate>
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%#
Bind("name") %>'>
</asp:TextBox><br />
description:
<asp:TextBox ID="descriptionTextBox" runat="server"
Text='<%# Bind("description") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
==============================

Thus, you can avod putting additional code to extract the parameters from
control collection manually.

Here are some additional reference about two-way databniding in ASP.NET 2.0:

http://msdn2.microsoft.com/en-us/library/ms178366(VS.80).aspx

http://davidhayden.com/blog/dave/archive/2005/05/25/1051.aspx

http://www.manuelabadia.com/blog/PermaLink,guid,a2abee08-bb7f-4fe5-8a24-6e56
da99172a.aspx


Hope this also helps. If there is any further question on this, please feel
free to post here.

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

AG

Thanks Steven,

As I mentioned, I am using an object data source and the controls are bound.

There are some nested controls, and viewstate is on for all controls.
I realize that I can loop through all the controls, but I would think that
there should be an easier way.
After all, isn't two way data binding supposed to make things easier?

--

AG
Email: discuss at adhdata dot com



Steven Cheng said:
Thanks for Masudur's informative input.

Hi AG,

As Masudur suggested, you can use FormView1.FindControl to get reference
to those sub controls(TextBox or dropdownlist or ...) in the certain
template(ItemTemplate, Edittemplate or InsertItemTemplate...), and the
"ItemInserting" event is dedicated to this usage. If the control
structure
in your FormView's template is abit complex(such as have nested control
hierarchy), you may need to call FindControl multiple times from super
level to downlevel so as to get the underlying control reference.

BTW, for simple databinding and control template scenario, you can use the
two-way databinding support in ASP.NET 2.0. You can use the "Bind"
expression to declere the certain control's property to be mapped to
insert
parameter at inserting time. for example, in the following case, the
FormView's insertItemtemplate use "bind" expression to map two control
field to the insert command parameters

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

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETTestDBConnectionString %>"
....... InsertCommand="INSERT INTO [RVTable] ([name], [description])
VALUES (@name, @description)">
................
<InsertParameters>
<asp:parameter Name="name" Type="String" />
<asp:parameter Name="description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>


<InsertItemTemplate>
name:
<asp:TextBox ID="nameTextBox" runat="server" Text='<%#
Bind("name") %>'>
</asp:TextBox><br />
description:
<asp:TextBox ID="descriptionTextBox" runat="server"
Text='<%# Bind("description") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server"
CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
==============================

Thus, you can avod putting additional code to extract the parameters from
control collection manually.

Here are some additional reference about two-way databniding in ASP.NET
2.0:

http://msdn2.microsoft.com/en-us/library/ms178366(VS.80).aspx

http://davidhayden.com/blog/dave/archive/2005/05/25/1051.aspx

http://www.manuelabadia.com/blog/PermaLink,guid,a2abee08-bb7f-4fe5-8a24-6e56
da99172a.aspx


Hope this also helps. If there is any further question on this, please
feel
free to post here.

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

Steven Cheng[MSFT]

Thanks for your reply AG,

Sure, if possible, I would prefer using the two-way databing( through the
"Bind" expression I mentioned previously). However, the Bind experssion
normally attached to a top level property of a certain control in the
FormView(or other template databound control's template).

Anyway, using the ItemUpdating or ItemInserting event and manually locate
Control (through FindControl) approach will certainly work. For yoru
scenario, I'm still wondering your FormView's InsertTemplate control
structure and your objectdatasource insert method's signature and how it is
defined in aspx template. Would you provide me some code snippet so that I
can get a further view about it?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Steven,

I am using the Bind expression.

So, are you saying that there is no simple way to retain the entered values
of the controls if the insert fails?
That I must 'find' all the controls and repopulate them myself?

Here is the insert template

<InsertItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">

<tr>

<td style="width: 15%" valign="top">

Record #</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

</td>

</tr>

<tr>

<td style="width: 15%" valign="top">

Entry Date</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

<asp:TextBox ID="EntryDateTextBox" runat="server" Text='<%#
Bind("EntryDate") %>' Width="100px"></asp:TextBox>

</td>

</tr>

<tr>

<td style="width: 15%" valign="top">

Life Areas</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

<asp:CheckBoxList ID="LifeAreasCheckBoxList" runat="server"
DataSourceID="odsLifeArea"

DataTextField="LifeAreaDesc" DataValueField="LifeAreaId" RepeatColumns="3">

</asp:CheckBoxList></td>

</tr>

<tr>

<td style="width: 15%" valign="top">

Service</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

<asp:DropDownList ID="ServicesDropDownList" runat="server"
DataSourceID="odsServices"

DataTextField="Service" DataValueField="ServiceID" SelectedValue='<%#
Bind("ServiceId") %>' AutoPostBack="True"
OnSelectedIndexChanged="ServicesDropDownList_SelectedIndexChanged">

</asp:DropDownList></td>

</tr>

<tr>

<td style="width: 15%" valign="top">

Service Type</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

<asp:DropDownList ID="ddlServiceTypeID" runat="server"
DataSourceID="odsCPN_ServiceType"

DataTextField="ServiceType" DataValueField="ServiceTypeId"
SelectedValue='<%# Bind("ServiceTypeId") %>'>

</asp:DropDownList>

<asp:TextBox ID="txtOtherServiceType" runat="server" Text='<%#
Bind("OtherServiceType") %>' MaxLength="50"
Width="175px"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 15%" valign="top">

Time Spent</td>

<td style="width: 1%" valign="top">

:</td>

<td style="width: 84%" valign="top">

<asp:TextBox ID="txtTimeSpent" runat="server" Text='<%# Bind("TimeSpent")
%>' Width="80px"></asp:TextBox></td>

</tr>

<tr>

<td style="width: 15%; height: 16px" valign="top">

Format</td>

<td style="width: 1%; height: 16px" valign="top">

:</td>

<td style="width: 84%; height: 16px" valign="top">

<asp:DropDownList ID="FormatDropDownList" runat="server" SelectedValue='<%#
Bind("FormatID") %>' AutoPostBack="True"
OnDataBound="FormatDropDownList_DataBound"
OnSelectedIndexChanged="FormatDropDownList_SelectedIndexChanged"
DataSourceID="odsCPN_Format" DataTextField="FormatName"
DataValueField="FormatID">

</asp:DropDownList></td>

</tr>

</table>

<hr />

<asp:GridView ID="gvFormatPartNotes" Width="100%" runat="server"
AutoGenerateColumns="false" DataKeyNames="FormatPartLabel"
BorderColor="Transparent" ShowHeader="false" BorderWidth="0px"
OnRowDataBound ="gvFormatPartNotes_RowDataBound">

<Columns>

<asp:TemplateField HeaderText="Label">

<ItemTemplate>

<asp:Label ID="lblFormatPartLabel" runat="server" Text='<%#
Bind("FormatPartLabel") %>'></asp:Label>

</ItemTemplate>

<ItemStyle Width="15%" />

<HeaderStyle Width="15%" />

</asp:TemplateField>

<asp:TemplateField HeaderText="Notes">

<ItemTemplate>

<asp:TextBox ID="txtFormatPartNotes" runat="server" Text='<%# Bind("Notes")
%>' TextMode="MultiLine" Rows="4" Width="95%"></asp:TextBox>

<asp:RequiredFieldValidator ID="valrFormatPartNotes" runat="server"
ControlToValidate="txtFormatPartNotes"

Display="Dynamic" EnableViewState="False" ErrorMessage="Required"
SetFocusOnError="True"></asp:RequiredFieldValidator>

</ItemTemplate>

<ItemStyle Width="85%" />

<HeaderStyle Width="85%" />

</asp:TemplateField>

</Columns>

</asp:GridView>

<hr />

<asp:Label ID="ClientIdLabel" runat="server" Text='<%# Bind("MemberID") %>'
Visible="False"></asp:Label><br />

<asp:Label ID="EpisodeNumberLabel" runat="server" Text='<%#
Bind("EpisodeNumber") %>' Visible="False"></asp:Label>

<asp:Label ID="AgencyNumberLabel" runat="server" Text='<%#
Bind("AgencyNumber") %>' Visible="False"></asp:Label>

<asp:Label ID="TPIdLabel" runat="server" Text='<%# Bind("TPId") %>'
Visible="False"></asp:Label><br />

<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert"

Text="Insert" OnClick="InsertButton_Click">

</asp:LinkButton>&nbsp;

</InsertItemTemplate>


--

AG
Email: discuss at adhdata dot com
 
S

Steven Cheng[MSFT]

Thanks for your reply AG,

I've reviewed the InsertTemplate template you provided, it does be much
more complex than I've expected. For those top level sub controls in the
InsertTemplate(such as those dropdownlist), I think directly two-way
databinding should work. However, you also put a nested GridView in it,
will you need to pull out values from those text field in each GridView
row(as parameter) when performing the inserting? If so, I'm afraid directly
two-way databinding can not help here. For such scenario, the preferred way
is to use custom code to locate those nested control field and extract
value from them (in ItemUpdating event handler). Actually, you do not to
loop all the controls, as you know the control structure, you only need to
query the necessary controls.

Please feel free to let me know if you have any further questions here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Similar Threads

FormView insert with UpdatePanel 0
FormView after insert 0
Formview hijinks 1
FormView 3
FormView Insert Session value 1
FormView 0
FormView and Master Page 0
Getting at Formview Data 0

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top