生成无限制的标签

时间:2011-08-31 06:30:16

标签: asp.net comments

我编写了评论页面。它的工作正常但所有SELECTED记录都是使用Response.Write(dr[0].ToString());显示但是使用Response.Write我们无法按顺序管理它们。所以,我决定在Labels中显示记录,但我对使用分页不感兴趣。那么,现在要在每次更新记录时自动添加新的Set Labels(即set是一组标签名称,电子邮件,网站和评论)?

我也尝试使用Labels代替Response.Write,但在标签中只显示最后一条记录。

请告诉我如何在每组labels中显示每条记录。

我的代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;

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=test_com;" + "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();
            }
            while (dr.Read())
            {
                Response.Write(dr[0].ToString());
                Response.Write(dr[1].ToString());
                Response.Write(dr[2].ToString());
                Response.Write(dr[3].ToString());

                /* test using labels. Being displayed only last record. 
                Label1.Text = dr[0].ToString();
                Label2.Text = dr[1].ToString();
                Label3.Text = dr[2].ToString();
                Label4.Text = dr[3].ToString();
                */
            }
    }
    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=test_com;" + "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();
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

Label1.Text += dr[0].ToString() + "<br/>";
Label2.Text += dr[1].ToString() + "<br/>";
Label3.Text += dr[2].ToString() + "<br/>";
Label4.Text += dr[3].ToString() + "<br/>";

+=运算符,用于将文字附加到Labels<br/>的现有文字以进行换行

答案 1 :(得分:0)

我建议你抓住你的大部分内容并使用转发器。

按照以下基本方法使用转发器文章http://www.sitepoint.com/asp-net-repeater-control/

答案 2 :(得分:0)

您是否可以使用绑定到数据源的转发器控件来检索您当前使用OdbcConnection获取的记录?然后使用它们的索引或其他标记迭代它们。

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/repeater.aspx