【问题标题】:Make GridView AutGenerateColumn a LinkButton that fires RowCommand event使 GridView AutGenerateColumn 成为触发 RowCommand 事件的 LinkBut​​ton
【发布时间】:2017-09-03 08:36:37
【问题描述】:

我的搜索似乎是空白的简单问题。

我有一个 AutoGeneratingColumns=True 的 GridView

这会生成一个列“MyColumn”。我想让这些列的值成为一个可点击的 LinkBut​​ton,它使用特定的 CommandNmae 触发 GridView 的 RowCommand 事件,并将单元格值作为 CommandArgument。

我正在尝试在不使用自定义 ItemTemplate 的情况下执行此操作,我知道该怎么做。我希望我可以按照描述务实地修改 GridView 中自动生成的列。

蒂亚

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这可以在 GridView 的 OnRowDataBound 事件中完成。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //check if the current row is a datarow
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ///create a new linkbutton
            LinkButton button = new LinkButton();
            button.Text = "LinkButton";
            button.CommandArgument = "MyArgs";
    
            //assign a command to the button
            button.Command += Button_Command;
    
            //add the button to cell x
            e.Row.Cells[3].Controls.Add(button);
        }
    }
    

    因为您是动态添加控件,所以 GridView 的绑定不应包含在 IsPostBack 检查中。你通常会做的事情。

    【讨论】:

    • 如果我的 GridView 被绑定在按钮点击上(甚至在前一个屏幕上),我将如何处理?
    • 您必须将点击“存储”在某处,例如在会话中。因此,当页面重新加载时,您可以执行与单击按钮相同的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多