GridView 绑数据,重新查询后不能回到第1页
前端代码:
- HTML code
<div class="searchlayer" id="searchdiv" runat="server"> 链接文本:<asp:TextBox ID="txtbtitle" runat="server"></asp:TextBox> 系统类型:<asp:DropDownList ID="dropType" runat="server" AutoPostBack="false"> </asp:DropDownList> IP段/分局:<asp:DropDownList ID="dropIP" runat="server" AutoPostBack="false"> </asp:DropDownList> <br /> <asp:ImageButton ID="ibtnAdd" ImageUrl="~/App_Themes/Default/Images/btn_search.gif" ImageAlign="AbsMiddle" runat="server" OnClick="btnsearch_Click" /> <asp:ImageButton ID="ibtnReset" ImageUrl="~/App_Themes/Default/Images/btn_reset.gif" ImageAlign="AbsMiddle" runat="server" OnClick="btnReset_Click" /> <span style="margin: 5px;"> <img src="App_Themes/Default/Images/icon_08.gif" alt="添加新的链接" /> <a style="color: #000;" href="addLink.aspx">添加链接</a></span> </div> </center> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" OnPageIndexChanged="GridView1_PageIndexChanged" AllowPaging="true" PageSize="14" Width="850"> <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" /> <Columns> <asp:TemplateField HeaderText="ID" ItemStyle-Width="50px"> <ItemTemplate> <asp:Label ID="Labelid" runat="server" Text='<%# Bind("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="系统名称" ItemStyle-Width="300px"> <ItemTemplate> <asp:Label ID="Labeltile" runat="server" Text='<%# Bind("title") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="链接地址" ItemStyle-Width="400px"> <ItemTemplate> <a style="color: #000;" href="<%# Eval("url") %>" target="_blank"> <asp:Label ID="Labelurl" runat="server" Text='<%# myleft(Eval("url").ToString(),40) %>' ToolTip='<%# Eval("url") %>'></asp:Label></a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="IP段/分局" ItemStyle-Width="250px"> <ItemTemplate> <asp:Label ID="Labelip" runat="server" Text='<%# Eval("ip")+" / "+IPtoText(Eval("ip").ToString()) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="类型" ItemStyle-Width="220px"> <ItemTemplate> <asp:Label ID="Labeltypeid" runat="server" Text='<%# typeidtotext(Eval("typeid").ToString()) %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="操作" ItemStyle-Width="130px"> <ItemTemplate> <!--<a style="color: #000;" href="javascript:Update(<%# Eval("id") %>)">修改</a>--> <a style="color: #000;" href="EditLink.aspx?id=(<%# Eval("id") %>)">修改</a> <span ><a href="dellink.aspx?id=<%# Eval("id") %>" style="color: Black;">删除</a></span> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:TemplateField> </Columns> <RowStyle BackColor="#eef2ff" ForeColor="#333333" Height="25" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <PagerStyle BackColor="#7D9AFF" ForeColor="#333333" HorizontalAlign="Center" /> <HeaderStyle BackColor="#7D9AFF" Font-Bold="True" ForeColor="White" Height="30" /> <AlternatingRowStyle BackColor="White" /> <PagerTemplate> 第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页 共/<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页 <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First"></asp:LinkButton> <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev"></asp:LinkButton> <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next"></asp:LinkButton> <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last"></asp:LinkButton> 跳到<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'/>页 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" OnClick="Turn_Click" /> <asp:RegularExpressionValidator ID="RegularExpressionValidatorSum" runat="server" ControlToValidate="txtNewPageIndex" Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[-]?(\d+\.?\d*|\.\d+)$">(必须为数字)</asp:RegularExpressionValidator> <!-- here set the CommandArgument of the Go Button to '-1' as the flag --> </PagerTemplate> </asp:GridView>
- C# code
private void BindData() { string mytitle = txtbtitle.Text.ToString(); string myip = dropIP.SelectedValue.ToString(); string mytype = dropType.SelectedValue.ToString(); string strsql = "select * from FlatInfo where 1=1 "; if (mytitle != "") { strsql += " and title like '%" + mytitle + "%'"; } if (myip != "") { strsql += " and ip='" + myip + "' "; } if (mytype != "") { strsql += " and typeid='" + mytype + "' "; } strsql += " order by ID desc"; DataSet ds = SqlHelper.ExecuteDataset(DAConfig.MainDBConnString, CommandType.Text, strsql); DataTable dt = ds.Tables[0]; GridView1.DataSource = dt; GridView1.DataBind(); //int newPageIndex = 0, txtNewPageIndex = 0; //GridView1.PageIndex = newPageIndex; }这是点击查询后的执行代码。现在的问题是如果在查询前被翻到第N页(N>1),则重新查询后出来结果后自动翻到第N页(如果结果>=N的话),而不是定位到第1页。
翻页的代码是:
- C# code
public int pagesize = 1; protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.EditIndex = -1; GridView theGrid = (GridView)sender; // refer to the GridView int newPageIndex = 0, txtNewPageIndex = 0; GridViewRow pagerRow = null; if (-2 == e.NewPageIndex) { pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate } if (null != pagerRow) { txtNewPageIndex = Int16.Parse(((TextBox)pagerRow.FindControl("txtNewPageIndex")).Text); // refer to the TextBox with the NewPageIndex value } if (0 != txtNewPageIndex) { newPageIndex = txtNewPageIndex - 1; // get the NewPageIndex } else { newPageIndex = e.NewPageIndex;// when click the first, last, previous and next Button } // check to prevent form the NewPageIndex out of the range newPageIndex = newPageIndex < 0 ? 0 : newPageIndex; newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex; // specify the NewPageIndex theGrid.PageIndex = newPageIndex; //bind(); }请问该怎么解决?非常感谢。
[解决办法]
theGrid.PageIndex = 1;
BindData();
等于1在重新绑定下