When I decided to start programming one of the first places I visited was Project Euler, not because it would teach me how to program but because it had the perfect challenges for using programming to solve problems when starting out learning to code. The problems are based on mathematics, starting off very easy (think fizzbuzz type thing) where it is mostly the programming part that is the challenge but very quickly the problems become harder and increasingly you have to recall your mathematics knowledge in combination with programming skill to get to the answer.
Once you manage to enter the correct answer to the problem you get access to the forum for that particular problem to see what everyone else's solution is. This is the most interesting part! Posted are solutions using all manner of languages such as Haskell, Lua, Pascal etc (looking at the statistics page there are 74 different languages among the users!) and the first solutions were published 9 years ago. You can really see the effort made in the beginning (at least for the first 30-40 problems that I have tackled) to optimize the code to run faster which seem to be decreasing these days with faster PCs.
Also apparent is the solutions by mathematicians where not a line of code seem to have been written before thorough boundary analysis has taken place and the problem has been thought out using standard mathematical rules that once pointed out seem obvious, but as coders we often seem to leave the the computers to take care of, giving them often more than twice the amount of work necessary.
If there ever was a website to inspire you to try a different language or see how clever people use logic and mathematics before coding a solution, this is it! Have a go but beware it is quite addictive!
Wednesday, 25 December 2013
Wednesday, 11 December 2013
How to add buttons dynamically to ASP.Net
If you don't know how many of something you need on a page, ASP.Net allows you to add controls dynamically. These dynamically loaded controls are unfortunately forgotten each time the page is posted back and needs to be reloaded at every postback as well as the first time the page is requested. To be able to access their values through the viewstate you are best adding them as early as possible (Page_Init) and populate them and customize them in the Page_Load event. To read an excellent article about this please visit the 4guysfromrolla website which goes into this in great detail.
However, what if it isn't just a value you want to access but instead want to access an event from a dynamically added control such as a button? Events are only raised after the Page_Load has taken place and then your controls have already been regenerated.
The way to solve this as I have found (if there is a better way let me know!) is to specifically reload the section that should be updated by the button click after first having cleared it of the controls that the postback generated in the Page_Init stage if there are controls already there that you wish to update. Failure to clear the placeholder means that they would be added twice (one with the old values as the button event hasn't been raised yet and once with the new values) which isn't allowed unless your IDs are different. Please see example below which tries to demonstrate this.
However, what if it isn't just a value you want to access but instead want to access an event from a dynamically added control such as a button? Events are only raised after the Page_Load has taken place and then your controls have already been regenerated.
The way to solve this as I have found (if there is a better way let me know!) is to specifically reload the section that should be updated by the button click after first having cleared it of the controls that the postback generated in the Page_Init stage if there are controls already there that you wish to update. Failure to clear the placeholder means that they would be added twice (one with the old values as the button event hasn't been raised yet and once with the new values) which isn't allowed unless your IDs are different. Please see example below which tries to demonstrate this.
Monday, 9 December 2013
Being bad at maths does not automatically make you creative!
![]() |
| Created by mathematicians and programmers in an easy format for arty people to appreciate. |
When people say this, it is like the only signs of creativity is what is immediately in front of them, a pretty picture, a lovely piece of music etc. What unfortunately they have failed to acknowledge is the amazing creativity that goes into many technical and scientific projects that would require them first to learn the technical concepts and backgrounds to be able to appreciate. It is a lot easier to label it as non-creative and keep looking at a painting instead.
In no other technical field is creativity so visible and tangible as in computing. Designs are forever evolving and ways of solving problems can be truly beautiful. The restrictions are fewer than in most fields and that leaves the programmer to truly fulfill his creative potential. Maybe not with a huge audience but still, that doesn't matter as the best bit about programming is the feeling you have created something! And you can always feel a little sorry for the self proclaimed "arty" people who might never be able to appreciate a beautiful piece of code...
Saturday, 7 December 2013
So what have I learnt....
| Cats and computing in perfect harmony! |
Trying to explain and present something makes you think extra hard about what you do and gives you a much better understanding for how things work. I often find at work, that once you have made something work you are inevitably focusing on the next stage, not feeling that there is enough time to go back and dive deeper into a specific feature. However when you are trying to explain it to someone else you have to at least try and figure out how and why you did certain things and inevitably you find you understand things better afterwards. And who knows someone might even suggest an even better way of doing it.
When I now encounter problems at work that either I can't figure out straight away or even after searching the web is not particularly obvious, I think whether it might be worth writing another post and straight away I feel more motivated to try and understand what is happening rather than just randomly trying things until it magically works.
So even if not many people will read my posts (and hey ho if I'm the only person reading them to remind me next time I encounter the same issue) then the worst thing that can happen is that more server space gets filled up with more data. And honestly lets face it, it would be filled up with pictures of cats anyway!
Friday, 6 December 2013
Customize headers for the ASP.Net Gridview control dynamically
I almost exclusively bind the data for my gridview in the code behind as often the same gridview is used for displaying different tables. It also gives the user more control to make minor changes in the database tables without having to redesign the UI every time. So a lot of things happen in the gridview's onRowDatabound event which is the event that gets triggered when the data is being bound to the elements of the gridview. For instance this is where you can change the background colour of cells depending on their value and add formatting to the text in the cells etc but as I'm going to show you below you can also add and format the header rows dynamically depending on the data.
In this particular gridview I am adding three extra rows as headers as well as formatting the data rows depending on if they are in edit mode or not
The method that created as add the new header rows this is shown below:
If you are just displaying data you can finish here. However one thing to bear in mind is that if you are calling the commands on individual rows such as edit/update etc you are going to have a slight problem as there is a mismatch of the index sent to the code behind (which has three extra rows!) compared to the actual index of the row you want to update as the page doesn't know about the extra rows you have added dynamically when you are of reloading the page. So in the RowEditing event you simply subtract the extra rows from the e.NewEditIndex. This seems to work a little different depending if you have autogenerated the edit/update buttons or if you have added them in a template field. For this particular case I have added them in a template field.
In this particular gridview I am adding three extra rows as headers as well as formatting the data rows depending on if they are in edit mode or not
The method that created as add the new header rows this is shown below:
If you are just displaying data you can finish here. However one thing to bear in mind is that if you are calling the commands on individual rows such as edit/update etc you are going to have a slight problem as there is a mismatch of the index sent to the code behind (which has three extra rows!) compared to the actual index of the row you want to update as the page doesn't know about the extra rows you have added dynamically when you are of reloading the page. So in the RowEditing event you simply subtract the extra rows from the e.NewEditIndex. This seems to work a little different depending if you have autogenerated the edit/update buttons or if you have added them in a template field. For this particular case I have added them in a template field.
Subscribe to:
Posts (Atom)
