请教gridview中使用ddl重新绑定数据的问题
我用gridview想实现以下功能。
默认显示的是所有的数据。。。
然后在页面有个dropdownlist(不在gridview中),当我选择后 显示该大类的所有数据。
那么我在dropdownlist的事件中重新为ID为database的DataSource 指定了selectcommand语句,然后为gridview重新绑定了database..
现在的问题是。。。
点击DDL的大类后显示第一页正常。。点击下一页就错。。比如一共两页。。第一页正常。。第二页不但没有数据。分页显示的也错了 成了 2/1(应该是2/2)
代码如下:
<asp:GridView ID= "gridview_article " runat= "server " AutoGenerateColumns= "False " DataSourceID= "database "
EmptyDataText= "无记录 " GridLines= "None " CssClass= "cTable table tCenter " AllowPaging= "true " PageSize= "16 " OnDataBound= "gridview_article_databound ">
</gridview>
<asp:SqlDataSource ID= "database " runat= "server " ConnectionString= " <%$ ConnectionStrings:databaseConnectionString %> "
ProviderName= " <%$ ConnectionStrings:databaseConnectionString.ProviderName %> ">
<SelectParameters>
<asp:ControlParameter ControlID= "Big_Column " PropertyName= "SelectedItem.Value " Name= "FirstColumnNum " />
<asp:ControlParameter ControlID= "Son_Column " PropertyName= "SelectedItem.Value " Name= "SecondColumnNum " />
</SelectParameters>
</asp:SqlDataSource>
cs页面
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//指定database默认的selectcommand语句
database.SelectCommand = "SELECT [num],LEFT(Title,15) as Title, [FirstColumnNum],[FirstColumnName],[Times], [IssueDate] FROM [Article] ";
}
Button_First.CommandName= "1 ";
Button_Next.CommandName=(gridview_article.PageCount==1 ? gridview_article.PageCount.ToString() : Convert.ToString((gridview_article.PageIndex+2)));
Button_Pre.CommandName=(gridview_article.PageIndex==0 ? "1 " : gridview_article.PageIndex.ToString());
Button_Last.CommandName=gridview_article.PageCount.ToString();
}
//分页LinkButton
protected void PagerButtonClick(object sender,EventArgs e)
{
gridview_article.PageIndex=Convert.ToInt32(((LinkButton)sender).CommandName)-1;
}
//databound事件
protected void gridview_article_databound(object sender, EventArgs e)
{
Label1.Text = Convert.ToString((gridview_article.PageIndex + 1));
Label2.Text = gridview_article.PageCount.ToString();
if (Label2.Text == "0 ")
{
Label2.Text = "1 ";
}
}
//顶部大类文章下拉框的数据绑定
protected void Big_Column_select(object sender, EventArgs e)
{
database.SelectCommand = "SELECT [num],[Title], [FirstColumnNum],[FirstColumnName], [Times], [IssueDate] FROM [Article] where FirstColumnNum=@FirstColumnNum ";
gridview_article.DataSourceID = "database ";
}
[解决办法]
大多数都是Page_Load里面的IsPostBack问题,你调试一下程序,看执行到哪里导致这样的错误嘛~~~
[解决办法]
http://blog.csdn.net/ranzige/ 看看有没有你想要的。
[解决办法]
是因为你翻页的时候,dropdownlist的selectvalue没有保持.
我一般的做法是:
把dropdownlist的selectvalue放在一个隐藏的控件里,在数据绑定和翻页的时候,根据这个隐藏的控件的text值来重新绑定gridview
[解决办法]
幫,頂