在asp.net Repeater控件下的GridView [英] Gridview under Repeater control in asp.net

查看:201
本文介绍了在asp.net Repeater控件下的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含网格的基础上,从数据库值Repeater控件,例如说我有中继器控件内部2格,现在无论是网格包含有上下按钮,现在当用户点击按钮列从任何网格,我怎么能检查从电网按钮被调用。
下面是我的code我在哪里灌上 RepeaterItemDataBound事件

i have a repeater control which contains grids, based on values from database, say for example i have 2 grids inside repeater control, now both the grids contains a column which have up and down buttons, now when user clicks on the button from any grids, how can i check from which grid the button is called. below is my code where i am filling the grids on RepeaterItemDataBound Event

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

这里部分名中包含的章节的名称,根据段的数量,我生成网格。
我的设计是这样的:

here section name contains the name of the sections, based on number of sections, i generate the grids. My Design looks like this:

<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>

这是我的GridView的row_command事件

this is my gridviews row_command event

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")
    {

    }
}

谁能告诉我怎么可以从电网上/下按钮被点击该得到的。

can anyone tell me how can i get from which grid up/down button has been clicked.

推荐答案

您可以用命令的参数来传递需要的值。
下面是同样的方式使用的ImageButton的示例:

You can use command argument to pass required value. Here is sample of using imagebutton in similar way:

                <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 property.You采取通知可以用值,表示内部转发特定的GridView的设置。

Take notice on CommandArgument property.You can set it with value that indicates specific gridview inside repeater.

和这里是如何来检查值:

And here is how to check the value:

    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:

You can also set CommandArgument using this approach:

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

那么你应该在页面一侧像这样声明函数:

Then you should declare function on page side something like this:

public string GetMySpecialValue() 
{
     return "some value";
}

这篇关于在asp.net Repeater控件下的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆