【问题标题】:Programmatically click a ButtonColumn within a DataGrid?以编程方式单击 DataGrid 中的 ButtonColumn?
【发布时间】:2014-05-01 22:23:42
【问题描述】:

我的搜索页面可能会在单独的 DataGrid 控件中返回多个搜索结果,或者如果搜索足够具体,单个会导致单个网格。

如果只找到一个结果,然后我想在该网格的唯一一行中调用ButtonColumn 的点击,然后打开一个单独的页面,就像用户自己点击它一样。

这是我的Page_LoadComplete 事件处理程序:

protected void Page_LoadComplete(object sender, EventArgs e)
{
    var allControls = new List<DataGrid>();

    // Grab a list of all DataGrid controls on the page.
    GetControlList(Page.Controls, allControls);

    var itemsFound = allControls.Sum(childControl => childControl.Items.Count);

    for (var i = 0; i <= allControls.Count; i++)
    {
        itemsFound += allControls[i].Items.Count;

        // If we're at the end of the for loop and only one row has
        // been found, I want to get a reference to the ButtonColumn.
        if (i == allControls.Count && itemsFound == 1)
        {
            var singletonDataGrid = allControls[i];

            // **Here** I want to reference the ButtonColumn and then
            // programmatically click it??


        }            
    }
}

如何获得对相关ButtonColumn 的引用,然后以编程方式单击它?

【问题讨论】:

  • 客户端发生点击。您的 C# 存在于服务器上。您无法从服务器单击它。您可以改为调用按钮的单击处理程序。

标签: c# asp.net datagrid


【解决方案1】:

找到了解决办法。我以编程方式调用OnSelectedIndexChanged 事件处理程序(定义为Select_Change),如下所示:

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        var allControls = new List<DataGrid>();

        // Grab a list of all DataGrid controls.
        GetControlList(Page.Controls, allControls);

        var itemsFound = 
            allControls.Sum(childControl => childControl.Items.Count);

        for (var i = 0; i < allControls.Count; i++)
        {
            if (allControls.Count > 0 && allControls[i].ID == "grid")
            {
                // If a single row is found, grab a reference to the
                // ButtonColumn in the associated grid.
                if (i == (allControls.Count - 1) && itemsFound == 1)
                {
                    var singletonDataGrid = allControls[i];

                    singletonDataGrid.SelectedIndex = 0;

                    Select_Change(singletonDataGrid, new EventArgs());
                }
            }
        }

    }

使用对单行的DataGrid 的引用,我还将其SelectedIndex 设置为第一行(也是唯一一行),以便在调用开始后继续进行其他操作。

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2011-10-22
    相关资源
    最近更新 更多