我用GridView加载数据库数据,页面分页,在Class1类里建立seclect方法,用ObjectDataResource引用这个方法。建立了一个下拉菜单,用like匹配选中的值,这个值我用的static值,而且检测赋值成功了,然后用button执行Class1类里的select()没反应,但点下一页时却实现了搜索,有哪位高手知道是怎么回事吗?要实现检索我该怎么弄呢?

解决方案 »

  1.   

       static会使方法变成全局的。或者是静态的。本人也不提倡用。而且,你能不能再把你想要的效果说清楚直观一点,这样不是很清楚。
      

  2.   

    我只定义了static的字段,原来没用static但因为怀疑是赋值有问题改用这个了
    页面代码
    <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="true"  CodeFile="datareader.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:Login ID="Login1" runat="server" onloggedin="Login1_LoggedIn">
            </asp:Login>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server" 
                onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem>全部数据</asp:ListItem>
                <asp:ListItem Selected="True">gaohuipu</asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            <br />
            <asp:LinkButton ID="LinkButton1" runat="server"  >LinkButton</asp:LinkButton>    </div>
        <asp:ObjectDataSource ID="movie1" runat="server" 
            SelectMethod="select" TypeName="diaoche" UpdateMethod="UpDate" 
            DeleteMethod="Delete" InsertMethod="Insert">
        </asp:ObjectDataSource>
        
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            DataSourceID="movie1">
        </asp:GridView>
        </form>
    </body>
    </html>
    页面脚本代码
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;//
    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.Web.Configuration;//public partial class _Default : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            diaoche.SerUser="gaohuipu";
            Label2.Text = User.Identity.Name;
           diaoche diao = new diaoche();
            string name = DropDownList1.SelectedValue; ;
            if (name == "全部数据")
                name = "";
            else name = DropDownList1.SelectedValue;
            diao.requset();
           
          Label1.Text = diaoche.SerUser;
          this.GetPostBackEventReference(GridView1);
          
        }    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            diaoche diao = new diaoche();
            string name = DropDownList1.SelectedValue; ;
            if (name == "全部数据")
                name = "";
            else name = DropDownList1.SelectedValue;
            diao.requset();
            Label1.Text = diaoche.SerUser;
            diao.select();
            
        }
        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            diaoche diao = new diaoche();
            diao.requset();
        }
    class1代码
    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;//
    using System.Web.Configuration;//
    using System.Collections.Generic;///// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class diaoche
    {
         public static string SerUser;
        string user1; //在select方法中赋值不行
        string name;
     
        public diaoche()
        {
            _conString = WebConfigurationManager.ConnectionStrings["diaoche"].ConnectionString;
        }
        private string _conString;   
           public void requset()
        {
       SerUser= HttpContext.Current.Request["DropDownList1"]  ;
        }   
        public DataSet select()
        {
            
            
            SqlConnection con = new SqlConnection(_conString);
       user1 = HttpContext.Current.User.Identity.Name;
               string commandText; 
          commandText = "SELECT Id,  UserName   ,xinghao,shengchanchangjia,dunwei ,zhubi ,fubi ,chaopei  ,chezhu ,hangye ,chezhuquyu ,areaprincipal ,xiansuozaisheng ,xiansuozaidi ,shiyongkongxian ,shiyongdanwei ,shiyongkongxiandidian ,shiyonggongcheng FROM diaoche WHERE UserName like '" + SerUser + "%'  ";     
                SqlDataAdapter dad = new SqlDataAdapter(commandText, con);
            DataSet set = new DataSet();
            using (dad)
            {
                dad.Fill(set);
            }
            return set;
        }
        
    }
      

  3.   

    我感觉可能是button点击后无法重新查询造成的,我在页面上看那个下一页有一个_dopostback,是不是和这个有关?
      

  4.   

     我用了GridView1.DataSource = set;
            GridView1.DataBind();
    提示不能用DatasourceId和Datasource
    怎么才能让他刷新数据呢?
      

  5.   

    protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {
    diaoche.SerUser="gaohuipu";
    Label2.Text = User.Identity.Name;
    diaoche diao = new diaoche();
    string name = DropDownList1.SelectedValue; ;
    if (name == "全部数据")
    name = "";
    else name = DropDownList1.SelectedValue;
    diao.requset();Label1.Text = diaoche.SerUser;
    this.GetPostBackEventReference(GridView1);
    }
    }
      

  6.   

    static会使方法变成全局的。或者是静态的。本人也不提倡用
    这样不是节约内存么?
      

  7.   

       你这里开始加载的时候是手动的绑定的数据源对吧。但是,当你单击按钮的时候,你所查询的结果会改变对吧。你就应该把原来绑定的数据源给移除掉。
    可以通过:this.GridView1.ObjectDataSourceId=null;
    再重新绑定一下,可以通过:this.GridView1.DataSource=diao.select();
                           this.GridView1.DataBind();
      

  8.   

      哦。不好意思,刚刚那个地方因为写得有点快,那个this.GridView1.DataSource=diao.select().table[0];这样才对。
      

  9.   

    楼主在Page_Lode事件里边没有经过判断,if(!IsPostBack){......}要判断是否回传,否则的话页面会重新加载,当然就没反应了!!!
      

  10.   

    我用了this.GridView1.DataSource=diao.select();但分页又不能用了
    提示:“GridView1”激发了未处理的事件“PageIndexChanging”。 
    这是怎么回事?
      

  11.   

    又加了一个
    GridView1_PageIndexChanging
    {
    this.GridView1.DataSourceID = "movie1";}
    可以翻页了