0

データベースからの値に基づいて、グリッドを含むリピーター コントロールがあります。たとえば、リピーター コントロール内に 2 つのグリッドがあり、両方のグリッドに上下のボタンを持つ列が含まれています。ユーザーが任意のグリッドからボタンをクリックすると、 、ボタンがどのグリッドから呼び出されているかを確認するにはどうすればよいですか。以下は、グリッドを埋めている私のコードですRepeaterItemDataBound Event

GridView gvw = e.Item.FindControl("grid") as GridView;
gvw.DataSource = info.GetStories(sectionNames[e.Item.ItemIndex].Trim());
gvw.DataBind();

ここでセクション名には、セクションの数に基づいてセクションの名前が含まれており、グリッドを生成します。私のデザインは次のようになります。

<asp:Repeater ID="rptGrids" runat="server" 
                        OnItemDataBound="rptGrids_ItemDataBound">
                        <ItemTemplate>
                            <asp:GridView ID="grid" runat="server" Width="100%" CellPadding="5" AllowPaging="true" ShowHeader="true" PageSize="10" AutoGenerateColumns="false" OnRowCommand="Stories_RowCommand">
                                <Columns>
                                    <asp:BoundField DataField="ArticleID" HeaderText="Article ID" ItemStyle-CssClass="center" />
                                    <asp:BoundField DataField="CategoryID" HeaderText="Category ID" ItemStyle-CssClass="center" />
                                    <asp:BoundField DataField="Title" HeaderText = "Article Title" />
                                    <asp:BoundField DataField="PublishDate" DataFormatString="{0:d}" HeaderText="Publish Date" ItemStyle-CssClass="center" />
                                    <asp:TemplateField HeaderText="Select Action" ItemStyle-CssClass="center">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="btnMoveUp" runat="server" ImageUrl="/images/up.gif" CommandArgument="Up" CommandName='<%# Container.DataItemIndex + "," + DataBinder.Eval(Container.DataItem, "StoryType") %>' />
                                            <asp:ImageButton ID="btnMoveDown" runat="server" ImageUrl="/images/dn.gif" CommandArgument="Down" CommandName='<%# Container.DataItemIndex + "," + DataBinder.Eval(Container.DataItem, "StoryType") %>' />
                                            <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/deny.gif" CommandArgument="Delete" OnClientClick="return confirm('Are you sure you want to delete this article?');" CommandName='<%# Container.DataItemIndex %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField Visible="false">
                                        <ItemTemplate>
                                            <asp:HiddenField ID="hdStoriesSortOrder" runat="server" Value='<%# Eval("SortOrder") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                            <div class="blank"></div>
                        </ItemTemplate>
                    </asp:Repeater>

これは私の gridviews row_command イベントです

protected void Stories_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandName.Split(',')[0]);
    string section = e.CommandName.Split(',')[1].Trim().ToString();
    string command = e.CommandArgument.ToString();
    if (command.ToLower() == "up")
    {
        GridView grd = rptGrids.Items[1].FindControl("grid") as GridView; // If i specify the index here, i gets proper grid, but how to recognize at runtime.
        Response.Write(grd.Rows.Count);
    }
    else if (command.ToLower() == "down")
    {

    }
}

グリッドの上下ボタンがクリックされた場所を取得する方法を教えてください。

4

1 に答える 1

0

コマンド引数を使用して、必要な値を渡すことができます。同様の方法で imagebutton を使用するサンプルを次に示します。

                <asp:ImageButton ID="btnView" runat="server" ToolTip="<% $resources:AppResource,Edit %>"
                    SkinID="EditPage" CommandName="myCommand" CommandArgument='<%# Eval("CustomerId") %>'
                    PostBackUrl='<%# "~/AdminPages/Customer.aspx?id=" + Eval("CustomerId").ToString() %>' />

CommandArgument プロパティに注意してください。リピーター内の特定のグリッドビューを示す値で設定できます。

そして、値を確認する方法は次のとおりです。

    protected void EntityGridViewContacts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //here you can check for command name...
        switch (e.CommandName)
        {
            case "myCommand":
                //here you access command argument...
                int customerId = Convert.ToInt32(e.CommandArgument.ToString());
                break;
        }

        //here is how you access source gridview...
        GridView gridView = (GridView)sender;
        string controlId = gridView.ID;
    }

このアプローチを使用して CommandArgument を設定することもできます。

CommandArgument='<%# GetMySpecialValue() %>'

次に、ページ側で関数を次のように宣言する必要があります。

public string GetMySpecialValue() 
{
     return "some value";
}
于 2012-10-29T17:24:47.303 に答える