0

首先我的html

<asp:GridView ID="GridViewCustomers" runat="server" AutoGenerateColumns="False" 
            AllowPaging="True" datakeynames="Reservation_ID,Excursion_ID"
            onpageindexchanging="GridViewCustomers_PageIndexChanging"
            onrowcommand="GridViewCustomers_SelectedIndexChanged" 
             BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" >

            <AlternatingRowStyle BackColor="#F7F7F7" />

            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="Reservation_ID" HeaderText="РЕЗ. №" />
                                <asp:BoundField DataField="Name"  HeaderText="Име на Екскурзия" />
                                <asp:BoundField DataField="Reservation_TotalAmount"  HeaderText="Сума по резервацията" />

                <asp:BoundField DataField="Excursion_ID" Visible="false" />

                <asp:BoundField DataField="ReservationDate" HeaderText="Дата на заминаване" />

               <asp:TemplateField HeaderText="Имена на Туристи">
            <ItemTemplate>
                <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("TouristName") %>'>
                    <ItemTemplate>
                        <%# (Container.ItemIndex+1)+"."+ Container.DataItem  %><br />
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
             </asp:TemplateField>

            </Columns>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <SortedAscendingCellStyle BackColor="#F4F4FD" />
            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
            <SortedDescendingCellStyle BackColor="#D8D8F0" />
            <SortedDescendingHeaderStyle BackColor="#3E3277" />
        </asp:GridView>

因此,在代码中,您将看到单击选择按钮我得到值 od datakeyna 并将用户重定向到另一个页面。问题是当我单击gridview 的第2 页时,我被重定向到另一页,就好像我选择了一行一样。

    protected void Page_Init(object sender, EventArgs e)
    {
        this.lstUserReservations.AddRange(lstUserReservations);
        this.lstUserReservations.AddRange(lstUserReservations);
        this.lstUserReservations.AddRange(lstUserReservations);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            User_Reservation trr = new User_Reservation();
            lstUserReservations = new List<User_Reservation>();
            lstUserReservations = LoadReservationsForUser();
            GridViewCustomers.DataSource = lstUserReservations;
            GridViewCustomers.DataBind();
        }

    }

    protected void GridViewCustomers_SelectedIndexChanged(object sender,  GridViewCommandEventArgs e)

    {  int indexer = Int32.Parse(e.CommandArgument.ToString()); 
                   Session["еxcursion_id"] = this.GridViewCustomers.DataKeys[indexer].Values["Excursion_ID"].ToString();
        Response.Redirect("ReservationInfo.aspx");

    }
    protected void GridViewCustomers_PageIndexChanging(object sender,
       System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridViewCustomers.PageIndex = e.NewPageIndex;
         this.GridViewCustomers.DataBind();
    }

}

你看我有

4

1 回答 1

0

您必须注意一个小代码。如果要更改分页,请检查是否单击了分页,如果有则返回,否则选择该行。此外,当您更改分页时,该RowCommand事件首先和该PageIndexChanging事件之后起作用。

protected void GridViewCustomers_SelectedIndexChanged(对象发送者,GridViewCommandEventArgs e)

{  
    if(e.CommandName == "Page")
        return;
    int indexer = Int32.Parse(e.CommandArgument.ToString()); 
    Session["еxcursion_id"] = this.GridViewCustomers.DataKeys[indexer].Values["Excursion_ID"].ToString();
    Response.Redirect("ReservationInfo.aspx");

}

让我知道这是否对您有用。

于 2013-10-15T09:45:32.940 回答