首先我的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();
}
}
你看我有
您需要处理一些轻微的代码。如果要更改分页,请检查是否已单击分页并返回(如果有)或者选择该行。此外,当您更改分页时,RowCommand
事件首先在PageIndexChanging
事件之后。
protected void GridViewCustomers_SelectedIndexChanged(object sender,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");
}
如果这对您有用,请告诉我。