Generating unlimited labels
I coded for a comments page. Its working fine but all the SELECTED
records are being displayed using Response.Write(dr[0].ToString());
But using Response.Write
we cannot manage them in orderly manner. So, I decided to display records in Labels
but I am not interested to use pagination. So, now to automatically add a new Set
of Labels
(i.e. a set
is a set of labels name, email, website and comments) on every update of records?
I also tried using Labels
instead of Response.Write
but in Labels only last record is being displayed.
Please tell me how to display each record in each set of labels
.
My code follows.
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 labe开发者_StackOverflow社区ls. 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();
}
}
Try this:
Label1.Text += dr[0].ToString() + "<br/>";
Label2.Text += dr[1].ToString() + "<br/>";
Label3.Text += dr[2].ToString() + "<br/>";
Label4.Text += dr[3].ToString() + "<br/>";
+=
operator for appending text to existing text of Labels
and <br/>
for line break
I would suggest you scratch most of what you have and use a repeater.
Follow the following basic how to use a repeater article http://www.sitepoint.com/asp-net-repeater-control/
Could you use a repeater control bound to the datasource that retrieves the records you are currently getting using your OdbcConnection? Then use their indexes or some other tag to iterate throught them.
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/repeater.aspx
精彩评论