gridview中的分页获取所有页面的相同数据

时间:2010-10-18 09:56:12

标签: c# asp.net

我有gridview根据两个事件从数据库中获取数据。第一个来自方法,第二个选择ddl.it工作得很好,我做了分页,当我浏览分页页面时我必须点击两次点击页面数量转到这个页面,所有页面都获得了相同的数据页面。所以任何人帮助我。

<aspx>

<div class="div_open_CS m10 ad_mun_font">
                                <table width="560" border="0" align="center" cellpadding="2" cellspacing="0">
                                    <tr>
                                        <td class="pag_bor">
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                    <td class="Pag_toparow">
                                                        &nbsp;
                                                    </td>
                                                    <td class="Pag_hedfont">
                                                        Dealers list
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="pag_bor">
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                    <td class="ad_mun_font_h" width="100">
                                                        Select City
                                                    </td>
                                                    <td>
                                                        <asp:SqlDataSource ID="SDSGetDealerArea" runat="server" ConnectionString="<%$ ConnectionStrings:ElarabyGroup %>"
                                                            ProviderName="System.Data.SqlClient" SelectCommand="SELECT [AREA_ID], [AREA_ENG] FROM [DealerArea]">
                                                        </asp:SqlDataSource>
                                                        <%--  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                                                <ContentTemplate>--%>
                                                        <asp:DropDownList CssClass="ad_mun_font_h" ID="DDlCity" runat="server" AutoPostBack="True"
                                                            DataSourceID="SDSGetDealerArea" DataTextField="AREA_ENG" DataValueField="AREA_ID"
                                                            OnDataBound="DDlCity_DataBound" OnSelectedIndexChanged="DDlCity_SelectedIndexChanged">
                                                        </asp:DropDownList>
                                                        <%-- </ContentTemplate>
                                                                <Triggers>
                                                                    <asp:AsyncPostBackTrigger ControlID="DDlCity" EventName="SelectedIndexChanged" />
                                                                </Triggers>
                                                            </asp:UpdatePanel>--%>
                                                    </td>
                                                    <td>
                                                        <asp:Label ID="LblResult" runat="server" Text="Result" Font-Bold="True" ForeColor="#006699"
                                                            Visible="false"></asp:Label>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td valign="top" class="tm10 rm10">
                                            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                                                AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None"
                                                CellPadding="3" DataKeyNames="DEALER_ID,LOC_ID,CAT_ID,AREA_ID" CellSpacing="3"
                                                GridLines="Horizontal" Width="510px" BorderWidth="1px" OnPageIndexChanging="GridView1_PageIndexChanging">
                                                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                                                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                                                <Columns>
                                                    <asp:TemplateField HeaderText="Name" SortExpression="NAME_ENG">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("NAME_ENG") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NAME_ENG") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Address" SortExpression="ADR_ENG">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ADR_ENG") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ADR_ENG") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Telephone" SortExpression="TEL_ENG">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("TEL_ENG") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("TEL_ENG") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Mobile" SortExpression="TEL_ARA">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("TEL_ARA") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("TEL_ARA") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Location" SortExpression="LOC_ENG">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("LOC_ENG") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("LOC_ENG") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Category" SortExpression="CAT_NAME">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label6" runat="server" Text='<%# Bind("CAT_NAME") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("CAT_NAME") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Area" SortExpression="AREA_ENG">
                                                        <ItemTemplate>
                                                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("AREA_ENG") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("AREA_ENG") %>'></asp:TextBox>
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                                                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                                                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                                                <AlternatingRowStyle BackColor="#DCDCDC" />
                                            </asp:GridView>
                                            <asp:Panel ID="pnlModal" runat="server" Style="display: none" CssClass="modalPopup">
                                                Our Valued Customer You Are Not Registered Are You Want To Register?
                                                <br />
                                                <br />
                                                <br />
                                                <br />
                                                <table id="LoginTL" runat="server" width="300" border="0" align="center" cellpadding="0"
                                                    cellspacing="2" visible="true" style="border-color: #666666">
                                                    <tr>
                                                        <td width="80px" class="bod_d_reg_txt_p lm7">
                                                            Username :
                                                        </td>
                                                        <td>
                                                            <label>
                                                                <asp:TextBox ID="TxtUser" runat="server" Width="125px"></asp:TextBox>
                                                            </label>
                                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                                                ControlToValidate="TxtUser" Display="Dynamic" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                                                        </td>
                                                        <td>
                                                            <table width="110" border="0" cellspacing="0" cellpadding="0">
                                                                <tr>
                                                                    <td>
                                                                        <label>
                                                                            <asp:CheckBox ID="CBRemeber" runat="server" class="tx_s" OnCheckedChanged="CBRemeber_CheckedChanged" />
                                                                        </label>
                                                                    </td>
                                                                    <td class="bod_d_reg_txt_w">
                                                                        Remember me
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td width="80" class="bod_d_reg_txt_p lm7">
                                                            Password :
                                                        </td>
                                                        <td>
                                                            <label>
                                                                <asp:TextBox ID="TXTPassword" runat="server" TextMode="Password" Width="125px"></asp:TextBox>
                                                            </label>
                                                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                                                                ControlToValidate="TXTPassword" Display="Dynamic" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                                                        </td>
                                                        <td>
                                                            <asp:ImageButton ID="IBLogin" runat="server" ImageUrl="images/home-d_login.jpg" OnClick="IBLogin_Click" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:Label ID="Label8" runat="server" CssClass="ad_mun_font" Text="result" Visible="false"></asp:Label>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <a href="Default.aspx" id="btnClose" runat="server">Close</a>
                                                        </td>
                                                        <td>
                                                        </td>
                                                        <td>
                                                            <a href="Registeration.aspx" id="Register" runat="server">Register</a>
                                                        </td>
                                                    </tr>
                                                </table>
                                                <br />
                                            </asp:Panel>
                                            <cc1:ModalPopupExtender TargetControlID="btnYes" ID="pnlModal_ModalPopupExtender"
                                                runat="server" Enabled="True" BackgroundCssClass="modalBackground" PopupControlID="pnlModal"
                                                CancelControlID="btnYes" DropShadow="true">
                                            </cc1:ModalPopupExtender>
                                            <asp:HiddenField ID="btnYes" runat="server" />
                                        </td>
                                    </tr>
                                </table>
                            </div>

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {


           BindGridFunction();
        }


    }


    private void BindGridFunction()
    {

        DataTable DT = new DataTable();
        using (SqlConnection con = Connection.GetConnection())
        {
            SqlCommand Com = new SqlCommand();
            if (DDlCity.SelectedIndex < 0)
            {
                Com = new SqlCommand("GetDealers", con);
                Com.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DA = new SqlDataAdapter(Com);
                DA.Fill(DT);
                GridView1.DataSource = DT;
                GridView1.DataBind();
            }
            else
            {
                Com = new SqlCommand("GetDealersByArea", con);
                Com.CommandType = CommandType.StoredProcedure;
                Com.Parameters.Add(Parameter.NewNVarChar("@AREA_ENG", DDlCity.SelectedItem.Text));
                SqlDataAdapter DA = new SqlDataAdapter(Com);
                DA.Fill(DT);
                GridView1.DataSource = DT;
                GridView1.DataBind();
            }



        }
    }



    protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridFunction();


    }


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

1 个答案:

答案 0 :(得分:1)

GridView1.PageIndex = e.NewPageIndex;致电GridView1.DataBind(); 之后尝试重新绑定。

<强> - 编辑 -

DataSet,然后是DataBind(),尝试如下:

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