Background color to comment
I am using ASP.NET repeater function. Its working fine. But here all the comments are coming one by one without any separation. I need each comment to be placed in a separate box and to add back ground color to that box. Just like in wordpress comment page how the comments are separated. My complete code follows please tell me how to separate each comment.
comments.aspx
<html>
<body>
<form id="form1" runat="server">
<asp:Repeater id="Repeater1" runat="server" >
<HeaderTemplate>
<table border=1>
</HeaderTemplate>
<ItemTemplate>
<tr>
<%# DataBinder.Eval(Container.DataItem, "Name") %> </br>
<%# DataBinder.Eval(Container.DataItem, "Val4") %> </br></br></br>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<br />
Name*:
<asp:TextBox ID="tb_name" runat="server" placeholder="Your Name"></asp:TextBox>
<br />
<br />
Email*:
<asp:TextBox ID="tb_email" runat="server"></asp:TextBox>
<br />
<br />
Website :
<asp:TextBox ID="tb_website" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="tb_comment" runat="server" Height="114px" TextMode="MultiLine"
Width="322px"></asp:TextBox>
<br />
<br />
<br />
<asp:CheckBox ID="cb_notify" runat="server"
Text="Notify me of followup comments via e-mail." />
<br />
<br />
<br />
<asp:Button ID="Submit" runat="server" onclick="Submit_Click"
Text="Post Comment" />
</form>
</body>
</html>
comments.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Collections;
public partial class comment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=softmail;" + "UID=root;" + "PASSWORD=********;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("Select name, email, website, comments from awm_comments", MyConnection);
OdbcDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
ArrayList values = new ArrayList();
while (dr.Read())
{
if (!IsPostBack)
{
string a = dr[0].ToString();
string b = dr[1].ToString();
string c = dr[2].ToString();
string d = dr[3].ToString();
values.Add(new PositionData(a, b, c, d));
Repeater1.DataSource = values;
Repeater1.DataBind();
}
}
}
protected void Submit_Click(object sender, EventArgs e)
{
string name = tb_name.Text;
string email = tb_email.Text;
string website = tb_website.Text;
string comment = tb_comment.Text;
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=softmail;" + "UID=root;" + "PASSWORD=******;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
OdbcCommand cmd = new OdbcCommand("INSERT INTO awm_comments(name, email, website, comments, notify)VALUES(?, ?, ?, ?, ?)", MyConnection);
cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = name;
cmd.P开发者_如何学Goarameters.Add("@alternate_email", OdbcType.VarChar, 255).Value = email;
cmd.Parameters.Add("@ipaddr", OdbcType.VarChar, 255).Value = website;
cmd.Parameters.Add("@security_question", OdbcType.VarChar, 255).Value = comment;
if (cb_notify.Checked == true)
{
int not = 1;
cmd.Parameters.Add("@security_question", OdbcType.Int, 11).Value = not;
}
else if (cb_notify.Checked == false)
{
int not = 0;
cmd.Parameters.Add("@security_question", OdbcType.Int, 11).Value = not;
}
MyConnection.Open();
cmd.ExecuteNonQuery();
MyConnection.Close();
Response.Redirect("comment.aspx");
}
public class PositionData
{
private string name;
private string ticker;
private string val3;
private string val4;
public PositionData(string name, string ticker, string val3, string val4)
{
this.name = name;
this.ticker = ticker;
this.val3 = val3;
this.val4 = val4;
}
public string Name
{
get
{
return name;
}
}
public string Ticker
{
get
{
return ticker;
}
}
public string Val3
{
get
{
return val3;
}
}
public string Val4
{
get
{
return val4;
}
}
}
}
Output
I would make such a thing like this
<ItemTemplate>
<div style="padding: 10px; margin: 5px; border: 1px dotted black; background: yellow;">
<%# DataBinder.Eval(Container.DataItem, "Name") %> </br>
<%# DataBinder.Eval(Container.DataItem, "Val4") %>
</div>
</ItemTemplate>
With this approach you can leave Footer and Header tempaltes. Style div as you like.
You can use a SeparatorTemplate (see: http://msdn.microsoft.com/en-us/library/c012haty(v=vs.71).aspx ) to add some markup for separating the comments.
If you want alternating colors etc., likewise you can use the AlternatingItemTemplate to change the colours for odd/even rows. Add different CSS classes to the TR and use your CSS for the colors.
(You might want to get rid of the table completely and use div's instead, but that is my personal taste)
I think you’re missing some HTML. I suspect your ASP.Net code is outputting HTML like this:
<table border=1>
<tr>
Name </br>
Val4 </br></br></br>
</tr>
</table>
You need table cell tags in there for the HTML to be valid:
<table border=1>
<tr>
<td>Name</td>
<td>Val4</td>
</tr>
</table>
And to make it look like you describe:
<table border=1 cellpadding=5 cellspacing=10>
<tr>
<td style="background-color: #ccc;">Name</td>
<td style="background-color: #ccc;">Val4</td>
</tr>
</table>
精彩评论