Skip to main content

Posts

How to merge cells with equal values in the GridView

My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the code. The algorithm is simple: to bypass all the rows, starting from the second at the bottom, to the top. If a cell value is the same as a value in the previous (lower) row, then increase RowSpan and make the lower cell invisible, and so forth. The code that merges the cells is very short: public class GridDecorator { public static void MergeRows(GridView gridView) { for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) { GridViewRow row = gridView.Rows[rowIndex]; GridViewRow previousRow = gridView.Rows[rowIndex + 1]; for (int i = 0; i < row.Cells.Count; i++) { if (row.Cells[i].Text == previousRow.Cells[i].Text) { row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : ...

Charts: let Google work for you

I can't class myself as an developer who is always hip to the latest news in the software industry, partly through laziness and partly through necessity to write code, and not merely to surf the Internet :) That is why I missed appearance of a very cute and handy service from Google , which allowed to integrate charts into the web pages. For the time being it is only half a year old as far as I can judge by the date of the first record in the discussion group, therefore I think this information can be interesting for someone else besides me. The service allows to dynamically generate practically all essential types of charts such as bar charts, pie charts and so on. The only thing you have to do to get the chart is forming the url in concordance with some rules and the service will return the PNG-format image. As an example I decide to use data from another Google service, namely Google Analytics, which together with all its merits has one lack: the absence of public access to the ...

How to use standard FileUpload in AJAX-enabled web applications

I would like to note that this article is not about the ability to upload files to the server without the postback. There are a lot of articles on this topic, just type "AJAX FileUpload" on any search engine and you'll get many examples. However with AJAX they actually have little in common, because the XMLHttpRequest does not support asynchronous uploading of files to the server, they are rather a variety of imitations, for example, using hidden IFRAME element. Nevertheless I want to emphasize that the article is not about that but about the standard FileUpload control. There are two problems you might encounter when using it on UpdatePanel. Problem 1 If the postback is caused by the control which lies on UpdatePanel, FileUpload is always empty when it has come to the server, regardless whether a file has been selected or not. Example: <asp:UpdatePanel ID="UpdatePanel1" runat=server> <ContentTemplate> <asp:FileUpload ID="FileUpload1...

Simultaneous Selection of Checkboxes in a GridView's column

If you are using checkboxes in GridView in order to select a few rows, then, no doubt, you have been faced with the task how to select/deselect all checkboxes in a selected column at a time. Usually it is solved by addition of special buttons somewhere or addition a checkbox in the GridView's column header. I want to dwell on the second variant. In order to refrain from repetition of code all of required functionality can be hidden inside of a special control. This control is inherited from the CheckBox and have only one method overriden (OnLoad). Inside the method a script is registered, this script allows to select/deselect all checkboxes in a GridView's column if a checkbox in the GridView's header is clicked. Besides, the script tracks situation when all checkboxes in rows are selected one by one and selects the checkbox in the column's header. public class GridViewCheckBox : CheckBox { protected override void OnLoad(EventArgs e) { GridViewRow parentRow = th...

Dynamic Rounded Panel

This is another variant of the rounded panel creation (the first one you can see here ), that uses images to present curved corners. To be more precise, the only image is used, the one that is build based on the specified circle radius, the background color, the border color and width. The panel appearance varies according to all these parameters and can be dynamically changed in runtime. Picture 1. The structure of the top part of the rounded panel a - the left part where the upper left part of the image is shown. b - the middle that is filled with the background color and contains a block element (d). The d element imitates the image border. c - the right part where the upper right part of the image is shown. Of course, the middle can be simpler if to use the only block that is filled with the background color and has the upper border. But different browsers interpret borders ambiguously, some of them enlarge total height of the block element. It caused a small complication of th...

How to avoid unexpected postback after pressing Enter in textbox

Problem You fill a form with many input fields. After filling first textbox you mechanically press Enter and see that the page are submitted to the server. Inasmuch as the rest of the fields stay blank the result of this submit is either saving incomplete data or a few validators of type "Field XXX is required." are shown. The well-known situation, is not it? It is not very good to allow such behaviour in a web application, especially because it easily can be corrected. Of course, there are situations when this is useful (e.g.: you have a single search box and after typing a search keyword it is very convenient to start search by pressing Enter). But in the most cases such behaviour just annoys visitors. Solution Surprisingly, but this inconvenience caused by built-in browser conveniences. :) Convenience #1. If a form contains only textbox then regardless of the submit button presence pressing Enter will send the form to the server. http://msdn2.microsoft.com/en-us/library/...

How to Register Stylesheet Created in Runtime

Here is an example from MSDN : // Create a Style object for the section of the Web page. Style bodyStyle = new Style(); bodyStyle.ForeColor = System.Drawing.Color.Blue; bodyStyle.BackColor = System.Drawing.Color.LightGray; // Add the style to the header of the current page. Page.Header.StyleSheet.CreateStyleRule(bodyStyle, this, "BODY"); It is the simple and handy method but has a few flaws. 1. Output is far from optimal. For example, following code that adds a border someStyle.BorderColor = System.Drawing.Color.Red; someStyle.BorderStyle = BorderStyle.Solid; someStyle.BorderWidth = new Unit("1px"); produces this output border-color:Red;border-width:1px;border-style:Solid; instead of border:Solid 1px Red; It has no matter if you don't pay attention to the size of pages you develop, but it is taken into consideration if you take into consideration your visitor's needs (sorry for tautology). But it's a trifle, of course. I mentioned it just for completene...