【问题标题】:Display delete button in gridview in gridview base on condition根据条件在gridview的gridview中显示删除按钮
【发布时间】:2015-05-25 10:18:08
【问题描述】:

我需要根据某些条件显示删除按钮。

ASPX:

<asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"
                         ImageUrl="~/Images/Delete.gif" Visible="false" />
    </ItemTemplate>
</asp:TemplateField>

代码隐藏:

protected void gvL3App_RowDataBound(object sender, GridViewRowEventArgs e)
{            
    DataTable dtInactiveCodes = DataRepository.getInactiveCodes();
    string[] strInactive = dtInactiveCodes.AsEnumerable()
                           .Select(row => row.Field<string>("Code")).ToArray();
    foreach (var value in strInactive)
    {
         ImageButton btnDelete= (ImageButton)e.Row.FindControl("btnDelete");
         btnDelete.Visible = true;
    }
}

这里

dtInactiveCodes 数据表返回值如145248268478 等。现在,gridview 的第一列将具有这些数据表返回值。我需要检查这些值并显示删除。

但删除按钮对所有行都是可见的,并且不适用于上述代码。

谁能帮我解决这个问题?

【问题讨论】:

  • 我认为你需要在if (value != string.empty){ btnDelete.Visible = true; } 内执行此操作 foreach 循环
  • 就这样吧,Visible=&lt;%# Eval("show").toString() == "true" ? "false" %&gt;。如果它取决于所选表中的键或值。
  • @VinodKumar Eval("show") 是什么?
  • @RahulNikate Eval 是您使用从绑定数据源获取值的东西。阅读msdn.microsoft.com/en-us/library/4hx47hfe%28v=vs.110%29.aspx 了解更多信息。
  • @RahulNikate - 我尝试了你的建议,但还是一样。有没有其他方法可以让它工作。

标签: asp.net gridview


【解决方案1】:

您可以拥有一个通用函数SetButtonVisibility,它将接收"code" 或您要检查的任何列名称。

ASPX:

<asp:TemplateField>
<ItemTemplate>
<!-- I have added a additional parent Div to contain the row style... You could use or replace this with some other element -->
<div class='<%# CssClassForRow(Eval("code").ToString()) %>'>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"     
ImageUrl="~/Images/Delete.gif" Visible='<%# SetButtonVisibility(Eval("code").ToString()) %>' />
</div>
</ItemTemplate>
</asp:TemplateField>

这里只是重用你的相同逻辑来比较这个"code"是否存在于你的getInactiveCodes中并返回"true""false"。 代码背后:

public bool SetButtonVisibility(String codeValueOfRow)
{
  if(GetMatchingCodeCount(codeValueOfRow) > 0) return true;
  else return false;
}

public string CssClassForRow(String codeValueOfRow){
  //your logic here to check which CssClass to apply

  //If you want the logic to be same as above, then I suggest calling the same method.
  if(GetMatchingCodeCount(codeValueOfRow) > 0) return "highlight";
  else return "non_highlight";

  //[OR]

  //If you want to use some other logic... then...
  //A simple sample logic would be...
  if(someValue == "highlight") return "highlight";
  else return "non_highlight";
}

//Creating this method, to avoid code repetition.
public int GetMatchingCodeCount(string codeValue) {
  DataTable dtInactiveCodes = DataRepository.getInactiveCodes();
  return dtInactiveCodes.AsEnumerable().Count(row => row.Field<string>("Code")==codeValue);
}

Css 样式表:

.highlight {
  background-color: #f00;/* Hexa-decimal color code for background*/
}
.non_highlight { /* No style here ... But you could add something here for the other rows, if you want*/ }

希望这会有所帮助。

【讨论】:

  • 我收到此错误Operator '&gt;' cannot be applied to operands of type string[] and int。另外,如何将此逻辑应用于我的 rowdatabound 事件?
  • @Learner 哦。我的错误......这应该是Count 而不是Select
  • @Learner 您不必将此逻辑应用于行数据绑定。它在行创建/绑定时自动完成。
  • 我知道了。我现在收到此错误Cannot implicitly convert type string to bool
  • @Learner,没问题。我很乐意提供帮助。我已经用一些示例样式更新了代码。请检查并告诉我。
【解决方案2】:
if (e.Row.RowType == DataControlRowType.DataRow)
{
    Label lbl_firstCol = (Label)e.Row.FindControl("FirstCols");
    if (lbl_firstCol.Text=="145" || lbl_firstCol.Text=="248" || lbl_firstCol.Text=="268" ||  lbl_firstCol.Text=="478")  
    {
        ImageButton btnDelete= (ImageButton)e.Row.FindControl("btnDelete");
        btnDelete.Visible = true;
    }
}

【讨论】:

  • 这些值是动态的并且来自数据表,我不能像这样对值进行硬编码,所以方法必须是动态的。
猜你喜欢
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多