【问题标题】:How to get GridView RowIndex when checkbox is clicked单击复选框时如何获取GridView RowIndex
【发布时间】:2015-06-30 15:24:02
【问题描述】:

我有一个简单的网格视图。每行都有一些 boundFields 和一个复选框。该复选框具有 AutoPostBack=True 并使用 OnCheckedChanged 事件触发方法 (chkPlanned_OnCheckedChanged)。

     <asp:GridView ID="gvAudits" runat="server" EnablePersistedSelection="true"
            AllowSorting="True" CssClass="DDGridView"
            RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6" AutoGenerateColumns="False" DataKeyNames="AuditId" OnSorting="GridView1_Sorting" OnRowDataBound="gvAudits_RowDataBound" OnRowCommand="gvAudits_RowCommand"
            OnDataBound="OnDataBound">

            <Columns>



                <asp:BoundField DataField="AuditDate" SortExpression="AuditDate" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Audit Date" ItemStyle-Width="50px" />

                <asp:TemplateField HeaderText="Division Owner" SortExpression="DivisionOwner" ItemStyle-Width="140px">
                    <ItemTemplate>
                        <div style="width: 140px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
                            <%# Eval("DivisionOwner") %>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Main Auditee" SortExpression="MainAuditee" ItemStyle-Width="140px">
                    <ItemTemplate>
                        <div style="width: 140px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
                            <%# Eval("MainAuditee") %>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="TotalFindings" SortExpression="TotalFindings" HtmlEncode="False" HeaderText="Total <br />Findings " ItemStyle-Width="25px">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>

                <asp:BoundField DataField="NoMajorFindings" SortExpression="NoMajorFindings" HtmlEncode="False" HeaderText="Major <br />Findings " ItemStyle-Width="25px">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>




                <asp:TemplateField HeaderText="Potential" SortExpression="Planned">
                    <ItemTemplate>
                        <asp:CheckBox runat="server" ID="chkPlanned" Checked='<%# Convert.ToBoolean(Eval("Planned"))%>' AutoPostBack="True"   OnCheckedChanged="chkPlanned_OnCheckedChanged" />
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                </asp:TemplateField>


                <asp:BoundField DataField="AuditId" HeaderText="AuditId" SortExpression="AuditId"
                    ReadOnly="True" Visible="False">
                    <HeaderStyle Width="50px" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        &nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this item?");' CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'><i class="fa fa-remove"></i></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>


            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
        </asp:GridView>

后面的 C# 代码中的这个方法然后使用以下逻辑获取 rowIndex:

protected void chkPlanned_OnCheckedChanged(object sender, EventArgs e)
    {
  CheckBox chk = (CheckBox) sender;
  GridViewRow gr = (GridViewRow) chk.Parent.Parent;
  var dataKey = gvAudits.DataKeys[gr.RowIndex];
}

一切都很完美,然后我需要添加一个分组的列标题,我以编程方式执行如下操作:

    protected void OnDataBound(object sender, EventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        TableHeaderCell cell = new TableHeaderCell();
        cell.Text = "";
        cell.ColumnSpan = 5;
        row.Controls.Add(cell);

        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "Finding Status";
        row.Controls.Add(cell);

        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "Finding Type";
        row.Controls.Add(cell);


        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "";
        row.Controls.Add(cell);

        row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
        gvAudits.HeaderRow.Parent.Controls.AddAt(0, row);
    }

问题是,现在当我在添加分组标题后单击复选框时,我无法再从代码隐藏中获取 RowIndex。 gr.RowIndex 总是返回 0。有什么想法吗?

也许有一种方法可以将分组添加到 ASPX 中的 gridview 标头,而不是在后面的代码中,这样就可以解决问题?

【问题讨论】:

  • 检查我提供的答案..
  • 始终使用chk.NamingContainer 而不是chk.Parent.Parent
  • @DeadlyDee 得到了解决方案..

标签: c# asp.net


【解决方案1】:

使用下面的代码,希望它能解决你的问题

public void Chacked_Changed(object sender, EventArgs e)
{
GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
string id = grd1.DataKeys[Row.RowIndex].Value.ToString();
string cellvalue = Row.Cells[1].Text;
}

或使用

 CheckBox chk = (CheckBox)sender; 
 GridViewRow gvr = (GridViewRow)chk.NamingContainer;
 string id = GridView1.Rows[gvr.RowIndex].Value.ToString();
 string cellvalue = GridView1.Rows[gvr.RowIndex].Cells[1].Text;

【讨论】:

  • 您好,这个解决方案不起作用,它仍然给我一个 RowIndex = 0。
  • @DeadlyDee 是的,如果您选择第一行,那么 rowindex 将为 0...如果您只想要行索引,那么您将轻松通过这一行 -> int selRowIndex = ((GridViewRow)(( (CheckBox)sender).Parent.Parent)).RowIndex;
  • 不-我也试过了,但它不起作用。如果我删除分组标题一切正常。但我需要
  • @DeadlyDee 我已经检查过了,我可以在你的 rowbound 中在线看到 -> gvAudits.HeaderRow.Parent.Controls.AddAt(0, row); -- 为什么你在每一行上绑定标题控件?我认为这条线会导致问题
  • 您好,标题只绑定一次,如图所示,分组标题插入到主标题上方。据我所知,实现是正确的,我看不出有什么问题吗?感谢您的检查:)
猜你喜欢
  • 2014-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 2013-03-26
  • 2019-04-16
相关资源
最近更新 更多