繁体   English   中英

如何在列表视图的特定行中禁用编辑按钮

[英]How to disable edit button in specific row in Listview

我有一个ASP Net Web表单,我需要禁用ASP Net Listview中特定行的按钮。 我在此行的Status = "Reject"时,需要在列表视图行列中禁用按钮(仅用于此行,而不是所有列表视图)
我了解如何从代码隐藏中找到它,以及如何在aspx页面中禁用它,但我不了解如何针对特定行禁用它。
如何禁用Listview中特定行的编辑按钮?
我的代码和我在下面尝试的内容:

     <asp:ListView ID="ListView2" ItemType="DocCat.Models.Inf"  DataKeyNames="Id" EnableViewState="false"  runat="server" OnItemDataBound="ListView2_ItemDataBound" >
        <LayoutTemplate>
            <div class="outerContainer">
                <table id="docTable">
                    <thead>
                    <tr>
                  <th>ID</th><th>Name</th>
<th>Status</th></tr>
                        </thead> <tbody runat="server" id="itemPlaceholder"></tbody></table> </div> </LayoutTemplate>
<ItemTemplate ><tr> <td><%# Item.ID %></td> <td><%# Item.F2 %></td>
         <td><%# Item.Status %></td>
                <td>
                    <asp:Button ID="YesBtn" runat="server" Text="Bla"   CommandName="Update" Onclick="YesBtn_Click"  /> </td>
                 <td><asp:Button ID="NoBtn" runat="server" Text="Bla" CommandName="Update"  Onclick="NoBtn_Click"  />
                     </td></tr> </ItemTemplate></asp:ListView> 

我试图使它像这样:

 `<asp:Button ID="YesBtn"  Visible='<%# (Eval("ReqStatus") == "Reject" )?true:false %> ' ...  />   

我所有列表视图中的此禁用按钮都具有此状态
我尝试这样:

 protected void ListView2_ItemDataBound(object sender, ListViewItemEventArgs e){
           {
            if (e.Item == null) return;
            Button btn1 = (Button)e.Item.FindControl("YesBtn");
            btn1.Visible = false;

我不知道如何为此添加特定的行信息。
ASP.NET Web表单实体框架C#

我建议您遍历ListView的循环并获取状态列的索引。 最后,执行以下操作:

for (int i = 0; i < ListView1.Items.Count(); i++)
{
    //Get the Label by row
    Label label1 = (Label)ListView1.Items[i].FindControl("label1");
}

看到更多- 遍历ListView

更新: Default.aspx

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
      <ItemTemplate>
            <asp:Label ID="label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
            <asp:Label ID="label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
            <asp:Label ID="label3" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
            <asp:Button ID="button1" runat="server" Text="Add" Enabled='<%# Eval("Status").ToString() == "1" %>' />
            <br />
      </ItemTemplate>
</asp:ListView>

Default.aspx.cs:只需将下面的代码粘贴到后面的代码中

protected void Page_Load(object sender, EventArgs e)
    {
        ListView1.DataSource = GetAllUsers();
        ListView1.DataBind();
    }

    public class User
    {
        public int ID { get; set; }
        public string name { get; set; }
        public string Status { get; set; }
    }

    public List<User> GetAllUsers()
    {
        User aUser = new User();

        List<User> Users = new List<User>();

        Users.Add(new User() { ID = 1, name = "AT-2016", Status = "1" } );
        Users.Add(new User() { ID = 2, name = "AT-2014", Status = "0" } );

        return Users;
    }

    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        for (int i = 0; i < ListView1.Items.Count(); i++)
        {
            //Get the Label by row
            Label status = (Label)ListView1.Items[i].FindControl("label3");
            Button button1 = (Button)ListView1.Items[i].FindControl("button1");

            if (status.Text == "1")
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }
    }

样本输出:

ListView_Button_Disable

尝试这个:

<asp:Button Visible='<%# Eval("ReqStatus").ToString() == "Reject" %>' runat="server" Text="Button1" ID="YesBtn" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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