Asp linkbutton文本取决于gridview中的复选框控件

时间:2015-06-21 08:15:05

标签: c# asp.net gridview

是否可以根据gridview列中复选框的状态更改链接按钮的文本?

查看:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowCommand="FireRowCommand" >
<AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:CommandField ShowEditButton="False" ShowDeleteButton="False" />
    <asp:TemplateField HeaderText="Approve">
        <ItemTemplate>
            <asp:LinkButton ID="approveBtn" runat="server" CommandArgument='<%# Eval("Id") %>'
                CommandName="ApproveCmd" Text="Mark Approved" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
    <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
    <asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
    <asp:BoundField DataField="BusinessName" HeaderText="BusinessName" SortExpression="BusinessName" />
    <asp:CheckBoxField DataField="IsVerified" HeaderText="IsVerified" SortExpression="IsVerified" />
    <asp:CheckBoxField DataField="IsNewsletter" HeaderText="IsNewsletter" SortExpression="IsNewsletter" Visible="false"/>
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
</Columns>

我希望将文字更改为&#34; Mark Unapproved&#34;如果IsVerified是真的。

代码隐藏:

protected void FireRowCommand(object sender, GridViewCommandEventArgs e)
        {

            var command = e.CommandName;

            var clientId = Convert.ToInt32(e.CommandArgument);

            switch (command)
            {

                case "ApproveCmd":

                    var service = new WholesaleClientService();
                    service.ToggleClientVerfication(clientId);
                    break;
            }
        }

3 个答案:

答案 0 :(得分:0)

复选框上有一个可用于进行任何更改的事件,它是CheckedChanged事件,https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged(v=vs.110).aspx

答案 1 :(得分:0)

将此添加到.cs文件中:(假设 CheckBox1 是您必须添加到复选框的ID)

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = CheckBox1 as CheckBox;
    if (chk != null && chk.Checked)
    {
        approveBtn.Text ="Mark Unapproved"
    }
}

答案 2 :(得分:0)

一位同事想出了以下解决方案:

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)         {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
            var check = ((CheckBox)(e.Row.Cells[9].Controls[0])).Checked;

            if (check == true)
            {
                LinkButton btn = (LinkButton)e.Row.Cells[1].FindControl("approveBtn");
                btn.Text = "Mark Unapproved";
            }
        }
    }