我正在开发一个从6列数据库生成数据的网站。 其中一列是shortDescription,另一列是FullDescription。我在shortDescription旁边添加了一个链接,“更多”,它必须带到另一个页面,并且每个特定条目只显示数据库中的FullDescription ..下面是代码:
public partial class Events : System.Web.UI.Page
{
public SqlDbType EventID { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection Connection;
SqlCommand Command;
SqlDataReader Reader;
string sql = System.Configuration.ConfigurationManager.ConnectionStrings["EventDBConnectionString"].ConnectionString;
Connection = new SqlConnection(sql);
string CommandString = "SELECT * FROM Event ";
Command = new SqlCommand(CommandString, Connection);
Command.CommandType = CommandType.Text;
Command.Connection = Connection;
Command.Connection.Open();
Command.ExecuteNonQuery();
string html = "<table>";
html += "<tr><td>Name</td><td>Type</td><td>NumTickets</td><td>EventDate</td><td>ShortDescription</td><td>ImageLocation</td></tr>";
Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
if(Reader.HasRows)
{
while (Reader.Read())
{
html += "<tr><td>"+ Reader["Name"]+"</td>";
html += "<td>" + Reader["Type"] + "</td>";
html += "<td>" + Reader["NumTickets"] + "</td>";
html += "<td>" + Reader["EventDate"] + "</td>";
html += "<td>" + Reader["ShortDescription"] + " <a href='Events.aspx?id" + Reader["EventID"] + "'> More</a>" + "</td>";
html += "<td><img src='App_Media\\" + Reader["ImageLocation"] +"'>"+ "</td><tr>";
}
html += "</table>";
movieString.InnerHtml = html;
}
Command.Connection.Close();
Command.Dispose();
Connection.Dispose();
}
}
}
如果有人知道如何使用网址参数实现这一目标,那么您的输入将非常受欢迎。
答案 0 :(得分:0)
你需要创建另一个页面:EventDetails.aspx并从url获取id值以显示长描述。
在EventDetails.aspx页面内,您必须检索一行(按id)并使用Web控件显示信息,类似于Events.aspx页面,但为Sql命令添加WHERE子句。
示例代码:
Events.aspx
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = System.Configuration.ConfigurationManager.ConnectionStrings["EventDBConnectionString"].ConnectionString;
var connection = new SqlConnection(sql);
var commandString = " SELECT * FROM Event ";
var Command = new SqlCommand(commandString, connection);
Command.CommandType = CommandType.Text;
Command.Connection = connection;
Command.Connection.Open();
Command.ExecuteNonQuery();
var html = "<table><tr><td>Name</td><td>Type</td><td>NumTickets</td><td>EventDate</td><td>ShortDescription</td><td>ImageLocation</td></tr>";
var Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
if (Reader.HasRows)
{
while (Reader.Read())
{
html += "<tr><td>" + Reader["Name"] + "</td>";
html += "<td>" + Reader["Type"] + "</td>";
html += "<td>" + Reader["NumTickets"] + "</td>";
html += "<td>" + Reader["EventDate"] + "</td>";
html += "<td>" + Reader["ShortDescription"] + " <a href='Events.aspx?id=" + Reader["EventID"] + "'> More</a>" + "</td>";
html += "<td><img src='App_Media\\" + Reader["ImageLocation"] + "'>" + "</td><tr>";
}
html += "</table>";
movieString.InnerHtml = html;
}
Command.Connection.Close();
Command.Dispose();
connection.Dispose();
}
}
}
Details.aspx
public partial class Details : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var sql = System.Configuration.ConfigurationManager.ConnectionStrings["EventDBConnectionString"].ConnectionString;
var connection = new SqlConnection(sql);
var commandString = " SELECT * FROM Event WHERE EventID = @eventID ";
var eventID = -1;
try
{
eventID = Convert.ToInt32(Request.QueryString["id"]);
}
catch
{
eventID = -1;
}
var command = new SqlCommand(commandString, connection);
command.CommandType = CommandType.Text;
command.Connection = connection;
command.Parameters.Add(new SqlParameter("@eventID", eventID));
command.Connection.Open();
command.ExecuteNonQuery();
var html = "<table><tr><td>Name</td><td>Type</td><td>NumTickets</td><td>EventDate</td><td>ShortDescription</td><td>ImageLocation</td></tr>";
var reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{
reader.Read();
// display values in controls
movieString.InnerHtml = html;
}
command.Connection.Close();
command.Dispose();
connection.Dispose();
}
}
}
请不要使用&#34; SELECT * FROM ...&#34;,有关使用这种选择的性能问题,请为每个列名提供一个select语句。
此代码还有其他改进: