为Gridview进行分页并不正确

时间:2013-08-20 07:32:40

标签: asp.net gridview paging

我有一个搜索页面。在列表中显示搜索结果。

当我从页码中选择另一页时,搜索结果消失并显示列表,就像页面被刷新一样。我该怎么办?我尝试过OnSelectedIndexChanged和OnSelectedIndexChanging。并没有奏效。谢谢。

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" 
                    CellPadding="4" DataSourceID="ahsun" ForeColor="#333333" GridLines="None" Width="100%"
                    DataKeyNames="SICIL_NO" OnSelectedIndexChanged="LinkButton1_Click" CssClass="icerik" o >
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"  />
                    <Columns>
                        <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
                                    CssClass="hover" Text="Seç"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
<asp:TemplateField HeaderText="Adı" SortExpression="ADI">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton3" runat="server" Text='<%# Bind("ADI") %>' CausesValidation="False"
                                    CommandName="Select" CssClass="hover"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Soyadı" SortExpression="SOYADI">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton4" runat="server" Text='<%# Bind("SOYADI") %>' CausesValidation="False"
                                    CommandName="Select" CssClass="hover"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Birimi" SortExpression="BIRIMI">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton5" runat="server" Text='<%# Bind("BIRIMI") %>' CausesValidation="False"
                                    CommandName="Select" CssClass="hover"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Görevi" SortExpression="GOREVI">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton6" runat="server" Text='<%# Bind("GOREVI") %>' CausesValidation="False"
                                    CommandName="Select" CssClass="hover"></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>
                    <RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" />
                    <EditRowStyle BackColor="#2461BF" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
                <asp:SqlDataSource ID="ahsun" runat="server" ConnectionString="<%$ ConnectionStrings:oraLiveConnectionString %>"
                    ProviderName="<%$ ConnectionStrings:oraLiveConnectionString.ProviderName %>"
                    SelectCommand="select  SICIL_NO, ADI, SOYADI, BIRIMI ,GOREVI,PERSON_ID from xxah_hr_calisanlar WHERE SICIL_NO <> '01470' "></asp:SqlDataSource>
                <br />
                <asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4" DataKeyNames="SICIL_NO"
                    DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="50px"
                    Width="100%" CaptionAlign="Left" AutoGenerateRows="False" CssClass="icerik">
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
                    <EditRowStyle BackColor="#2461BF" />
                    <RowStyle BackColor="#EFF3FB" HorizontalAlign="Left" Width="75%" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" Width="25%" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left"
                        Width="25%" />
                    <AlternatingRowStyle BackColor="White" />
                    <Fields>
<asp:BoundField DataField="ADI" HeaderText="Adı" SortExpression="ADI" />
                        <asp:BoundField DataField="SOYADI" HeaderText="Soyadı" SortExpression="SOYADI" />
<asp:BoundField DataField="BIRIMI" HeaderText="Birimi" SortExpression="BIRIMI" />
                        <asp:BoundField DataField="GOREVI" HeaderText="G&#246;revi" SortExpression="GOREVI" />
</Fields>
                </asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:oraLiveConnectionString %>" EnableCaching="True"
                    ProviderName="<%$ ConnectionStrings:oraLiveConnectionString.ProviderName %>"
                    SelectCommand="select c.* from TABLE c where c.SICIL_NO=:sicil" >
                    <SelectParameters>
                        <asp:ControlParameter ControlID="GridView1" Name="sicil" PropertyName="SelectedValue" />
                    </SelectParameters>
                </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:1)

对于分页你需要处理PageIndexChanging.Nothing与OnSelectedIndexChanged。

例如:

方式

 protected void BindData()
        {
            string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";
            SqlConnection con = new SqlConnection(strConnection);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Userinfo", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
        }

<强> PageIndexChanging

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }

答案 1 :(得分:0)

尝试通过添加EnableViewState =“true”和ViewStateMode =“Enabled”来启用aspx页面中数据源的ViewState