Monthly Archives: January 2012

ASP.NET: Get selected RadioButton’s value in specific GroupName

RadioButton selRB = radioButtonsContainer.Controls.OfTypeRadioButton>().FirstOrDefault(rb => rb.Checked);if(selRB != null){    int registrationTypeAmount = Convert.ToInt32(selRB.ToolTip);    string cbText = selRB.Text;}

where radioButtonsContainer is the container of the radiobuttons.

Update

If you want to ensure you get RadioButtons with the same group, you have 2 options:

  • Get them in separate containers
  • Add the group filter to the lamdba expression, so it looks like this:

    rb => rb.Checked && rb.GroupName == "YourGroup"

Update 2

Modified the code to make it a little more fail proof by ensuring it won’t fail if there’s no RadioButton selected.

jQuery ColorBox: Remove close button from popup

Open colorbox.css and set #cboxClose to display none. You need to use !important to override an inline style that sets it to display block.

Code:
#cboxClose{right:0; background-position:-100px -25px; display:none !important;}#cboxClose.hover{background-position:-100px 0px;}

Also, if you are using the slideshow and want to shift it over to the right edge, change its right position to zero

Code:
.cboxSlideshow_on #cboxSlideshow{background-position:-125px 0px; right:0;}.cboxSlideshow_on #cboxSlideshow.hover{background-position:-150px 0px;}.cboxSlideshow_off #cboxSlideshow{background-position:-150px -25px; right:0;}.cboxSlideshow_off #cboxSlideshow.hover{background-position:-125px 0px;}

You could go through the original source code (not that minimized mess) and remove the close button but this way is much easier.

 

Extending the asp:image control to set a maximum width

In this example, we are going to extend the control to allow a MaxWidth proprety, which will limit the size of the image to that value, if it is specified.
Create a new class called MaxWidthImage.cs in your App_Code folder and add the following code:
using System;using System.Web;using System.Web.UI.WebControls;namespace MyNameSpace{public class MaxWidthImage : Image{    private int _MaxWidth;    public int MaxWidth    {        get        {            return _MaxWidth;        }        set        {            _MaxWidth = value;        }    }    protected override void OnPreRender(EventArgs e)    {        if (string.IsNullOrEmpty(ImageUrl)) return;        using (System.Drawing.Image MyImage =            System.Drawing.Image.FromFile            (HttpContext.Current.Server.MapPath(ImageUrl)))        {            int CurrentWidth = MyImage.Width;            if (MaxWidth != 0 && CurrentWidth > MaxWidth)            {                CurrentWidth = MaxWidth;            }            Attributes.Add("width", CurrentWidth.ToString());        }    }}}

ASP.NET: Modify WebControl on Render

protected override void Render(HtmlTextWriter writer){    if (this.StopProcessing)    {        base.Render(writer);    }    else    {        System.Text.StringBuilder sb = new System.Text.StringBuilder();        Html32TextWriter mwriter = new Html32TextWriter(new System.IO.StringWriter(sb));        base.Render(mwriter);        string content = sb.ToString();        content = ParseLinks(content);        writer.Write(content);    }}

File Content and Directory Search using Directory.GetFiles and PLINQ

Array of File Names

Starting .NET 4, you can use PLINQ queries to parallelize operations on file directories. The following code snippet shows how you can write a query by using the GetFiles method to populate an array of file names in a directory and all subdirectories. This method does not return until the entire array is populated, and therefore it can introduce latency at the beginning of the operation. However, after the array is populated, PLINQ can be used to search inside all the files with the specific extension located in a particular directory for a specific word very quickly. For measuring the performance, you can create a folder called CLOBS and create 8 large text files (1GB each).