简体   繁体   中英

Enable Disable button on the basis of grid row

Hi I have a grid control where i am binding list of stickers. On the above of grid , that is out side grid i have two buttons Create Sticker and Void Sticker. Sticker basically have three properties Active , Void and Expired displayed as text in column. There is condition of adding only one sticker at a time. Also if there is an active sticker then user cannot add another sticker until and unless its is expired or void.

So what i want is that whenever my grid is loaded if there is an column with text active the the create / add sticker will get disabled and void will get enabled. I am using the folloing code

  /// <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;
    }
}

It works well on the first load of the grid. But fails whenever I changes the page index of grid.

Update

Here is binding and pageindexchanging events

  /// <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();
                    }
                }
            }
        }

Are you sure that the RowDataBound event is fired every time the page is loaded ? I think the GridView control might take the data from the ViewState when the postback is occured.

UPDATE

Maybe there is an error in your logic. You're continuously enabling and disabling the buttons for each row meaning that they will be disabled if the last sticker is active and opposite if the last sticker is inactive. Here is how I'd suggest you to do:

  1. In your RowDataBound event handler count the number of active stickers (or just use the flag indicating whether active sticker was found on the current page).
  2. Subscribe to PreRender event and switch buttons to appropriate state depending on how many active stickers will be rendered on the current page.

-- Pavel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM