根据单元格值C#更改asp:gridview中的按钮文本 [英] Change button text in asp:gridview based on cell value C#

查看:50
本文介绍了根据单元格值C#更改asp:gridview中的按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单元格值从gridview传递到后面的代码中.最终,我需要根据单元格的值更改按钮的文本.

I’m trying to pass a cell value from a gridview to my code behind. Ultimately, I need to change the text of a button depending on the cell value.

这是我的代码:

我的.aspx页面:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
  <ContentTemplate>
    <asp:GridView ID="gridView1" runat="server" OnRowCommand="gview_RowCommand" AutoGenerateColumns="False">
        <Columns>
             <asp:TemplateField HeaderText="Active Status">
                <ItemTemplate>
                    <asp:Label ID="Active" runat="server" Text='<%# Eval("IsActive") %>'></asp:Label>
                    <asp:Button runat="server" ID="buttonChangeActiveStatus" CommandName="<%# Eval("IsActive") %>" CommandArgument="<%# Eval("IsActive") %>" onclick="buttonChangeActiveStatus_Click" text=""/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
     </asp:GridView>
</ContentTemplate>

后面还有我的.cs代码:

And my .cs code behind:

protected void gview_RowCommand(Object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i <= gridView1.Rows.Count - 1; i++)
        {
            Button activeButton = (Button)gridView1.Rows[i].FindControl("buttonChangeActiveStatus");


           // if (cellValue = true)
           // {
                //change button text foo
           // }
           // else
           // {
           //     //change button text to bar
           // }    
        }

    }

推荐答案

弄清楚了.这是我添加到我的后台代码中的内容:

Figured it out. here's what i added to my code-behind:

  protected void RowDataBound(Object sender, GridViewRowEventArgs e)
    {

        for (int i = 0; i <= gridView.Rows.Count - 1; i++)
        {
            Button activeButton = (Button)gridViewTenantDetails.Rows[i].FindControl("buttonChangeActiveStatus");

            string cellValue = activeButton.CommandArgument.ToString();
            if (cellValue == "True")
            {
                activeButton.Text = "foo";

            }

            else
            {
                activeButton.Text = "bar";
            }
        }   
    }

这篇关于根据单元格值C#更改asp:gridview中的按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆