0

I have one button to change based on the page. I have 3 page which is confirmed, pending and rejected.

On confirmed and rejected the text inside the button is same but different for pending. How can change the word in the button. The button placed in the grid view.

Below the code in aspx file:

<asp:TemplateField>
    <HeaderTemplate>Actions</HeaderTemplate>
    <ItemTemplate>
         <asp:Button ID="lnkbtnInfo" runat="server" CssClass="btn btn-success" Text="" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' CommandName="Detail" /></td>
    </ItemTemplate>
</asp:TemplateField>

How can i do in the cs file, to use the if else. For rejected and confirmed page, the button is "view" and pending page is "review"

1
  • Have you want to change button text in codebehind? Which variable/property used to determine "confirmed", "rejected" and "pending" state? Commented Jun 21, 2016 at 1:55

3 Answers 3

1

I figured that you need to use RowDataBound event handler on gridview and create if statement based on button's CommandArgument state:

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i < GridView.Rows.Count - 1; i++)
    {
        Button status = (Button)GridView.Rows[i].FindControl("lnkbtnInfo"); // find control from your button ID

        String state = status.CommandArgument.ToString(); // assume the value given by Eval data binding
        if (state.Equals("confirmed") || state.Equals("rejected"))
        {
            status.Text = "view";
        }
        else // if (state.Equals("pending"))
        {
            status.Text = "review";
        }
    }
}

Reference: Change button text in asp:gridview based on cell value C#

Sign up to request clarification or add additional context in comments.

4 Comments

did i need to change in text="" part in aspx file?
Tetsuya, the command argument actually refer to id which is order id and it will show the status based on the order id created by user.is there any way to hard code the button name?
You can set directly Text="view" in asp:Button, however if you want to change it programmatically from gridview's cell value when page state is pending, use proper CommandArgument to pass page state value, then set string variable state from it.
thanks for the idea, i have modify it a bot as the eval data binding not refer the status, it refer to id order..refer to my answer above
0

What you can also do is use Eval with ternary for changing Button Text on .aspx page,

Ternary Operator syntax

(your condition) ? "if true value":" if false value";

Here is Example " if you want to check that your 'Id' is 3 then Button Text should be View else It should be Review."

<asp:Button ID="lnkbtnInfo" runat="server" CssClass="btn btn-success" Text='<%# ((int)Eval("Id") == 3) ? "view":"Review" %>' CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' CommandName="Detail" />

Comments

0

Here my answer that works on my situation:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            Button name = (Button)GridView1.Rows[i].FindControl("lnkbtnInfo"); // find control from your button ID
            Label status= (Label)GridView1.Rows[i].FindControl ("lblStatus");
            if (status.Text == "Complete" || status.Text == "Rejected" || status.Text == "Cancelled" || 
                status.Text == "Returned" || status.Text == "UserRejected")//refer to confirm and reject order
            {
                name.Text = "View";
            }

            else // refer to pending order
            {
                name.Text = "review";
            }

        }
    }

Comments

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.