1

Below is gridview that I want it to be, ID, Name and Status are Label, Action is LinkButton

ID ------------- Name ------------- Status ------------- Action

1 -------------- Name1 ------------ Active ------------ Disable

2 -------------- Name2 ------------ In-active --------- Enable

3 -------------- Name3 ------------ Active ------------ Disable

How can I set the LinkButton Text to "Disable" or "Enable" depend on value (text) of Status (which is always either Active or In-active)?

My link button is as below, how can change the text 'Do you want to proceed' to 'Do you want to Disable' or 'Do you want to Enable' base on the logic above

<asp:LinkButton ID="lbtAction" runat="server"
               CommandArgument='<%# Eval("ID")%>'
               OnClientClick="return confirm('Do you want to proceed?')"
               OnClick="DoAction"></asp:LinkButton>
3
  • 1
    Use RowDataBound and place your logic there. Commented Mar 28, 2014 at 16:11
  • Do it on RowDataBound event. Commented Mar 28, 2014 at 16:11
  • That's correct as suggested by @TimSchmelter; do it on RowDataBound event Commented Mar 28, 2014 at 16:12

2 Answers 2

3

Use RowDataBound and place your logic there:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lbtAction = (LinkButton) e.Row.FindControl("lbtAction");
        Label lblStatus = (Label) e.Row.FindControl("lblStatus");
        bool active = lblStatus.Text == "Active";
        lbtAction.Text = active ? "Disable" : "Enable";
        string onClientClick  = string.Format("return confirm('Do you want to {0}?')",
                                               active ? "Disable" : "Enable");
        lbtAction.OnClientClick = onClientClick;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That solved my first problem, thank you! Can you help me with the second problem I just added to question?
@RonaldinhoState: i have edited my answer accordingly.
1

Use Gridview RowDataBound event to achieve the same. Like below (not exact code but you can start with alike)

void RowDataBound(object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(((Label)e.Row.FindControl("Status")).Text == "Active")
{
  //disable
}
else
{
//enable
}
}
}

2 Comments

He's using TemplateFields (since he mentioned labels and linkbuttons), then you can't use the Cell.Text because it's empty.
@TimSchmelter, correct. didn't observed that. Thanks for pointing that out.Answer edited. Was just trying to show the way to achieve the result. I am not much of asp.net guy :)

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.