在asp.net中的Repeater控件下的Gridview

时间:2012-10-29 17:14:05

标签: asp.net gridview repeater

我有一个包含网格的转发器控件,基于数据库的值,例如我在转发器控件中有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")
    {

    }
}

任何人都可以告诉我如何从点击的网格向上/向下按钮获得。

1 个答案:

答案 0 :(得分: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属性。您可以使用指示转发器内特定gridview的值来设置它。

以下是检查值的方法:

    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";
}
相关问题