Home > .Net, Musings, Problems > ASP.Net dynamic control/postback/event-handler madness – Part 1

ASP.Net dynamic control/postback/event-handler madness – Part 1

Well, I recently had a difficult problem that I solved in a way that I don’t really like, but it works all the same. In the process I discovered something bizarre about ASP.Net. Here was the problem: I had a dataset coming out of a SQL stored procedure (that I had no control over or information about, except the output). The dataset needed to be grouped (1 – by period) and subgrouped (2 – by description) based on the properties, then displayed in a table for editing (3), if conditions permitted (4).

I solved these 4 problems with some interesting logic, first based on a container object that I populated out of the database. Then I did the following to solve problem 1:

HashSet periods = new HashSet();
List report = Helper.GetReport();
foreach (objContainer container in report)
{
periods.Add(container.Period);
}

Dictionary<string, List> periodDictionary = new Dictionary<string, List>();
//need to get periods and descriptions within them.
foreach (string period in periods)
{
periodDictionary.Add(period, report.FindAll(p => p.Period == period));
}

This gave me the values broken down into a dictionary of the containers grouped by their period property, but I had to do this again in order to group them again, and then I had to break it down again in order to output the information to the screen. Which, presented a problem, incidentally. I needed a way to display this information in an aspx. So, I just created a div tag with a server side ID value (grids) and then added controls to it, which you’ll see here:

foreach (KeyValuePair<string, List> kvp in periodDictionary)
{
//Okay, broken down by periods now.
//Now we need to break down each value list by description in the same way.

//print header
Label title = new Label();
title.Text = kvp.Key;
title.CssClass = “PeriodHeader”;

grids.Controls.Add(title);

HashSet descriptions = new HashSet();
foreach (objContainer container in kvp.Value)
{
descriptions.Add(container.Desc);
}

Dictionary<string, List> descDictionary = new Dictionary<string, List>();
foreach (string description in descriptions)
{
descDictionary.Add(description, kvp.Value.FindAll(p => p.Desc == description));
}

foreach (KeyValuePair<string, List> kvp2 in descDictionary)
{
Label subtitle = new Label();
subtitle.Text = kvp2.Key;
subtitle.CssClass = “DescHeader”;

grids.Controls.Add(subtitle);
DataGrid grid1 = BuildDataGrid(kvp2.Value);
grids.Controls.Add(grid1);
}
}

I’m sure anyone could pick out issues here, primarly the use of a DataGrid instead of a GridView. I like the DataGrid, okay?!? Get off my case!! Anyway, this worked and allowed me to just generate the information and group it with classes as needed in my BuildDataGrid() method. The specifics aren’t important, but here are a few examples of how I created the grid:

dataSource = dataSource.OrderByDescending(o => o.TransactionDate).ToList();
DataGrid grid = new DataGrid();
grid.DataSource = dataSource;
grid.ItemDataBound += new DataGridItemEventHandler(this.grid_ItemDataBound);
grid.ItemCommand += new DataGridCommandEventHandler(this.Grid_ItemCommand);

grid.AutoGenerateColumns = false;
grid.BorderStyle = BorderStyle.None;

BoundColumn column1 = new BoundColumn();
column1.DataField = “TransactionDate”;
column1.DataFormatString = “{0:MMM dd,yyyy}”;

ButtonColumn column4 = new ButtonColumn();
column4.ItemStyle.CssClass = “editButton”;
column4.DataTextField=”ShowEdit”;
column4.CommandName = “Edit”;
column4.ButtonType = ButtonColumnType.PushButton;

grid.Columns.Add(column1);
grid.Columns.Add(column4);
grid.DataBind();

return grid;

Okay, this long post is getting very long, but the point is that I was able to dynamically generate the columns and buttons and data all on the PageLoad() call and entirely server side. The bolded lines show how I was able to add the event handler code to the grid so that I could get in the way of the binding and the itemCommand events to adjust things as needed (styling, and dynamic commands like delete or edit based on the CommandName).

Categories: .Net, Musings, Problems
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *