简体   繁体   English

根据列值动态更改gridview的linkbutton的文本

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

I am from PHP background. 我来自PHP背景。 I have the following code for a GridView which is getting populated from the database. 我有下面的代码用于从数据库中填充的GridView。 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 . 例如,如果状态列的值是'pending',则链接按钮应显示文本Edit Details而不是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. 您需要使用RowDataBound事件来动态设置文本。 Get the reference to linkbutton and set it's text based on the data item. 获取对linkbutton的引用,并根据数据项设置其文本。 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 (1)这样会跳过页眉和页脚,因此代码仅针对“常规”行运行

In // modify the row here you can acces the controls inside the row, and modify them. // modify the row here您可以访问行内的控件,然后对其进行修改。

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). 您也可以使用GridView.RowDataBound事件 ,该事件具有传递到行的数据信息(与上一个示例类似)。

In both cases you have the Row available, and you can access all of its properties . 在这两种情况下,您都可以使用Row ,并且可以访问其所有属性 You'll probably use these properties: 您可能会使用以下属性:

  • Cells : cells (columns) of the row Cells :该行的单元格(列)
  • Controls : controls inside the row Controls :行内的控件
  • 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) DataItem :它允许您访问绑定到此路由的数据(您可以使用调试器来查看它如何进行必要的强制转换)

The process would be to look at the data in the DataItem , and look for the control (or cell) which you need to modify. 该过程将是查看DataItem中的数据,并查找您需要修改的控件(或单元格)。 (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). (我坚持在调试器上使用断点来浏览属性的值...它们有点麻烦,特别是数据项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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