2011-01-05 49 views
0

嗨我有一個網格控件,我在哪裏綁定的貼紙列表。在上面的網格中,這是外側網格我有兩個按鈕創建貼紙和虛空貼紙。 貼紙基本上有三個屬性Active,Void和Expired顯示爲列中的文本。每次只能添加一個貼紙。另外,如果有活動的貼紙,則用戶不能添加另一張貼紙,除非其過期或無效。基於網格行啓用禁用按鈕

所以我想要的是,只要我的網格被加載,如果有一個文本活動的列創建/添加貼紙將被禁用和無效將啓用。我正在使用以下代碼

/// <summary> 
/// Handles the RowDataBound event of the gvSticker control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param> 
/// <remarks></remarks> 
protected void gvSticker_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
{ 
    if (Session["FisherId"] != null) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      Label lblStatus = (Label)e.Row.FindControl("lblStickerStatus"); 

      if (lblStatus.Text.Contains("Active")) 
     { 
      btnAddSticker.Enabled = false; 
      btnVoidSticker.Enabled = true; 

       HyperLink hlStickerNum = (HyperLink)e.Row.FindControl("hlStickerNumber"); 
       hlStickerNum.Attributes.Add("style", 
         "cursor:hand;text-decoration:underline;font-weight:bold;"); 
      if (!string.IsNullOrEmpty(hlStickerNum.Text.Trim())) 
      { 
      string urlWithParameters = "Stickers.aspx?StickerId=" 
            + hlStickerNum.Text; 
        hlStickerNum.Attributes.Add("OnClick", "popWinNote('" + 
            urlWithParameters + "')"); 
       } 

      } 
      else 
      { 
       btnAddSticker.Enabled = true; 
       btnVoidSticker.Enabled = false; 
      } 

     } 
    } 
    else 
    { 
     btnAddSticker.Enabled = true; 
     btnVoidSticker.Enabled = false; 
    } 
} 

它在網格的第一次加載時工作良好。但每當我改變網格的頁面索引時都會失敗。

更新

這裏有約束力,pageindexchanging事件

/// <summary> 
    /// Handles the PageIndexChanging event of the gvSticker control. 
    /// </summary> 
    /// <param name="sender">The source of the event.</param> 
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param> 
    /// <remarks></remarks> 
    protected void gvSticker_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     gvSticker.PageIndex = e.NewPageIndex; 
     BindStickerGrid(); 
    } 



/// <summary> 
     /// Binds the sticker grid. 
     /// </summary> 
     /// <param name="stickers">collection of stickers.</param> 
     /// <remarks></remarks> 
     protected void BindStickerGrid() 
     { 
      if (Session["FisherId"] != null) 
      { 
       Collection<Sticker> _stickerCollection = _manager.GetStickerDetailsForGrid(Session["FisherId"].ToString(), "fisher"); 

       if (_stickerCollection != null) 
       { 
        if (_stickerCollection.Count > 0) 
        { 
         gvSticker.DataSource = _stickerCollection; 
         gvSticker.DataBind(); 
        } 
       } 
      } 
     } 

回答

1

您確保RowDataBound事件被激發每個頁面加載時間?我認爲GridView控件可能會在發生回發時從ViewState中獲取數據。

UPDATE

也許有你的邏輯錯誤。您將持續啓用和禁用每行的按鈕,這意味着如果最後一張標籤處於活動狀態,它們將被禁用,如果最後一張標籤處於非活動狀態,則會反向。這裏是我會建議你做:

  1. 在你RowDataBound事件處理程序算活躍不乾膠貼紙數量(或只是使用標明活性貼紙是否是當前頁面上找到的標誌)。
  2. 訂閱PreRender事件並將按鈕切換到適當狀態,具體取決於當前頁面上將呈現多少活動貼紙。

- 帕維

+0

看到我的問題,增加了PageIndex的改變和有約束力的方法 – 2011-01-05 11:42:01

+0

我已經編輯我的答案更新。 – volpav 2011-01-05 12:36:56