We can leverage the use of the Asp.Net’s Repeater control and use ADO.Net to save/retrieve
data.
The code is self-explanatory and necessary comments are provided at all apt places.
This would be the markup of the page. Ignore the banner, footer and left menu user
control markup. Otherwise it would consist of a Repeater control with some required
field validators. The interesting snippets are highlighted in green.
<%@ Register TagPrefix="uc1" TagName="Footer" Src="./UserControls/Footer.ascx" %>
<%@ Register TagPrefix="uc1" TagName="Menus" Src="./UserControls/Menus.ascx" %>
<%@ Register TagPrefix="uc1" TagName="LeftMenu" Src="./UserControls/LeftMenu.ascx" %>
<%@ Register TagPrefix="AIG" TagName="Banner" Src="./UserControls/Banner.ascx" %>
<%@ Page language="c#" Codebehind="Suggestion.aspx.cs" AutoEventWireup="false" Inherits="Samples.Suggestions" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML xmlns:o>
<HEAD>
<title>Suggestion Page</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="./CSS/MainCss.css" type="text/css" rel="stylesheet">
<script language="JavaScript" src="./Scripts/Scripts.js" type="text/javascript"></script>
</HEAD>
<body bgColor="lightgrey" MS_POSITIONING="GridLayout">
<form id="frmSuggestions" method="post" runat="server">
<TABLE id="Parent" cellSpacing="0" cellPadding="0" width="100%" border="0" height="0">
<tr>
<td>
<aig:banner id="Banner1" runat="server"></aig:banner>
</td>
</tr>
<TR>
<TD vAlign="top" bgColor="#58608a" colSpan="11" height="1"> </TD>
</TR>
<tr>
</tr>
</TABLE>
<table id="navigation" cellSpacing="0" cellPadding="0" border="0">
<tr vAlign="top">
<td align="left" valign="top">
<uc1:LeftMenu id="LeftMenu1" runat="server"></uc1:LeftMenu>
</td>
<td align="left" valign="top">
<table border="0" align="left">
<tr>
<td vAlign="top" align="left">
<uc1:Menus id="Menus1" runat="server"></uc1:Menus>
<table>
<TBODY>
<TR>
<TD class="standard-text" align="right">
</TD>
<TD>
</TD>
</TR>
<tr>
<th class="standard-text" align="right">
Enter your Comment:</td>
<td><asp:textbox id="txtSuggestions" runat="server" CssClass="standard-text" size="35" Height="50px"
Width="350px" TextMode="MultiLine"></asp:textbox>
<asp:requiredfieldvalidator id="validSuggestionsRequired" runat="server" display="Dynamic" errormessage="Please enter a Comment/Suggestion"
ControlToValidate="txtSuggestions">
</asp:requiredfieldvalidator></td>
</tr>
<TR>
<TD class="standard-text" align="right"> </TD>
<TD> </TD>
</TR>
<TR>
<TD class="standard-text" align="right"> </TD>
<TD><asp:imagebutton id="ibPostComment" runat="server" ImageAlign="AbsBottom" ImageUrl="./Images/btnPost.gif"></asp:imagebutton><asp:label id="lblError" runat="server" ForeColor="Red" EnableViewState="False"></asp:label></TD>
</TR>
</table>
<P>
<asp:repeater id="rptSuggestions" runat="server">
<HeaderTemplate>
<h3 class="standard-text">Your Comments/Suggestions:</h3>
</HeaderTemplate>
<ItemTemplate>
<fieldset><legend><img alt="Comments" src="./images/Comments.gif"><strong>
<%# DataBinder.Eval(Container.DataItem, "EnteredAt") %>
</strong>
</legend>
<table>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "Suggestion")%>
</td>
<br>
</table>
</fieldset>
<br>
</ItemTemplate>
<FooterTemplate>
<hr />
</FooterTemplate>
</asp:repeater></P>
<P></P>
<TR>
<TD vAlign="top" align="left"></TD>
</TR>
<TR>
<TD vAlign="top" align="left" width="100%" colSpan="10"></TD>
</TR>
</table>
</TD></TR></TBODY></TABLE></TD>
<TD align="right">
</TD>
</TR></TABLE>
<uc1:Footer id="Footer1" runat="server"></uc1:Footer>
</form>
</body>
</HTML>
And this is the code-behind of the markup shown above.
using System;
using sqlhelper;
using System.Xml;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Drawing;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Samples
{
/// <summary>
/// Summary description for Suggestions.
/// </summary>
public class Suggestions : System.Web.UI.Page
{
#region declarations
protected System.Web.UI.WebControls.Repeater rptSuggestions;
private DataSet dsSuggestions = new DataSet();
private string conn = ConfigurationSettings.AppSettings["ConnectionString"];
protected System.Web.UI.WebControls.TextBox txtSuggestions;
protected System.Web.UI.WebControls.RequiredFieldValidator validSuggestionsRequired;
protected System.Web.UI.WebControls.ImageButton ibPostComment;
protected System.Web.UI.WebControls.Label lblError;
#endregion declarations
//Page load event bind the list of suggestions to the repeater
private void Page_Load(object sender, System.EventArgs e)
{
txtSuggestions.Text = "a\r\n b";
if (!IsPostBack)
{
BindData();
}
}
//the bind data function retrieves the list of suggestions from the database and
updates it in the repeater
private void BindData()
{
dsSuggestions = SqlHelper.ExecuteDataset(conn, "RetrieveSuggestions", userID, String.Empty, 0);
rptSuggestions.DataSource = dsSuggestions;
rptSuggestions.DataBind();
}
//Use the post comment image click event to save the new comment to the database
private void ibPostComment_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
SqlHelper.ExecuteNonQuery(conn,"CallAnSPToSaveData",userID,txtSuggestions.Text , 1);
BindData();
}
catch(Exception ex)
{
lblError.Text = ex.Message;
}
}
#endregion CustomCode
} //end of class
}//end of namespace