Shukhrat Nekbaev

Life.Compile(this).Run();

  • Weird behavior when debugging ASP.NET Web application: cookie expires (1/1/0001 12:00AM) by itself on next breakpoint hit.

    I'm working on ajaxified (Telerik AJAX Manager) ASP.NET application using Visual Studio 2010 (runs with admin privileges) and IIS 7.5. Basically, everything on the page is inside update panels. As for cookies I have custom encrypted "settings" cookie which is added to Response if it's not there on session start. Application runs smoothly, problem was arising when I started the debugging it:

    Actions: 

    no breakpoints set, F5 - application has started in debug mode, browser window loaded. I login to site, click on controls, all is fine. Next I set *any* breakpoint somewhere in code, break on it then let it continue running, but once I break again (immediately after first break) and check cookie: it has expired date 1/1/0001 12:00AM and no data in value property. I was storing current language there, which was used inside Page's InitializeCulture event and obviously exception was being raised. I spent several hours trying deleting browser cache, temporary ASP.NET files etc, nothing seemed to work.

    Same application has been tested on exactly same environment on another PC and no problems with debugging there.

    After all I've found the solution: visual studio generates for every solution additional .suo file where additional settings are stored, like UI state, breakpoints info, etc, so I deleted it and loaded project again, tried debugging - everything is ok now.

    Read more...

  • Flattening System.Web.UI ControlCollection

    Hi,

     

    Sometimes one may need to get a list of child controls inside specific container and don't care about the underlying hierarchy.

     

    The result is beautifully achieved using this extension method:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

     

     public static class ControlCollectionExtensionMethods
        {
            public static IEnumerable<Control> FlattenedList(this ControlCollection controls)
            {
                foreach (Control ctrl in controls)
                {
                      // return parent control
                       yield return ctrl;
               

                      // and dive into child collection
                       foreach (Control child in ctrl.Controls.FlattenedList())
                             yield return child;
                }
            }
        }

     

    P.S.: don't forget about namespaces when using it in your code, if above class is wrapped into namespace, for example: Sample, the source code file with calling code must explicitly reference it: using Sample;

    Read more...

  • Customizing the processing of ListItems for asp:RadioButtonList with "Flow" layout and "Horizontal" direction

    Hi,

    recently I was asked to add an ability to pad specific elements from each other to a certain distance in RadioButtonList control. Not quite common everyday task I would say :)

     

    Ok, let's get started!

     

    Prerequisites:

    ASP.NET Page having RadioButtonList control with RepeatLayout="Flow" RepeatDirection="Horizontal" properties set.

     

    Implementation: 

    The underlying data was coming from another source, so the only fast way to add meta information about padding was the text value itself (yes, not very optimal solution):

    Id = 1, Name = "This is first element" and for padding we agreed to use <space/> meta tag: Id = 2, Name = "<space padcount="30px"/>This is second padded element"

     

    To handle items rendering in RadioButtonList control I've created custom class and subclassed from it:

     

     public class CustomRadioButtonList : RadioButtonList
        {
            private Action<ListItem, HtmlTextWriter> _preProcess;

            protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
            {
                if (_preProcess != null)
                {
                    _preProcess(this.Items[repeatIndex], writer);
                }

                base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
            }

            public void SetPrePrenderItemFunction(Action<ListItem, HtmlTextWriter> func)
            {
                _preProcess = func;
            }
        }

     

    It is pretty straightforward approach, the key is to override RenderItem method. Class has SetPrePrenderItemFunction method which is used to pass custom processing function that takes 2 parameters: ListItem and HtmlTextWriter objects.

     

    Now update existing RadioButtonList control in Default.aspx:

    add this to beginning of the page:

     

    <%@ Register Namespace="Sample.Controls" TagPrefix="uc1" %>

     

    and update the control to:

     

    <uc1:CustomRadioButtonList ID="customRbl" runat="server" DataValueField="Id" DataTextField="Name"
                RepeatLayout="Flow" RepeatDirection="Horizontal">
    </uc1:CustomRadioButtonList>

     

    Now, from codebehind of the page:

     

    Add regular expression that will be used for parsing:

     

    private Regex _regex = new Regex(@"(?:[<]space padcount\s*?=\s*?(?:'|"")(?<padcount>\d+)(?:(?:\s+)?px)?(?:'|"")\s*?/>)(?<content>.*)?", RegexOptions.IgnoreCase | RegexOptions.Compiled);

     

    and finally setup the processing function in Page_Load:

     

    protected void Page_Load(object sender, EventArgs e)
        {
            customRbl.DataSource = DataObjects;

            customRbl.SetPrePrenderItemFunction((listItem, writer) =>
            {
                Match match = _regex.Match(listItem.Text);
                if (match.Success)
                {
                    writer.Write(string.Format(@"<span style=""padding-left:{0}"">Extreme values: </span>", match.Groups["padcount"].Value + "px"));

                    // if you need to pad listitem use code below
                    //x.Attributes.CssStyle.Add("padding-left", match.Groups["padcount"].Value + "px");

                    // remove meta tag from text
                    listItem.Text = match.Groups["content"].Value;
                }
            });

            customRbl.DataBind();
        }

     

    That's it! :)

     

    Run the attached sample application:

     

     

    P.S.: of course several other approaches could have been used for that purpose including events and the functionality for processing could also be embedded inside control itself.

    Current solution suits slightly better due some other reasons for situation where it was used, in your case consider this as a kick start for your own implementation :)

     

    Source application: CustomRadioButtonList.zip

    Read more...

  • Visual Studio 2008 and .NET Framework 3.5 Training Kit

    Visual Studio 2008 and .NET Framework 3.5 Training Kit containing Labs, Demos and PPTs that was developed by the Developer and Platform Evangelism Group is available for download here:

    Brief Overview

    The Visual Studio 2008 and .NET Framework 3.5 Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio 2008 features and a variety of framework technologies including: LINQ, C# 3.0, Visual Basic 9, WCF, WF, WPF, ASP.NET AJAX, VSTO, CardSpace, SilverLight, Mobile and Application Lifecycle Management.
     
    Resources:

    Read more...

  • Welcome onboard

    Thank you for visiting my blog!

    One day, late night I've been browsing around the web, searching for some interesting articles to read and again that old idea came up - start running own blog, yeah, sounds great but lot's of thing to do, lack of time, study, etc and as always I've almost suppressed that idea, but... it came up again and again. Hm, supposed to be like somekind of a sign I thought, ok, I decided to start running a blog on one of my fav sites and with a help of Joe Stagner I'm here today with you.

    So, enjoy your stay and let's get started ;-)

     

    Read more...