📄 default.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebNews
{
/// <summary>
/// index1 的摘要说明。
/// </summary>
public class index1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm fd;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.DataList Dlist;
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.LinkButton LinkButton3;
protected System.Web.UI.WebControls.Label lblPageCount;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
getHlArticle();
getNewArticle();
getHotNews();
}
}
public void ViewClass() //显示分类
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlCommand cmd=new SqlCommand("sp_selFclassAll",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader rd;
rd=cmd.ExecuteReader();
while (rd.Read())
{
Response.Write("<img src=pic/de.gif><a href=list.aspx?classname="+rd.GetString(1)+"><font bold=true>"+rd.GetString(1)+"</font></a><br>");
}
rd.Close();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
public void ListClass(string de) //显示分类
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlCommand cmd=new SqlCommand("sp_selFclassAll",conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader rd;
rd=cmd.ExecuteReader();
while (rd.Read())
{
Response.Write(de+"<a href=list.aspx?classname="+rd.GetString(1)+">"+rd.GetString(1)+"</a>");
}
rd.Close();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
private void getHlArticle() //得到头条新闻
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_selHlArticle",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Article");
MyDataGrid.DataSource=ds; //绑定
MyDataGrid.DataBind();
lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"页";
lblPageCount.Text="/共"+MyDataGrid.PageCount+"页";
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
private void getNewArticle() //得到最新新闻
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_selNewArticle",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Article");
DataList1.DataSource=ds; //绑定
DataList1.DataBind();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
public void PagerButtonClick(Object sender, EventArgs e) //分页显示
{
//获得LinkButton的参数值
string arg = ((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
case("first"):
MyDataGrid.CurrentPageIndex =0;
break;
}
getHlArticle();
}
private void getHotNews() //得到最热新闻
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_getHotNews",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Article");
Dlist.DataSource=ds;
Dlist.DataBind();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_SelectedIndexChanged(object sender,System.EventArgs e)
{
getHlArticle();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -