In this tutorial we can use the ListView control to
insert new records into a database table. The ListView control supports
an InsertItemTemplate. We will demonstrate how you can use the
InsertItemTemplate to create a simple customer feedback form.
How to use the InsertItemTemplate for a Comments Page
In this tutorial we can use the ListView control
to insert new records into a database table. The ListView control
supports an InsertItemTemplate. We will demonstrate how you can use the
InsertItemTemplate to create a simple customer feedback form.
This tutorial will show you how to use the InsertItemTemplate
using a ListView to create a simple Comment form in ASP.NET 3.5 Web
Application C#
To start, open Visual Studio 2008. File > New > Web Site >
ASP.NET Web site and name it InsertLV or whatever you want.
Assuming that you have a table named Comments with an id, comment,
and name columns we can begin. This time we can start with adding a
connectionString to your web.config to create a path to the database:
<connectionStrings>
<add name="CommentsConnectionString" connectionString="Data
Source=(local);Initial Catalog=Comments;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
|
Now swtich to the default.aspx, Source view and let’s edit the
markup to look something similar to below. In order to specify the
custom template declaratively, you need to add an InsertItemTemplate
element inside the ListView element. Then you can then add the contents
of the template to the InsertItemTemplate element. This way you could
associate a field with an input control by using a two-way binding
expression.
We used over 10 web hosting companies before we found Server Intellect..
Their dedicated servers and add-ons were setup swiftly, in less than 24
hours. We were able to confirm our order over the phone. They respond
to our inquiries within an hour. Server Intellect customer support and assistance are the best we've ever experienced.
The InsertItemTemplate template will usually contain an input
control for the user to enter the values for a new record. This usually
contains buttons or hyperlinks to insert the record and to cancel the
insert operation. In this example we will only use one submit button to
display the comment and record it to the database.
Within this example we can notice that when a record is inserted,
the ListView control automatically extracts the field value from the
associated input control.
<div>
<asp:ListView
ID="lstFeedback"
DataSourceId="dbComments"
InsertItemPosition="FirstItem"
runat="server">
<LayoutTemplate>
<asp:Placeholder
id="itemPlaceholder"
runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div class="comment">
<%# Eval("comment") %>
</div>
</ItemTemplate>
<InsertItemTemplate>
<div>
<fieldset>
<Legend>
Please enter any comments
or questions here:</Legend>
<asp:Label
id="lblComments"
Text="Comments:"
AssociatedControlID="tbxComment"
Runat="server" />
<asp:TextBox
id="tbxComment"
Text='<%# Bind("comment") %>'
TextMode="MultiLine"
Columns="40"
Rows="3"
Runat="server" />
<br />
<asp:Button
id="lnkInsert"
Text="Add Comment"
CommandName="Insert"
Runat="server" />
</fieldset>
</div>
</InsertItemTemplate>
</asp:ListView>
<asp:SqlDataSource
id="dbComments"
SelectCommand="SELECT id, comment FROM Comments"
InsertCommand="INSERT Comments (comment) VALUES (@comment)"
ConnectionString="<%$ ConnectionStrings:CommentsConnectionString %>"
Runat="server" />
</div>
|
Notice how the InsertItemTemplate appears only when you set the
ListView’scontrol’s InsertItemPosition property. This property can be
set to the value of the FirstItem, LastItem or None. Above we set the
value of FirstItem so that the insert form appears above all current
items.
The InsertItemTemplate contains a single TextBox control that has
its text property set to a data-binding expression. The template also
has a Button control that has a CommandName property set to the value
Insert. Now when you click the button, new items are inserted into the
database.
We moved our Web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
|