<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.WebForm1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="2" BorderWidth="1"
BorderColor="Gainsboro" GridLines="Vertical" Width="90%" AllowPaging="True" PageSize="5" OnPageIndexChanged="OnNewPage"
AllowSorting="True" OnSortCommand="Onsort">
<Columns>
<asp:BoundColumn HeaderText="Item ID" DataField="title_id" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn HeaderText="Title" DataField="title" SortExpression="title Asc" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn HeaderText="Price" DataField="Price" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white" Font-Bold="True" />
<ItemStyle BackColor="white" ForeColor="blue" />
<AlternatingItemStyle BackColor="beige" ForeColor="blue" />
</asp:DataGrid>
</form>
</body>
</HTML> <script language="C#" runat="server">
void Page_Load (Object sender,EventArgs e)
{
if (!IsPostBack)
{ SqlDataAdapter ad = new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView (ds.Tables[0]);
view.Sort=e.SortExpression.ToString();
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
}
void OnNewPage(Object sender,DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
SqlDataAdpter ad=new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
void OnSort(Object sender,DataGridPageChangedEventArgs e)
{
SqlDataAdpter ad=new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView(ds.Tables[0]);
view.Sort=e.SortExpression.ToString();
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
</script>
以上代码复制到html代码区。运行时出现 “System.EventArgs”并不包含对“SortExpression”的定义。
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="2" BorderWidth="1"
BorderColor="Gainsboro" GridLines="Vertical" Width="90%" AllowPaging="True" PageSize="5" OnPageIndexChanged="OnNewPage"
AllowSorting="True" OnSortCommand="Onsort">
<Columns>
<asp:BoundColumn HeaderText="Item ID" DataField="title_id" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn HeaderText="Title" DataField="title" SortExpression="title Asc" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" />
<asp:BoundColumn HeaderText="Price" DataField="Price" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white" Font-Bold="True" />
<ItemStyle BackColor="white" ForeColor="blue" />
<AlternatingItemStyle BackColor="beige" ForeColor="blue" />
</asp:DataGrid>
</form>
</body>
</HTML> <script language="C#" runat="server">
void Page_Load (Object sender,EventArgs e)
{
if (!IsPostBack)
{ SqlDataAdapter ad = new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView (ds.Tables[0]);
view.Sort=e.SortExpression.ToString();
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
}
void OnNewPage(Object sender,DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
SqlDataAdpter ad=new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
void OnSort(Object sender,DataGridPageChangedEventArgs e)
{
SqlDataAdpter ad=new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView(ds.Tables[0]);
view.Sort=e.SortExpression.ToString();
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
</script>
以上代码复制到html代码区。运行时出现 “System.EventArgs”并不包含对“SortExpression”的定义。
解决方案 »
- winform中如何读取普通文件按exe执行呢
- lumisoft BeginConnect方法
- 为什么adsl重连换ip频率过快,ip有时候不会马上改变?
- 调用Dispose方法,主要是为了释放非托管资源,而内存并没有回收是把
- ADO能否大批量插入数据
- C#中能否实现这个需求??
- 今天整理了论坛很多关于dotnet(C#)的试题,大家共享一下。
- 如何修改webserver序列化的xml编码
- 求一个项目需求带数据库.
- C# 多线程中Invoke更新TextBox信息时不显示
- 编写第一个XNA游戏——摘自国内第一个中文版XNA官方帮助(由中国XNA开发网www.XnaDev.cn译)
- 微软XNA官方帮助中文版-由中国XNA开发网在国内最先隆重推出,欢迎下载学习。
void OnSort(Object sender,DataGridPageChangedEventArgs e)
更改成
void OnSort(Object sender,DataGridSortCommandEventArgs e)
也没有用。
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0117: “System.EventArgs”并不包含对“SortExpression”的定义源错误: 行 44: ad.Fill(ds);
行 45: DataView view=new DataView (ds.Tables[0]);
行 46: view.Sort=e.SortExpression.ToString();
行 47: DataGrid1.DataSource=view;
行 48: DataGrid1.DataBind();
源文件: http://localhost/WebApplication3/WebForm1.aspx 行: 46
还有这个错误。
{
if (!IsPostBack)
{ SqlDataAdapter ad = new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView (ds.Tables[0]);
view.Sort=e.SortExpression.ToString();//瀑布汗
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
}
{
if (!IsPostBack)
{
SqlDataAdapter ad = new SqlDataAdapter
("select * from titles where price !=0",
"server=localhost;database=pubs;uid=sa;pwd=sqlsa");
DataSet ds=new DataSet();
ad.Fill(ds);
DataView view=new DataView (ds.Tables[0]);
// “System.EventArgs”并不包含对“SortExpression”的定义
// view.Sort=e.SortExpression.ToString();
// 可以先默认指定一个
view.Sort="title Asc";
DataGrid1.DataSource=view;
DataGrid1.DataBind();
}
}
view.Sort=e.SortExpression.ToString();
SqlDataAdapter 不是 SqlDataAdpter
<asp:datagrid id="dgPagerState" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 64px"
runat="server" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="3" GridLines="Horizontal" PageSize="5" Height="20px" AutoGenerateColumns="False"
Width="376px" AllowSorting="True" AllowPaging="True">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="LastName" SortExpression="LastName" HeaderText="LastName"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" SortExpression="FirstName" HeaderText="FirstName"></asp:BoundColumn>
<asp:BoundColumn DataField="City" SortExpression="City" HeaderText="City"></asp:BoundColumn>
<asp:BoundColumn DataField="BirthDate" SortExpression="BirthDate" HeaderText="BirthDate" DataFormatString="{0:D}"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:datagrid> private void InitializeComponent()
{
this.dgPagerState.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPagerState_PageIndexChanged);
this.dgPagerState.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgPagerState_SortCommand);
this.Load += new System.EventHandler(this.Page_Load); }
private void dgPagerState_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//设置DataGrid的当前显示页的索引值为选择的页面索引值
dgPagerState.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
} private void dgPagerState_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
//取得排序表达式
string vortExp = e.SortExpression;
//如果是当前排序表达式
if(ViewState["SortOrder"].ToString() == vortExp)
{
//如果原来是降序则改为升序,反之亦然
if((string)ViewState["OrderDire"] == "Desc")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "Desc";
}
else
{
//重新设置排序关键字
ViewState["SortOrder"] = e.SortExpression;
}
DataGridDataBind();
}
但是我是cs代码和页面代码分开写的
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
在这里进行排序
建议你也分开写,这样比较清晰。
链接数据库放在webconfig里,把数据联接和逻辑处理给分开
//aspx file
<asp:DataGrid id="dtgTest" runat="server" Width="600px" Font-Size="10pt" AutoGenerateColumns="False"
AllowPaging="false" GridLines="Vertical" CellPadding="2" BorderWidth="1px" BorderColor="LightGray"
PageSize="20" AllowSorting="True" OnSortCommand="dtgTest_SortCommand">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#C0C0FF"></SelectedItemStyle>
<AlternatingItemStyle ForeColor="DarkBlue" BackColor="Beige"></AlternatingItemStyle>
<ItemStyle ForeColor="DarkBlue" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" Height="20px" ForeColor="White" BackColor="MediumBlue"></HeaderStyle>
<FooterStyle Height="20px" ForeColor="#333333" BackColor="Blue"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="UserName" HeaderText="UserName" SortExpression="UserName">
<ItemStyle Width="50%" VerticalAlign="Top"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Age" HeaderText="Age">
<ItemStyle Width="20%" VerticalAlign="Top"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Address" HeaderText="Address">
<ItemStyle Width="30%" VerticalAlign="Top"></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Height="20px" HorizontalAlign="Center" ForeColor="Blue" BackColor="#E0E0E0" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>//cs file
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if( !this.IsPostBack )
{
DataTable dt = new DataTable( "Test" );
dt.Columns.Add( "UserName", typeof( string ) );
dt.Columns.Add( "Age", typeof( int ) );
dt.Columns.Add( "Address", typeof( string ) );
for( int i = 0; i < 5; i++ )
dt.Rows.Add( new object[]{ i.ToString(), i, i.ToString() } );
dt.AcceptChanges();
ViewState["DataTable"] = dt; DataView dv = new DataView( dt, null, "UserName ASC",
DataViewRowState.CurrentRows );
ViewState["SortOrder"] = "ASC";
dtgTest.DataSource = dv;
dtgTest.DataBind();
}
}public void dtgTest_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataTable dt = ViewState["DataTable"] as DataTable;
if( dt != null )
{
if( ViewState["SortOrder"].ToString() == "ASC" )
ViewState["SortOrder"] = "DESC";
else
ViewState["SortOrder"] = "ASC"; Debug.WriteLine( ViewState["SortOrder"].ToString() );
DataView dv = new DataView( dt, null,
e.SortExpression + " " + ViewState["SortOrder"].ToString(),
DataViewRowState.CurrentRows ); dtgTest.DataSource = dv;
dtgTest.DataBind(); }
}
我的代码都在一楼啊。不过我的代码都在html代码区。你的好像从void page_load开始都在cs区。
现在就一个问题,它不显示我要的数据。运行后就一个空白的网页。和about_blank一样。
/WebApplication4”应用程序中的服务器错误。
--------------------------------------------------------------------------------类型“DataGridLinkButton”的控件“dtgTest__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 类型“DataGridLinkButton”的控件“dtgTest__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 堆栈跟踪:
[HttpException (0x80004005): 类型“DataGridLinkButton”的控件“dtgTest__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.DataGridLinkButton.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer)
System.Web.UI.WebControls.BaseDataList.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
System.Web.UI.Control.Render(HtmlTextWriter writer)
System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
System.Web.UI.Page.ProcessRequestMain()
我把你的程序测试了一下,发生以上的错误。麻烦了!
但是,看了你的代码,估计你都是copy的,没有仔细看你的代码,SqlDataAdapter 而不是SqlDataAdpter ,在OnSortCommand="Onsort"这个事件里,为 DataGrid 控件的 SortCommand 事件提供数据的是DataGridSortCommandEventArgs 类,并且无法继承此类。而你写成了DataGridPageChangedEventArgs ,这样的话怎么能出来数据?
你把这两个给改正过来,然后把Page_Load事件里的view.Sort=e.SortExpression.ToString();
这句话给去掉,你想要的结果就出来了。
感谢这么长时间的关注,真的很谢谢你。
你所说的我之前就已经改过来了。我用的是vs2003。
运行后还是没有结果。一片空白。