使用asp.net在网页中进行网格视图控制的页码? [英] page numbers for grid view control in the web page using asp.net?

查看:54
本文介绍了使用asp.net在网页中进行网格视图控制的页码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



使用asp.net进行网格视图控制的页码



这里我们使用两个下拉列表框和一个网格视图



和一旦选择汽车在dropdownlist1中显示网格视图中的所有汽车详细信息和下拉列表2也选择从高到低和低高选择任何一个将在gridview控件中显示记录正确显示输出但页码不正常



这里下面的设计和代码请检查回复这种方式是否正确和任何其他方式请回复我





设计:

Dear All,

page numbers for grid view control using asp.net

here we use two drop down list boxes and one grid view

and once select cars in dropdownlist1 display all cars details in grid view and drop down list2 also select high to low and low to high select any one will display records in gridview control displaying output properly but page numbers is not working properly

here below design and code please check reply this way correct or not and any another way please reply me


design:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

public partial class Cars : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            BindGridView();
            PriceList();
        }
    }
    private void LoadData()
    {
        string constr = "Data Source=pmnb3-PC;Database=Save;user id=sa;password=123;";
        string query = "SELECT ID, Name, Address, Email, Mobile, Category,Price FROM PostTable";

        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
        BindGridView();
        PriceList();
    }
    protected void BindGridView()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        if (DropDownList1.SelectedItem.Text == "Cars")
        {
            cmd = new SqlCommand("Select * From PostTable where Category='Cars'", con);
        }
        else if (DropDownList1.SelectedItem.Text == "Mobiles")
        {
            cmd = new SqlCommand("select * from PostTable where Category='Mobiles'", con);
        }
        else if (DropDownList1.SelectedItem.Text == "--Select--")
        {
            cmd = new SqlCommand("select * from PostTable", con);
        }

        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView();
    }
    protected void PriceList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        if (DropDownList2.SelectedItem.Text == "Price: High To Low")
        {
            cmd = new SqlCommand("Select * From PostTable order by price desc", con);
        }
        else if (DropDownList2.SelectedItem.Text == "Price: Low To High")
        {
            cmd = new SqlCommand("select * from PostTable order by price asc", con);
        }
        else if (DropDownList2.SelectedItem.Text == "Most Recently Ads")
        {
            cmd = new SqlCommand("select * from PostTable order by id desc", con);
        }

        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        PriceList();
    }
}

推荐答案

确保已设置Gridview1.AllowPaging = True;

然后合并你的LoadData(); BindGridView();价目单价目表();一个功能,如



Make sure you have set Gridview1.AllowPaging=True;
Then merge your LoadData(); BindGridView(); PriceList(); to One single function like

protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
          BindGridView();

       }
   }


   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       BindGridView();
   }

   protected void BindGridView()
   {
       string strCmd ="SELECT ID, Name, Address, Email, Mobile, Category,Price FROM PostTable WHERE (Category=@Category OR @Category IS NULL) ";

       if (DropDownList2.SelectedItem.Text == "Price: High To Low")
           strCmd=strCmd+"order by price desc";
       else if (DropDownList2.SelectedItem.Text == "Price: Low To High")
           strCmd=strCmd+"order by price asc";
       else
           strCmd=strCmd+"order by id desc";


       using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
       {
           using(SqlDataAdapter adap = new SqlDataAdapter(strCmd,con))
           {
                if (DropDownList1.SelectedItem.Text == "--Select--")
                     adap.SelectCommand.Parameters.Add("@Category",SqlDbType.NVarChar,50).Value=DBNull.Value;
                else if (DropDownList1.SelectedItem.Text == "Mobiles")
                     adap.SelectCommand.Parameters.Add("@Category",SqlDbType.NVarChar,50).Value=DropDownList1.SelectedItem.Text;

               using(DataTable dtbl =new DataTable())
               {
                   adap.Fill(dtbl);
                    GridView1.DataSource = dtbl;
                    GridView1.DataBind();
               }
           }
       }


   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       BindGridView();
   }

   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
   {
       BindGridView();
   }


这篇关于使用asp.net在网页中进行网格视图控制的页码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆