我正在使用中继器控制...

<asp:Repeater ID="rp" runat="server" OnItemDataBound="rp_ItemDataBound">
        <HeaderTemplate>
            <div>
                ABSTRACT OF MEASUREMENTS FOR IPC NO. 2</div>
            <div class="accord" >
        </HeaderTemplate>
        <ItemTemplate>
            <h3 id="acrdHeader" runat="server">
                <asp:Label ID="lbl" runat="server" Text='<%# Eval("HEADER")%>'></asp:Label>
            </h3>
            <div id="acrdDetail" runat="server">
                <p>
                    <asp:GridView OnRowDataBound="RowDataBound" OnSelectedIndexChanged="SelectedIndexChanged" ID="grd"  runat="server" >
                        <Columns>
                            <asp:BoundField DataField="BOQ_ITEM_NO" HeaderText="Item No." />
                            <asp:BoundField DataField="BOQ_ITEM_DESC" HeaderText="Particulars" />
                        </Columns>
                        <EmptyDataTemplate>
                            <span class="badge">
                                No Data Available
                            </span>
                        </EmptyDataTemplate>
                    </asp:GridView>
                </p>
            </div>
        </ItemTemplate>
        <FooterTemplate>
            </div>
        </FooterTemplate>
    </asp:Repeater>


然后尝试执行此RowDataBound但无法访问我的gridview名称grd

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grd, "Select$" + e.Row.RowIndex);
        e.Row.Attributes["style"] = "cursor:pointer";
    }
}

最佳答案

试试看:

GridView grd = (GridView)sender;


代码:

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Add this line and try
    GridView grd = (GridView)sender;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(grd, "Select$" + e.Row.RowIndex);
        e.Row.Attributes["style"] = "cursor:pointer";
    }
}

09-06 05:01