qzylalala 2015-12-12 12:10 采纳率: 0%
浏览 1839

如何不使用SqlDataSource绑定Datalist,实现查询功能

图片说明
通过这个食物种类和价格区间,查询符合要求的Datalist中的食物。
遇到的问题是,我本来用SqlDataSource控件绑定Datalist的,但是,由于我用了PageDataSource,有两个数据源了,冲突了,我就取消了Datalist前台的SqlDataSource控件。但是在这种情况下,我赶脚查询功能貌似我不会实现了······

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;

namespace OnlineOrderingSystem.预定服务
{
    public partial class OrderingService : System.Web.UI.Page
    {
        protected static PagedDataSource ps = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind(0);
            }

        }
        //进行数据绑定的方法
        public void Bind(int CurrentPage)
        {
            //实例化SqlConnection对象
            SqlConnection sqlCon = new SqlConnection();
            //实例化SqlConnection对象连接数据库的字符串
            sqlCon.ConnectionString = "server=(local);database=Order;uid=sa;pwd=;";
            //定义SQL语句
            string SqlStr = "select * from Food";
            //实例化SqlDataAdapter对象
            SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
            //实例化数据集DataSet
            DataSet ds = new DataSet();
            da.Fill(ds, "Food");

            ps.DataSource = ds.Tables["Food"].DefaultView;
            ps.AllowPaging = true; //是否可以分页
            ps.PageSize = 4; //显示的数量
            ps.CurrentPageIndex = CurrentPage; //取得当前页的页码

            this.DataList1.DataSource = ps;
            this.DataList1.DataKeyField = "FoodID";
            this.DataList1.DataBind();
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                //以下5个为 捕获用户点击 上一页 下一页等时发生的事件
                case "first"://第一页
                    ps.CurrentPageIndex = 0;
                    Bind(ps.CurrentPageIndex);
                    break;
                case "pre"://上一页
                    ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
                    Bind(ps.CurrentPageIndex);
                    break;
                case "next"://下一页
                    ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
                    Bind(ps.CurrentPageIndex);
                    break;
                case "last"://最后一页
                    ps.CurrentPageIndex = ps.PageCount - 1;
                    Bind(ps.CurrentPageIndex);
                    break;
                case "search"://页面跳转页
                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        int PageCount = int.Parse(ps.PageCount.ToString());
                        DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
                        int MyPageNum = 0;
                        MyPageNum = Convert.ToInt32(ddl.SelectedValue);
                        if (MyPageNum <= 0 || MyPageNum > PageCount)
                            Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
                        else
                            Bind(MyPageNum - 1);
                    }
                    break;
            }
        }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                //以下六个为得到脚模板中的控件,并创建变量
                Label CurrentPage = e.Item.FindControl("LabelNowPage") as Label;
                Label PageCount = e.Item.FindControl("LabelCount") as Label;
                LinkButton FirstPage = e.Item.FindControl("LinkButtonFirstPage") as LinkButton;
                LinkButton PrePage = e.Item.FindControl("LinkButtonPrePage") as LinkButton;
                LinkButton NextPage = e.Item.FindControl("LinkButtonNextPage") as LinkButton;
                LinkButton LastPage = e.Item.FindControl("LinkButtonLastPage") as LinkButton;
                CurrentPage.Text = (ps.CurrentPageIndex + 1).ToString();//绑定显示当前页
                PageCount.Text = ps.PageCount.ToString();//绑定显示总页数
                if (ps.IsFirstPage)//如果是第一页,首页和上一页不能用
                {
                    FirstPage.Enabled = false;
                    PrePage.Enabled = false;
                }
                if (ps.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
                {
                    NextPage.Enabled = false;
                    LastPage.Enabled = false;
                }

                DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList;
                ddl.Items.Clear();
                for (int i = 1; i <= ps.PageCount; i++)
                {
                    ddl.Items.Add(i.ToString());
                }
            }
        }

        protected void ButtonSelect_Click(object sender, EventArgs e)
        {
            string wheresql;
            if (DropDownListFoodType.SelectedValue == "全部")
            {
                wheresql = "where FoodPrice Between " + TextBoxLowPrice.Text + " And " + TextBoxHighPrice.Text;
            }
            else
            {


                wheresql = "where FoodTypeID =" + DropDownListFoodType.SelectedValue + " and FoodPrice Between " + TextBoxLowPrice.Text + " And " + TextBoxHighPrice.Text;

            }

            SqlDataSource1.SelectCommand = "SELECT  [FoodID],[FoodTypeID],[FoodName],[FoodPrice] ,[FoodIntroduction] ,[FoodCount] ,[FoodImageURL]  FROM [Food] " + wheresql + "order by  [FoodPrice] desc";

           //我原来的思路是通过SqlDataSourceS实现功能,这里还有个Datalist.Bind();
            ViewState["SelectCommand"] = SqlDataSource1.SelectCommand;
        }

    }


}


请问大神们,有没有不用sqlDataSource,实现查询的方法·····
说的比较乱,见谅

  • 写回答

1条回答 默认 最新

  • devmiao 2015-12-12 12:28
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?