C# 以编程方式单击DataGrid中的按钮列?

C# 以编程方式单击DataGrid中的按钮列?,c#,asp.net,datagrid,C#,Asp.net,Datagrid,我有一个搜索页面,可以在单独的DataGrid控件中返回多个搜索结果,或者如果搜索足够具体,可以在单个网格中返回单个结果 如果只找到一个结果,那么我想调用该网格唯一一行中按钮列的单击,然后打开一个单独的页面,就像用户自己单击它一样 以下是我的Page_LoadComplete事件处理程序: protected void Page_LoadComplete(object sender, EventArgs e) { var allControls = new List<DataGri

我有一个搜索页面,可以在单独的DataGrid控件中返回多个搜索结果,或者如果搜索足够具体,可以在单个网格中返回单个结果

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

以下是我的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的引用,然后以编程方式单击它?

找到了解决方案。我以编程方式调用定义为Select\u Change的OnSelectedIndexChanged事件处理程序,如下所示:


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

在客户端上单击。服务器上存在您的C。您无法从服务器上单击它。您可以调用按钮的单击处理程序。
    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());
                }
            }
        }

    }