Archive

Archive for the ‘.Net’ Category

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

May 13, 2013 Leave a comment

As a continuation of the HOW I was able to deal with dynamically creating grids based on a query, I had a problem. The problem was that that the ItemCommand was NEVER firing on postback. If you already know where I am going with this, then I’m sorry, but this was a crazy problem that was having a tough time getting to the bottom of. None of my debug code would ever hit or do anything related to that generated command button click, even though the post back happened.

When testing, I also noticed that all of my grids were disappearing on the post back, which made sense, because I had done this is usual:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

But, it appears that when the Page_Load fires on the postback and it does NOT create the control that caused the postback in the first place (my grid with a command button), it never fires the event handler! I was able to get it all to work by removing the IsPostBack check, but it seems silly to think that I am recreating the control from scratch in order to fire a button click. Almost like the button that ASP.Net is saying was clicked is gone forever and is replaced by another one which IT thinks was clicked. Kind of bizarre!!

Categories: .Net, Musings, Problems

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

May 13, 2013 Leave a comment

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

.Net vs Java

August 20, 2012 Leave a comment

I am sure that you are wondering, or have been brought here by trying to decide the merit or differences between Microsoft’s .Net and Sun/Oracle’s Java platforms.  There are a ton of sites that will give you the technical differences, ( Wikipedia has a few good ones: http://en.wikipedia.org/wiki/Comparison_of_the_Java_and_.NET_platforms and http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp) and I will touch on the differences, but here is the biggest and only substantial difference: personal preference.

 

About me, briefly: I learned VB.Net in college, right after it was released.  Many thanks to Dr. Jenny, who will likely never read this, but had the forethought to teach a class on this new technology, and it was a revelation to me and felt far superior (which it is, and I can defend that position easily) to C++ which I had studied previously.  I then entered the workforce doing a combination of VB.Net and C# depending on the project (see a future Musing for those practical differences).  After about 4-5 years of .Net work in various capacities, I took it upon myself to learn Java and try to expand my knowledge base.  I studied several books and took the SCJP certification test, which I passed handily.  Passing it handily was a result of study, but honestly was mainly a result of knowing C# and how to use it.

 

Throughout the years, Microsoft and Sun/Oracle have copied each other and the great ideas that they have respectively had: generics and collections, delegates, properties, foreach loops, anonymous things. There has been an almost constant convergence that  has started to reverse, but I believe will continue in the long term.  As one iteration of a language invents a new way of doing things, the other will inevitably follow, however, they will both attempt to stay ahead of the other.  Consider this similar to the great marketing rivalries in technology, Microsoft/Google vs. Apple, Microsoft vs Sony vs Nintendo.  Perhaps the biggest similarity is that Microsoft is always on one end of it!

 

Having gone from .Net to Java, specifically C# 3.5 to Java 6, it was very straightforward and easy to adjust.  Coming back from Java 6 to C# 4.0 was likewise easy, however one thing that is overlooked in the technical discussion is the IDE.  Microsoft’s Visual Studio is a great IDE.  Especially once you are used to it and finding everything, it works great.  Snippets and searching and managing projects is great, as well as integration with Visual Source Safe and/or Subversion for source control.  Oracle’s Eclipse is also a great IDE.  If you are ever trying to learn how to work with it, print out a cheat sheet of keyboard shortcuts!  (http://www.n0sl33p.org/dev/eclipse_keys.html)  I find myself missing the shortcuts like Cntl+Shift+R/T for quick class opening immensely.

 

If you are going from C# to Java, it will be as easy as getting used to Eclipse.  If you are going from Java to C#, I recommend reading a book to get used to Visual Studio and the Microsoft world as a whole.  If you are going to or coming from VB.Net, then you are in trouble, unless you have done some C-related coding in the past.  In that case, pick up an introductory book to VB.Net or Java to ensure you can grasp the syntax differences.  VB.Net reads like a book, Java and C# read like code, and you may need a translator to pull that off.  The general object oriented topics and abstraction will always be the same and at the end of the day any of these can meet your needs and they all have pluses and minuses, but it really just depends on what your coder likes the best.  Always put them in a position to give you the best code and you will [usually] get the best code.  Frameworks and platforms can be adjusted easily, but coders who are stuck in their ways cannot.

 

Personally, I miss Java right now, but I really missed .Net when I was working with Java, so that just means that they’re truly about the same!!

Categories: .Net, Java, Musings