【问题标题】:Asp linkbutton text depending on checkbox control in gridviewAsp linkbutton文本取决于gridview中的复选框控件
【发布时间】:2015-06-21 08:15:04
【问题描述】:

是否可以根据 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>

如果 IsVerified 为真,我希望将文本更改为“Mark Unapproved”。

代码隐藏:

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;
            }
        }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      将此添加到您的 .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"
          }
      }
      

      【讨论】:

      • 谢谢。但是,我的复选框上没有 .Checked 属性。我错过了什么吗?
      • 编辑了我的回复,但您可能需要稍微自定义一下
      • 这行得通,但我发现我无法在网格视图中的复选框中添加 ID 属性。认为我将不得不改变我实现这一点的方式。对不起,我不是很擅长asp.net。我已经用额外的代码更新了这个问题,以便更好地了解我所拥有的内容。
      【解决方案3】:

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

      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";
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 2011-08-02
        • 2015-10-08
        • 1970-01-01
        相关资源
        最近更新 更多