<html>
<head>
<title>FormView InsertItemTemplate Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
void Page_Load ( ) {
if ( ! IsPostBack ) {
if ( msgView.DataItemCount == 0 )
msgView.ChangeMode ( FormViewMode.Insert );
}
}
void setDate ( Object src, FormViewInsertEventArgs e ) {
messages.InsertParameters [ "MessageDate" ].DefaultValue = DateTime.Now.ToString ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>FormView <span class="hilite">InsertItemTemplate</span> Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<asp:formview id="msgView" runat="server"
datasourceid="messages"
datakeynames="MessageID"
width=80% cellpadding=5
gridlines="horizontal"
allowpaging
oniteminserting="setDate">
<rowstyle
backcolor="khaki"
borderstyle="outset"
borderwidth="1px"
horizontalalign="center" />
<insertrowstyle
backcolor="lightgreen"
borderstyle="inset"
borderwidth="1px"
horizontalalign="center" />
<itemtemplate>
<table width=100% cellpadding=5 border=0>
<col width=25% valign="top" align="right">
<tr>
<td>Date:</td>
<td><b><%# Eval ( "MessageDate" ) %></b></td></tr>
<tr>
<td>Name:</td>
<td><b><%# Eval ( "MessageFrom" ) %></b></td></tr>
<tr>
<td>Email:</td>
<td><b><%# Eval ( "Email" ) %></b></td></tr>
<tr>
<td>Subject:</td>
<td><b><%# Eval ( "MessageSubject" ) %></b></td></tr>
<tr>
<td>Message:</td>
<td><b><%# Eval ( "MessageBody" ) %></b></td></tr>
<tr>
<td colspan="2">
<asp:button id="newbutton"
text=" Add "
commandname="New"
runat="server" />
</td></tr>
</table>
</itemtemplate>
<insertitemtemplate>
<h5>To save changes to the database, press Add New.</h5>
<table width=100% cellpadding=5 border=0>
<col width=25% valign="top" align="right">
<tr>
<td>Name:</td>
<td><asp:textbox maxlength=254 id="msgFrom" runat="server"
text=<%# Bind ( "MessageFrom" ) %> />
<asp:requiredfieldvalidator
controltovalidate="msgFrom"
errormessage="Sender's name cannot be blank. "
runat="server">*</asp:requiredfieldvalidator></td></tr>
<tr>
<td>Email:</td>
<td><asp:textbox maxlength=254 id="msgEmail" runat="server"
text=<%# Bind ( "Email" ) %> />
<asp:regularexpressionvalidator
controltovalidate="msgEmail"
validationexpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid e-mail address. "
runat="server">*</asp:regularexpressionvalidator></td></tr>
<tr>
<td>Subject:</td>
<td><asp:textbox maxlength=254 id="msgSubject" runat="server"
text=<%# Bind ( "MessageSubject" ) %> /></td></tr>
<tr>
<td>Message:</td>
<td><asp:textbox id="msgBody" textmode="multiline" rows=5 columns=50 runat="server"
text=<%# Bind ( "MessageBody" ) %> /></td></tr>
<tr>
<td colspan="2">
<asp:button id="insertbutton"
text=" Add New "
commandname="Insert"
runat="server" />
<asp:button id="cancelbutton"
text=" Cancel "
commandname="Cancel"
runat="server" />
</td></tr>
</table>
<asp:validationsummary runat="server"
displaymode="SingleParagraph"
showmessagebox=true />
</insertitemtemplate>
</asp:formview>
<asp:sqldatasource id="messages" runat="server"
selectcommand="SELECT * FROM aspx_messages"
insertcommand="INSERT INTO aspx_messages
( MessageDate, MessageFrom, Email, MessageSubject, MessageBody )
VALUES ( @MessageDate, @MessageFrom, @Email, @MessageSubject, @MessageBody )"
connectionstring="<%$ ConnectionStrings:aspnet %>">
<insertparameters>
<asp:parameter name="MessageDate" type="DateTime" defaultvalue="" />
</insertparameters>
</asp:sqldatasource>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>