如何访问转发器内的gridview事件和gridview

时间:2014-06-23 04:17:47

标签: c# asp.net gridview

我正在使用转发器控制......

<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";
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

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";
    }
}

答案 1 :(得分:1)

找到网格控件的正确方法是在转发器中找到控件

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView grd = (GridView)sender;
    ...
    ... 

}