嗨我有一个网格控件,我在哪里绑定贴纸列表。在上面的网格中,即外侧网格我有两个按钮Create Sticker和Void Sticker。 贴纸基本上有三个属性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();
}
}
}
}
答案 0 :(得分:0)
您确定每次加载页面时都会触发RowDataBound
事件吗?我认为GridView
控件可能会在发生回发时从ViewState获取数据。
<强>更新强>
也许您的逻辑中存在错误。您将持续启用和禁用每行的按钮,这意味着如果最后一个贴纸处于活动状态,它们将被禁用,如果最后一个贴纸处于非活动状态,则相反。以下是我建议你做的事情:
RowDataBound
事件处理程序中计算活动贴纸的数量(或者只使用指示当前页面上是否找到活动贴纸的标记)。PreRender
事件并将按钮切换到适当的状态,具体取决于当前页面上将呈现的活动贴纸数量。- 帕维尔