0

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.

Here is the code I have:

My .aspx page:

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

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

    }

2 Answers 2

2

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";
            }
        }   
    }
Sign up to request clarification or add additional context in comments.

Comments

1
<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 = lblactive.Text;
    }
}

3 Comments

Useful. Thanks :)
Ooh Isn't that last line wrong? Should it instead be: btnnew.Text = lblactive.Text;
@Zeek2 Yes i will change it. thanks to identified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.