2012-10-29 74 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