【问题标题】:Change GridView Custom Button Text and Css on click event在单击事件上更改 GridView 自定义按钮文本和 Css
【发布时间】:2019-03-02 16:33:52
【问题描述】:

我正在开展一个项目,我正在为管理员创建仪表板。 我有一个 UsersGridView,其中显示了注册用户的数据。 使用 GridviewTemplate 字段,我创建了一个按钮,允许 Admin 使用 Lockout 或 允许用户使用系统。

<asp:TemplateField HeaderText="LockoutStatus">
    <ItemTemplate>
         <asp:Button ID="LockoutStatus" runat="server" CausesValidation="false" CommandName="LockoutStatus" Text="Enabled"
            CommandArgument='<%# Eval("Id") %>' />
   </ItemTemplate>
</asp:TemplateField>

RowCommand 事件中,如果用户被系统锁定,我该如何更改按钮 CssClass 和文本。

【问题讨论】:

    标签: css asp.net button gridview webforms


    【解决方案1】:

    有几种方法可以更改 CssClass。

    使用 RowDataBound 事件。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //check if the row is d datarow
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //cast the row back to a datarowview
            DataRowView row = e.Row.DataItem as DataRowView;
    
            //use findcontrol to locate the butotn
            Button btn = e.Row.FindControl("LockoutStatus") as Button;
    
            //change the class based on a column value
            if (row["ColumnName"].ToString() == "LockedOut")
            {
                btn.CssClass = "ClassA";
            }
        }
    }
    

    或者在带有三元运算符的aspx页面上。

    <asp:Button ID="LockoutStatus" runat="server" 
        CssClass='<%# Eval("ColumnName").ToString() == "LockedOut" ? "ClassA" : "ClassB" %>'
    

    或者在 RowCommand 事件中如您所愿。您可以使用 CommandSource 并将其转换为 Button。

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button btn = e.CommandSource as Button;
        btn.CssClass = "ClassA";
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2020-11-17
      • 2018-09-15
      • 2018-10-15
      • 1970-01-01
      相关资源
      最近更新 更多