C# 基于网格行启用禁用按钮

C# 基于网格行启用禁用按钮,c#,asp.net,webforms,C#,Asp.net,Webforms,嗨,我有一个网格控件,我在其中绑定标签列表。在网格的上方,即外侧网格上,我有两个按钮创建标签和空白标签。 标签基本上有三个属性:活动、无效和过期,在列中显示为文本。一次只添加一个标签是有条件的。此外,如果存在活动标签,则用户无法添加其他标签,除非其过期或无效 所以我想要的是,每当我的网格被加载时,如果有一列文本处于活动状态,那么创建/添加标签将被禁用,而void将被启用。我正在使用下面的代码 /// <summary> /// Handles the RowDataBound e

嗨,我有一个网格控件,我在其中绑定标签列表。在网格的上方,即外侧网格上,我有两个按钮创建标签和空白标签。 标签基本上有三个属性:活动、无效和过期,在列中显示为文本。一次只添加一个标签是有条件的。此外,如果存在活动标签,则用户无法添加其他标签,除非其过期或无效

所以我想要的是,每当我的网格被加载时,如果有一列文本处于活动状态,那么创建/添加标签将被禁用,而void将被启用。我正在使用下面的代码

  /// <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;
    }
}
//
///处理gvSticker控件的RowDataBound事件。
/// 
///事件的来源。
///包含事件数据的实例。
/// 
受保护的无效gvSticker_RowDataBound(对象发送者,System.Web.UI.WebControl.GridViewRowEventArgs e)
{
if(会话[“FisherId”]!=null)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
Label lblStatus=(Label)e.Row.FindControl(“lblStickerStatus”);
if(lblStatus.Text.Contains(“活动”))
{
btnAddSticker.Enabled=false;
btnVoidSticker.Enabled=true;
HyperLink HLSTICKNUM=(HyperLink)e.Row.FindControl(“HLSTICKNUMBER”);
hlstacknum.Attributes.Add(“样式”,
“光标:手;文字装饰:下划线;字号:粗体;”;
如果(!string.IsNullOrEmpty(hlsticknum.Text.Trim()))
{
字符串urlWithParameters=“Stickers.aspx?StickerId=”
+hlnum.Text;
添加(“OnClick”、“popWinNote(”)+
urlWithParameters+“')”;
}
}
其他的
{
btnAddSticker.Enabled=true;
btnVoidSticker.Enabled=false;
}
}
}
其他的
{
btnAddSticker.Enabled=true;
btnVoidSticker.Enabled=false;
}
}
它在电网的第一个负载上运行良好。但每当我更改网格的页面索引时都会失败

更新

下面是绑定和页面索引交换事件

  /// <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();
                    }
                }
            }
        }
//
///处理gvSticker控件的PageIndexchange事件。
/// 
///事件的来源。
///包含事件数据的实例。
/// 
受保护的无效GV标签\u页面索引交换(对象发送器,GridViewPageEventArgs e)
{
gvSticker.PageIndex=e.NewPageIndex;
BindGrid();
}
/// 
///绑定标签网格。
/// 
///收集贴纸。
/// 
受保护的空网格()
{
if(会话[“FisherId”]!=null)
{
Collection\u stickerCollection=\u manager.GetStickerDetailsForGrid(会话[“FisherId”].ToString(),“fisher”);
if(_-collection!=null)
{
如果(_.collection.Count>0)
{
gvSticker.DataSource=\u stickerCollection;
gvSticker.DataBind();
}
}
}
}

您确定每次加载页面时都会触发
RowDataBound
事件吗?我认为当回发发生时,
GridView
控件可能会从ViewState获取数据

更新

也许你的逻辑有错误。连续启用和禁用每行的按钮意味着,如果最后一个标签处于活动状态,则按钮将被禁用;如果最后一个标签处于非活动状态,则按钮将被禁用。以下是我建议您的做法:

  • RowDataBound
    事件处理程序中,计算活动标签的数量(或者只使用指示是否在当前页面上找到活动标签的标志)
  • 订阅
    PreRender
    事件,并根据当前页面上呈现的活动标签数量将按钮切换到适当的状态

  • --Pavel

    参见我的问题,添加了页面索引更改和绑定方法的更新