Question

I have search page which may return multiple search results in separate DataGrid controls, or if the search is specific enough, a single result in a single grid.

Should only one result be found, I then want to invoke the click of a ButtonColumn in the sole row of that grid to then open a separate page, as if the user clicked it themselves.

Here is my Page_LoadComplete event handler:

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??


        }            
    }
}

How can I obtain a reference to the ButtonColumn in question and then proceed to programmatically click it?

Was it helpful?

Solution

Found a solution. I programmatically invoke the OnSelectedIndexChanged event handler (defined as Select_Change) like so:

    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());
                }
            }
        }

    }

Using the reference to the DataGrid with the single row, I also set its SelectedIndex to the first (and only) row so that I can proceed with other operations after the invocation begins.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top