前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebShowXx.aspx.cs" Inherits="GridWive.WebShowXx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<table>
<tr><td><div id="divtitle" runat="server"></div></td></tr>
<tr><td><div id="divcontent" runat="server"></div></td></tr>
</table>
</center>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace GridWive
{
public partial class WebShowXx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["id"]))
{
string id = Request.QueryString["id"];
LoadData(id);
}
}
}
private void LoadData(string id)
{
string strcon = @"Data Source=PC-DLL;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=linlin";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//每次都显式打开
conn.Open();
string sqlstr = "SELECT NewsTitle,NewsContent FROM T_News1 WHERE Id=@id";
cmd.CommandText = sqlstr;
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
divtitle.InnerHtml = "<h2>" + dt.Rows[0]["NewsTitle"].ToString() + "</h2>";
divcontent.InnerHtml = dt.Rows[0]["NewsContent"].ToString();
}
}
}