我有一个gridview表格,设计的时候已经与SqlDataSource绑定好数据 ,并设定表格可以排序。运行时,表格按照要求填充了数据,但当点击排序时,表格没了,这是怎么回事?
代码如下:
在页面中:<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:jxpfConnectionString2 %>"
SelectCommand="">
</asp:SqlDataSource>
在代码中,
protected void Button1_Click(object sender, EventArgs e)
{
this.SqlDataSource1.SelectCommand = "sql语句";
this.GridView1.DataBind();
}
运行结果出来后,会出现一个表格,表格第一行是标题栏,可以单击排序,但就是单击后表格消失了,自然也看不到排序结果,求救。
代码如下:
在页面中:<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:jxpfConnectionString2 %>"
SelectCommand="">
</asp:SqlDataSource>
在代码中,
protected void Button1_Click(object sender, EventArgs e)
{
this.SqlDataSource1.SelectCommand = "sql语句";
this.GridView1.DataBind();
}
运行结果出来后,会出现一个表格,表格第一行是标题栏,可以单击排序,但就是单击后表格消失了,自然也看不到排序结果,求救。
GridView.DataBind();
this.GridView1.DataBind();少了设置数据源的句子 GridView1.DataSource = 数据源
SqlDataSource.SelectCommand是不会保存状态的吧,要在PageLoad里重新赋值,值可以保存在ViewState里
1.asp.net是无状态的,需要重新绑定。
2.你的排序方法又问题,导致排序后查询不到数据
this.SqlDataSource1.SelectCommand = "sql语句";
this.GridView1.DataSource = SqlDataSource1;
this.GridView1.DataBind();
结果运行出现错误:
“GridView1”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。
我这段代码是放在Button1_Click事件中完成的,还是需要在load中绑定吗?
去掉DataSourceID后能查询了,但排序无效,gridview里的allowsorting确实设置为true了哦