0

我正在尝试将网格视图中的单元格值传递给我后面的代码。最终,我需要根据单元格值更改按钮的文本。

这是我的代码:

我的 .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 代码在后面:

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

    }
4

2 回答 2

2

弄清楚了。这是我添加到我的代码隐藏中的内容:

  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";
            }
        }   
    }
于 2013-09-11T17:00:43.353 回答
0
<asp:GridView ID="gridView1" runat="server" OnRowDataBound="gview_RowDataBound" 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>


protected void gview_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button btnnew = (button)e.Row.FindControl("buttonChangeActiveStatus");
        Label lblactive = (Label)e.Row.FindControl("Active");
        btnnew.Text = Label.Text;
    }
}
于 2013-09-11T16:31:57.007 回答