如何禁用Listview中特定行的编辑按钮

时间:2016-10-18 15:16:48

标签: javascript c# asp.net listview

我有一个asp网络表单,我需要禁用asp net 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> 中为此行禁用按钮(仅适用于此行,不适用于所有列表视图)
我知道如何从代码隐藏中找到它,以及如何在aspx页面中禁用它,但我不明白如何禁用它在特定的行中。
如何在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;

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

--latex-engine=xelatex

我不知道如何为此添加特定的行信息 Asp.Net Web表单实体框架c#

2 个答案:

答案 0 :(得分:1)

我建议您遍历ListView循环并获取状态列的索引。最后做一些类似的事情:

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

有关详情,请参阅此内容 - Loop Through 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

答案 1 :(得分:1)

试试这个:

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