<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();
} 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; }
页面代码
<%@ 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;
}
}
GridView1.DataBind();
提示不能用DatasourceId和Datasource
怎么才能让他刷新数据呢?
{
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);
}
}
这样不是节约内存么?
可以通过:this.GridView1.ObjectDataSourceId=null;
再重新绑定一下,可以通过:this.GridView1.DataSource=diao.select();
this.GridView1.DataBind();
提示:“GridView1”激发了未处理的事件“PageIndexChanging”。
这是怎么回事?
GridView1_PageIndexChanging
{
this.GridView1.DataSourceID = "movie1";}
可以翻页了