Jump to content

Formview Woes


Yahweh

Recommended Posts

Basically, I'm allowing users to leave comments on articles. So, I have a simple setup:- A formview for taking user input.- A checkbox on the formview to prevent spam from being posted.- If the checkbox is checked, then the formview inserts a new record.- If the checkbox is unchecked, then the formview cancels the insert, and the user is prompted to try again.Unfortunately, when I cancel the insert, all of the textboxes on the formview are cleared, and the user is forced to reenter their data again. I don't want that behavior, I want the textboxes to retain their viewstate when the insert is canceled, but there doesn't seem to be an easy way to do that.How do I force my formview to maintain its viewstate when an insert fails?Here is my formview:

<asp:FormView ID="FormView1" runat="server" DataSourceID="sqlSubmitComment" DefaultMode="Insert">	<InsertItemTemplate>		<table width="100%" cellspacing="0" cellpadding="5" border="0">				<tr>					<td colspan="2">						Leave a comment:					</td>				</tr>				<tr>					<td></td>					<td></td>					<td rowspan="3" width="40%" align="left" valign="top">						<p>						<small>YCodes: <br /></small>						</p>					</td>				</tr>				<tr>					<td width="10%" align="left" valign="top">						Name:					</td>					<td width="50%" align="left" valign="top">						<asp:TextBox ID="txtAuthor" runat="server" Text='<%# Bind("author") %>' Width="190px"></asp:TextBox></td>				</tr>				<tr>					<td width="10%" align="left" valign="top">						Comment:												<p>						<small>(HTML is OFF)</small>						</p>					</td>					<td width="50%" align="left" valign="top">						<asp:TextBox ID="txtComment" runat="server" Height="116px" Text='<%# Bind("comment") %>'							TextMode="MultiLine" Width="362px"></asp:TextBox></td>				</tr>				<tr>					<td colspan="2" align="center">						<asp:Panel runat="server" ID="pnlHuman">							<p>								Check this box if you're a human: 								<asp:CheckBox ID="chkHuman" runat="server" />							</p>						</asp:Panel>						<asp:Button CommandName="Insert" ID="Button1" runat="server" Text="Submit Comment" />					</td>				</tr>			</table>	</InsertItemTemplate>	</asp:FormView>	<asp:SqlDataSource ID="sqlSubmitComment" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNetConnectionString %>"		InsertCommand="Submit_New_Comment" InsertCommandType="StoredProcedure">		<InsertParameters>			<asp:QueryStringParameter DefaultValue="0" Name="postID" QueryStringField="id" Type="Int32" />			<asp:CookieParameter CookieName="author" Name="author" Type="String" />			<asp:Parameter Name="comment" Type="String" />			<asp:Parameter Name="IPAddress" Type="String" />		</InsertParameters>	</asp:SqlDataSource>

Protected Sub sqlSubmitComment_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles sqlSubmitComment.Inserting		Dim bTrusted As Boolean		'Checks a "trusted" cookie to see if its value = "true"		bTrusted = StringGrabber.Cookie("trusted").Equals("true", StringComparison.OrdinalIgnoreCase)		If bTrusted = False Then			'Couldn't find the cookie, or the cookies value was			'wrong, so validating the checkbox instead			Dim chkHuman As CheckBox = CType(FormView1.FindControl("chkHuman"), CheckBox)			bTrusted = chkHuman.Checked			'adding a cookie			Dim myCookie As New HttpCookie("trusted")			myCookie.Value = bTrusted.ToString			myCookie.Expires = DateTime.Now.AddDays(365)			Response.Cookies.Add(myCookie)			Dim txtAuthor As TextBox = CType(FormView1.FindControl("txtAuthor"), TextBox)			myCookie = New HttpCookie("author")			myCookie.Value = txtAuthor.Text			myCookie.Expires = DateTime.Now.AddDays(365)			Response.Cookies.Add(myCookie)		End If		e.Cancel = Not bTrusted 	End Sub

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...