using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind(0,name);
            pageindex = 0;
            name = "";
        }
    }    private void Bind(int index,string ename)
    {
        using (SqlConnection con = new SqlConnection("server=.;database=News;uid=sa;pwd=123"))
        {
            con.Open();
            if (ename == null || ename == "" )
            {
                string sql = string.Format("select top 10 * from newsinfo where newsID not in (select top {0} newsID from newsinfo)", index);
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();                if (pageindex == (pagecount - 1))
                {
                    LinkButton1.Enabled = false;
                    LinkButton2.Enabled = true;
                }
                else if (pageindex == 0)
                {
                    LinkButton1.Enabled = true;
                    LinkButton2.Enabled = false;
                }
                else if (pageindex != 0 && pageindex != (pagecount - 1))
                {
                    LinkButton1.Enabled = true;
                    LinkButton2.Enabled = true;
                }
                string sqlstr = "select count(*) from newsinfo";
                SqlCommand cmd = new SqlCommand(sqlstr, con);
                int count = int.Parse(cmd.ExecuteScalar().ToString());
                pagecount = count % 10 == 0 ? count / 10 : (count / 10 + 1);
            }
            else
            {
                string sql = string.Format("select top 10 * from newsinfo where newsID not in (select top {0} newsID from newsinfo ) and newstype='{1}'", index, name);
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();                if (pageindex == (pagecount - 1))
                {
                    LinkButton1.Enabled = false;
                    LinkButton2.Enabled = true;
                }
                else if (pageindex == 0)
                {
                    LinkButton1.Enabled = true;
                    LinkButton2.Enabled = false;
                }
                else if (pageindex != 0 && pageindex != (pagecount - 1))
                {
                    LinkButton1.Enabled = true;
                    LinkButton2.Enabled = true;
                }
                string sqlstr = string.Format("select count(*) from newsinfo where newstype={0}",name);
                SqlCommand cmd = new SqlCommand(sqlstr, con);
                int count = int.Parse(cmd.ExecuteScalar().ToString());
                pagecount = count % 10 == 0 ? count / 10 : (count / 10 + 1);
            }        }
    }
    static int pageindex;
    static int pagecount;
    static string name;    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        pageindex = pageindex + 1;
        Bind(pageindex * 10, name);
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        pageindex = pageindex - 1;
        Bind(pageindex * 10, name);
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        pageindex = 0;
        Bind(pageindex * 10, name);
    }
    protected void LinkButton4_Click1(object sender, EventArgs e)
    {
        pageindex = pagecount - 1;
        Bind(pageindex * 10, name);
    } 
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  //选择不同的新闻类型
    {
        name = DropDownList1.SelectedValue.ToString();
        Label1.Text = name;
        Bind(0, name);
    }
}

解决方案 »

  1.   

    问题补充最后的这段代码是判断新闻类型的
    选择不同的类型,查询出不同类型的新闻,然后给不同类型的新闻分页功能
    当然,首页加载的时候是查询所有的新闻的
    当改变DropDownList1的时候会变化(我的设计),但是
     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  //选择不同的新闻类型 
        { 
            name = DropDownList1.SelectedValue.ToString(); 
            Label1.Text = name; 
            Bind(0, name); 
        } 
    这段代码的name取不到值 那是为什么?
    请指教
    代码有些冗余
    勉强看看
    是做分页实验用的
      

  2.   

    可能是DropDownList1.SelectedValue.ToString(); 这个取不到值吧 你调试测试一哈