如何在gridview中使用分页获取隐藏值

时间:2015-10-13 14:45:12

标签: c# asp.net gridview get pagination

当在第一页中时,可以获得隐藏值。所以,没有问题但是在页面索引改变之后,用户无法获得hiddenfieldvalue。 ıt在尝试获取隐藏值时显示有关“System.ArgumentOutOfRangeException”的错误

(string sValue = ((HiddenField)GridView1.Rows[rowIndex].FindControl("HiddenField1")).Value;

asp方面:

        <Columns>
           <asp:BoundField DataField="sayfa_adeti" HeaderText="SAYFA ADETİ"  ItemStyle-HorizontalAlign="Center" ItemStyle-Width="60px"></asp:BoundField>
              </asp:BoundField>

   <asp:HiddenField ID="HiddenField1" runat="server"Value='<%#Eval("ID")%>'</asp:HiddenField>   

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

C#侧

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //int rowIndex = Convert.ToInt32(e.CommandArgument);
    int rowIndex = Convert.ToInt32(e.CommandArgument.ToString().Trim());


    if (e.CommandName == "Goruntule")
    {
        string sValue = ((HiddenField)GridView1.Rows[rowIndex].FindControl("HiddenField1")).Value;
        GridViewRow gvRow = GridView1.Rows[rowIndex];

        Session["SELECTED_FOTOKOPI_TALEP_ID"] = sValue;
        Response.Redirect("~/fotokopiTalebimiGor.aspx");
    }

}

2 个答案:

答案 0 :(得分:1)

您可以尝试使用CommandSource。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    var control = e.CommandSource as Control;
    var gridViewRow = control.NamingContainer as GridViewRow;
    int rowIndex = gridViewRow.RowIndex;

    ....
}

答案 1 :(得分:1)

尝试使用:

int rowIndex = Convert.ToInt32(e.CommandArgument) % GridView1.PageSize;