?? 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.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)
{
Console.WriteLine("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)
{
Console.WriteLine("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(); //創(chuàng)建SqlDataAdapter 類
myCommand.SelectCommand=new SqlCommand("sp_selHlArticle",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充數(shù)據(jù)集
myCommand.Fill(ds,"Article");
MyDataGrid.DataSource=ds; //綁定
MyDataGrid.DataBind();
lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"頁";
lblPageCount.Text="/共"+MyDataGrid.PageCount+"頁";
conn.Close();
}
catch(SqlException e)
{
Console.WriteLine("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(); //創(chuàng)建SqlDataAdapter 類
myCommand.SelectCommand=new SqlCommand("sp_selNewArticle",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充數(shù)據(jù)集
myCommand.Fill(ds,"Article");
DataList1.DataSource=ds; //綁定
DataList1.DataBind();
conn.Close();
}
catch(SqlException e)
{
Console.WriteLine("Exception in Main: " + e.Message); //出錯處理
}
}
public void PagerButtonClick(Object sender, EventArgs e) //分頁顯示
{
//獲得LinkButton的參數(shù)值
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(); //創(chuàng)建SqlDataAdapter 類
myCommand.SelectCommand=new SqlCommand("sp_getHotNews",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet ds=new DataSet(); //建立并填充數(shù)據(jù)集
myCommand.Fill(ds,"Article");
Dlist.DataSource=ds;
Dlist.DataBind();
conn.Close();
}
catch(SqlException e)
{
Console.WriteLine("Exception in Main: " + e.Message); //出錯處理
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:該調(diào)用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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 + -