473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

FormView Insert

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

--

AG
Email: discuss at adhdata dot com


Feb 9 '07 #1
6 14411
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

On Feb 10, 4:28 am, "AG" <NOSPAMa-g...@newsgroups.nospamwrote:
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

--

AG
Email: discuss at adhdata dot com

Feb 10 '07 #2
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/lib...66(VS.80).aspx

http://davidhayden.com/blog/dave/arc...5/25/1051.aspx

http://www.manuelabadia.com/blog/Per...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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

Feb 12 '07 #3
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[MSFT]" <st*****@online.microsoft.comwrote in message
news:cO*************@TK2MSFTNGHUB02.phx.gbl...
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/lib...66(VS.80).aspx

http://davidhayden.com/blog/dave/arc...5/25/1051.aspx

http://www.manuelabadia.com/blog/Per...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/subscripti...ult.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/subscripti...t/default.aspx.

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

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

Feb 13 '07 #4
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.

Feb 13 '07 #5
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_Selec tedIndexChanged">

</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_Selecte dIndexChanged"
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

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:P1**************@TK2MSFTNGHUB02.phx.gbl...
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.

Feb 13 '07 #6
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.
Feb 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: le_mo_mo | last post by:
Hi, I am trying to insert some data using FormView which works fine but I do not know how to include hiddenfield and bind them in the construct so I can insert information like username, date,...
0
by: Guenter | last post by:
Hi, I am trying to use the ASP.NET 2.0 FormView as a method to enter data into my business object. At the moment, I am trying to do this without a custom DataSourceView control, but I can't...
1
by: Chris | last post by:
I have a formview based on an SQL Data Source see below. I have a formview bound to it. The item insert template has a command button with a command name insert. When I press it the insert doesn't...
0
by: Jason | last post by:
I'm reading that this is by design.. but It's just killing me. I see all sorts of nice events - onclick, onupdating, onupdating .. I can cancel the update with e.cancel etc... Great. So why...
5
by: J055 | last post by:
Hi How do I keep the values of the controls in a FormView after a PostBack in Insert mode? Thanks Andrew
0
by: =?Utf-8?B?UGF1bCBCdXp6YSwgb2xkc3RlciB1c2luZyBuZXcg | last post by:
I'm trying to retrieve the @@identity value of the just-inserted record using a FormView control on an .aspx page. Here's what I have tried..... --change in-line Insert in the SqlDataSource from...
2
by: Bishop | last post by:
ASP.NET 2.0 XP SP2 IIS W/IE7 Master Page I use the wizard to create the FormView and Datasource and set the default view to Insert. When I try to use the page, and click the insert link, I...
0
by: Dave | last post by:
How can I set the Value of a Textbox that resides in a FormView Insert Template? Thanks
1
by: d-42 | last post by:
Hi, I'm trying to use FormView to Insert an Object to an ObjectDataSource. The insert method signature is simply: void DALObject.InsertPerson(Person myObject) The person object for the...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, has anyone ever bound data to a formview that's in Insert Mode? For example, if you had to get default values from the database for a new record? thanks, rodchar
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.