2012-10-29 79 views
0

我有一个中继器控件,它包含基于数据库中的值的网格,例如我在中继器控件内部有2个网格,现在这两个网格都包含一个有向上和向下按钮的列,现在当用户点击任何网格中的按钮时,如何检查从哪个网格调用按钮。下面 是我的代码我在哪里灌上RepeaterItemDataBound Event在asp.net中的Repeater控件下的Gridview

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> 

这是我的GridView的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") 
    { 

    } 
} 

谁能告诉我怎样才能从已经点击的网格上/下键搞定。

回答

0

您可以使用命令参数传递所需的值。 这里是类似的方式使用的ImageButton的样本:在CommandArgument property.You

   <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() %>' /> 

采取通知可以用值,表示内部转发特定的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"; 
} 
+0

感谢@Gregor Primar为您提供的宝贵回复,请您告诉我如何通过repea内部的网格名称在设计中,我将单个网格放置在中继器中,如何在命令参数中命名它。 – Abbas

+0

请看我编辑的答案。快乐的编码! –

+0

实际上它应该是GridView.ClientID,因为ID将总是返回Grid,按照我上面的示例...无论如何非常感谢您的帮助 – Abbas