簡體   English   中英

從Repeater內的ItemCommand識別DataGrid

[英]Identify the DataGrid from ItemCommand inside Repeater

我在Repeater有一個DataGrid 在此DataGrid我有一個Button “ btnTest”。 如何從DataGridItemCommand識別哪個(中繼器的索引) DataGrid

ASPX:

<asp:Repeater ID="ProdList" OnItemDataBound="ProdList_ItemDataBound" OnItemCommand="ProdList_ItemCommand" runat="server">
    <ItemTemplate>
        <div style="padding: 5px;">
            <asp:DataGrid ID="dtg" OnItemDataBound="dtg_ItemDataBound" OnItemCommand="dtg_ItemCommand" runat="server" AutoGenerateColumns="False" DataSource='<%# DataBinder.Eval(Container.DataItem, "ProductItems") %>'>
                <Columns>
                    <asp:BoundColumn HeaderText="ProdName" DataField="ProdName"></asp:BoundColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:Button ID="btnTest" runat="server" CommandName="Test" Text="Test" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
            <asp:TextBox ID="txtSearch" runat="server" style="width:250px;" CssClass="txtSearch" autocomplete="off"></asp:TextBox>
            <asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btnSearch" onclick="btnSearch_Click" />
            <asp:Button ID="btnCheckAvailability" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProdId") %>' CssClass="button" CommandName="CheckAvailability" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ProdId") %>' />
        </div>
    </ItemTemplate>
</asp:Repeater>

ASPX.CS:

protected void ProdList_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {

    }
}

protected void ProdList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{

}

protected void dtg_ItemDataBound(object sender, DataGridItemEventArgs e)
{

}

protected void dtg_ItemCommand(object source, DataGridCommandEventArgs e)
{
    // I want to identify DataGrid here
}

請幫我。

您能否在“ dtg_ItemCommand”中嘗試該未經測試的代碼。

protected void dtg_ItemCommand(object source, DataGridCommandEventArgs e)
{
    //Get the reference of the datagrid.
    DataGrid dg= (source as DataGrid);

    //Get the Repeater Item reference
    RepeaterItem item = dg.NamingContainer as RepeaterItem;

    //Get the repeater item index
    int index = item.ItemIndex;
}

您必須在dtg_ItemCommand導航NamingContainer樹。

//get the current datagrid item from the sender
DataGridItem gridIitem = (DataGridItem)(((Control)e.CommandSource).NamingContainer);

//the index of the griditem if needed
int gridItemIndex = gridIitem.ItemIndex;

//then the datagrid from the underlying datagriditem
DataGrid grid = (DataGrid)gridIitem.NamingContainer;

//then the repeateritem from the underlying datagrid
RepeaterItem repeaterItem = (RepeaterItem)grid.NamingContainer;

//now you can get the index of the repeateritem
int repeaterIndex = repeaterItem.ItemIndex;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM