每当单击按钮时,Gridview.row.count 都会显示零



我有一个网格视图,每当用户点击网格视图的行时,记录将填充在文本框中,但我收到错误

索引超出范围。必须为非负数且小于 收藏

另外我检查网格视图.row.count显示零。请帮助下面是我的代码

<asp:GridView ID="GridView2"  runat="server" AutoGenerateColumns="False"  ShowHeader="false"  AllowPaging="false" AllowSorting="false" ToolTip="Click Here To Edit"
                          Style="table-layout: fixed;"  OnRowDataBound="GridView1_RowDataBound"
                        CssClass="mGrid"  PagerStyle-CssClass="pgr"  DataKeyNames="AcheadID"
                            AlternatingRowStyle-CssClass="alt" Width="100%" Height="100%"  >
    <AlternatingRowStyle CssClass="alt" />
           <Columns>
                <asp:TemplateField ItemStyle-Width="40px">
                  <ItemTemplate>
                     <%#Container.DataItemIndex+1 %>
                  </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Account Head ID" DataField="AcheadID" HeaderStyle-HorizontalAlign="Left" Visible="false" />
                <asp:TemplateField HeaderText="Account Head" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="400px">
                      <ItemTemplate>
                               <asp:Label ID="lblac_head" runat="server" Text='<%# Bind("ac_head") %>'></asp:Label>
                                    <asp:HiddenField ID="hndacheadID" runat="server" Value='<%# Bind("AcheadID") %>' />
                      </ItemTemplate>
                </asp:TemplateField>                       
           </Columns>
</asp:GridView>
<asp:Button ID="btnTest"  runat="server"  OnClick="GridView1_OnClick" style="display:none;"  />

我的代码隐藏为

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAccountHead();
        }
    }
 protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField hndac_headid = (HiddenField)e.Row.FindControl("hndac_headid");
            if (hndac_headid.Value != "0")
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d3d3d3'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                e.Row.Attributes.Add("style", "cursor:pointer;");
                //e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
                e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnTest, e.Row.RowIndex.ToString());
            }
        }
    }
    protected void GridView1_OnClick(object sender, EventArgs e)
    {
        Button buttonSender = sender as Button;
        int index;
        GridViewRow row = null;
        if (int.TryParse(Request.Params.Get("__EVENTARGUMENT"), out index))
        {
            row = GridView1.Rows[index];
            Label ac_head = (Label)GridView1.Rows[index].FindControl("lblac_head");

        }

索引超出范围问题发生Request.Params.Get("__EVENTARGUMENT")因为它不适用于Button(以及附加的 ImageButton)控件。

Button控件和ImageButton控件不调用__doPostBack函数。因此,_EVENTARGUMENT(和_EVENTTARGET)将始终为空。

但是,其他控件(如 CheckBoxesDropDownListsLinkButtons 使用 javascript 函数__doPostBack来触发回发。

尝试使用LinkButton

作为第二个检查(可以忽略),只需确保数据正确绑定到 GridView,例如检查IsPostback条件等......

检查您的 GridView,它有一个 Id GridView2,但您始终引用 GridView1。

最新更新