要评论的背景颜色

时间:2011-08-31 09:35:38

标签: asp.net html css

我正在使用ASP.NET转发器功能。它的工作正常。但是这里所有的评论都是一个接一个地没有任何分离。我需要将每个注释放在一个单独的框中,并将底色添加到该框中。就像在wordpress评论页面中一样,评论是如何分开的。我的完整代码如下,请告诉我如何分开每条评论。

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.Parameters.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;
            }
        }
    }
}

输出

enter image description here

3 个答案:

答案 0 :(得分:1)

我会做这样的事情

<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>

使用这种方法,您可以保留页脚和标题的临时值。 你喜欢的风格div。

答案 1 :(得分:0)

您可以使用SeparatorTemplate(请参阅:http://msdn.microsoft.com/en-us/library/c012haty(v=vs.71).aspx)添加一些标记以分隔注释。

如果您想要交替颜色等,同样可以使用AlternatingItemTemplate来更改奇数/偶数行的颜色。将不同的CSS类添加到TR并使用CSS作为颜色。

(你可能想要完全摆脱桌子并使用div代替,但这是我的个人品味)

答案 2 :(得分:0)

我认为你错过了一些HTML。我怀疑你的ASP.Net代码正在输出这样的HTML:

<table border=1>
    <tr>
        Name </br>
        Val4 </br></br></br>
    </tr>
</table>

您需要表格单元格标签才能使HTML有效:

<table border=1>
    <tr>
        <td>Name</td>
        <td>Val4</td>
    </tr>
</table>

让它看起来像你描述的那样:

<table border=1 cellpadding=5 cellspacing=10>
    <tr>
        <td style="background-color: #ccc;">Name</td>
        <td style="background-color: #ccc;">Val4</td>
    </tr>
</table>