注:新闻发布程序往往需要查看新闻条目,这里介绍一个简单的查看程序。
空网页:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showNews.aspx.cs" Inherits="showNews" %>
<!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>
<link href="css/css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 500px; height: 200px">
<tr>
<td align="center" colspan="3" style="height: 30px;" >
<asp:Label ID="labTitle" runat="server" CssClass="title" Text="Label"
Width="373px"></asp:Label></td>
</tr>
<tr>
<td colspan="3">
<asp:TextBox ID="txtContent" runat="server" CssClass="txt" Height="159px" TextMode="MultiLine"
Width="486px"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 144px; height: 19px">
</td>
<td align="center" style="width: 12px; height: 19px">
<asp:Button ID="btnClose" runat="server" OnClick="btnClose_Click" Text="关闭窗口" Width="103px" /></td>
<td style="width: 127px; height: 19px">
</td>
</tr>
</table>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //添加类库
public partial class showNews : System.Web.UI.Page
{
CommonClass CC = new CommonClass();
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = CC.GetDataSet("select * from tb_News", "tbNews");
//DataRow[] row = ds.Tables["tbNews"].Select("id=" + Request.QueryString["id"]);
DataRow[] row = ds.Tables["tbNews"].Select("id=" + "190");
//DataRow[] row = ds.Tables["tbNews"].Select("id = 'dlSZ'");
foreach (DataRow rs in row)
{
this.Page.Title = rs["title"].ToString();
this.labTitle.Text = rs["title"].ToString();
this.txtContent.Text =" "+ rs["content"].ToString();
}
}
protected void btnClose_Click(object sender, EventArgs e)
{
Response.Write("<script language=javascript>window.close()</script>");
}
}
公共类代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// CommonClass 的摘要说明
/// </summary>
public class CommonClass
{
public CommonClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 连接数据库
/// </summary>
/// <returns>返回SqlConnection对象</returns>
public SqlConnection GetConnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
/// <summary>
/// 说明:ExecSQL用来执行SQL语句。
/// 返回值:操作是否成功(True\False)。
/// 参数:sqlStr SQL字符串
/// </summary>
public Boolean ExecSQL(string sqlStr)
{
SqlConnection myConn = GetConnection();
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
try
{
myCmd.ExecuteNonQuery();
myConn.Close();
}
catch
{
myConn.Close();
return false;
}
return true;
}
/// <summary>
/// 说明:GetDataSet数据集,返回数据源的数据集
/// 返回值:数据集DataSet
/// 参数:sqlStr SQL字符串,TableName 数据表名称
/// </summary>
public System.Data.DataSet GetDataSet(string sqlStr, string TableName)
{
SqlConnection myConn =GetConnection();
myConn.Open();
SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, myConn);
DataSet ds = new DataSet();
adapt.Fill(ds, TableName);
myConn.Close();
return ds;
}
}