简体   繁体   中英

Changing the text of a linkbutton of a gridview dynamically on the basis of a column value

I am from PHP background. I have the following code for a GridView which is getting populated from the database. I want to change the text of the link button based on the value of the status column in the grid. For example, if the value of the status column is 'pending' then the link button should show text Edit Details instead of View Details . How do I do this?

<asp:GridView ID="empres1" 
    runat="server" 
    AllowPaging="True" 
    AutoGenerateColumns="False" 
    onrowcommand="empres1_RowCommand" 
    onrowediting="empres1_RowEditing" 
    onselectedindexchanged="empres1_SelectedIndexChanged1">

        <asp:BoundField DataField="Status" HeaderText="Status" />
        <asp:BoundField DataField="comments" HeaderText="comments"   />
        <asp:TemplateField HeaderText="" SortExpression="">  
            <ItemTemplate>   
                <asp:LinkButton ID="LinkButtonEdit" runat="server" 
                    CommandName="ShowPopup" 
                    CommandArgument='<%#Eval("EmployeeId") %>'>View Details
                </asp:LinkButton> 
                                                 -------------------^
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

You need to use the RowDataBound event to set the text dynamically. Get the reference to linkbutton and set it's text based on the data item. So your code should look like this.

    protected void empres1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton button =
        e.Row.Cells[2].FindControl("LinkButtonEdit");
        if (button != null)
        {
            DataRow dr = e.Row.DataItem;
            if (dr["status"].ToString() == "Pending")
            {
                button.Text = "Edit Details";
            }
            else
            {
                button.Text = "View Details";
            }
        }

    }
}

The code may not be syntactically perfect, but you would get an idea from this.

You can use a server side event of the grid view:

In this event you can acces the controls created inside the row, and modify them, like this:

if(e.Row.RowType == DataControlRowType.DataRow)  // (1)
{
  // modify the row here
}

(1) this skips headers and footers, so that the code runs only for "regular" rows

In // modify the row here you can acces the controls inside the row, and modify them.

You can alternatively use the GridView.RowDataBound Event , which has the information of the data passed to the row (in a similar fashion to the previous sample).

In both cases you have the Row available, and you can access all of its properties . You'll probably use these properties:

  • Cells : cells (columns) of the row
  • Controls : controls inside the row
  • DataItem : it lets you acces the data bound to this route (you can use the debugger to see how it lloks like to make the necessary casting)

The process would be to look at the data in the DataItem , and look for the control (or cell) which you need to modify. (I insist on using a breakpoint on the debugger to browse the values of the properties... they are a bit cumbersome, specially the data item).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM