Rajeeshcv.com

Sharing my knowledge

Google Chrome not rendering the checkboxes properly

[ad#ad-3] Google chrome(0.2.149.30) has got some problem in rendering the checkboxes. Today I thought about buying a new laptop, so I was searching for a laptop that will fit into my budget and requirements. And I came across a website called http://www.compareindia.com/products/laptops/ , I was using Google chrome for browsing, but when viewed that page it was like this

Chrome

 

There was no way for me  to narrow my search because I couldn't select any of options listed under "Narrow your  Search" section, but after sometime i.e after doing some scrolling and switching tabs checkboxes appeared on the left side of this list

AfterScrollingandswitchingTabs

[ad#ad-2]

Initially I thought that, it will a problem with the way that page was developed(may be some JavaScript issues), but when I opened the same page in Firefox(3.0.1) it was rendering properly, even in IE 6 is rendered that page correctly.

FF  I think this is a Chrome bug; I have reported this , follow this URL http://code.google.com/p/chromium/issues/detail?id=3151 if you have experienced same problem.


Google Syntax highlighter test

Testing Google syntax highlighter plugin...
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                DockableItemCollection items = value as DockableItemCollection;
                if (items == null)
                {
                    items = new DockableItemCollection();
                }
                DockableCollectionEditor itemEditor = new DockableCollectionEditor(items, editorService);
                editorService.ShowDialog(itemEditor);
                value = itemEditor.Items;
                context.OnComponentChanged();
            }

            return value;
        }
I hope the above sample code is highlighted and formatted properly


ASP.Net MVC - Conditional Rendering

We come across situations like rendering elements based on the conditions in Model. For example, in the view if we want to show a TextBox if some property is true. A normal way of doing this is like below

<% if (this.Model.Exists)
      {%>
      <%= Html.TextBox("Test") %>
   <% } 

This looks like old classic asp style, and when it comes to code maintenance this will be a pain. So a clean way is to use an Html helper to generate this

  <%= Html.If(this.Model.Exists, action => action.TextBox("Name")) %>

which looks cleaner than the old one. Source code for this helper method is

public static string If(this HtmlHelper htmlHelper, bool condition, Func<HtmlHelper, string> action)
{
    if (condition)
    {
        return action.Invoke(htmlHelper);
    }

    return string.Empty;
}

What about IfElse condition, we could write another helper method for that

public static string IfElse(this HtmlHelper htmlHelper, bool condition, Func<HtmlHelper, string> trueAction, Func<HtmlHelper, string> falseAction)
{
    if (condition)
    {
        return trueAction.Invoke(htmlHelper);
    }

    return falseAction.Invoke(htmlHelper);
}

Ok, now we got a conditionally rendered element, sometimes we may have to put a wrapper around this element. So I have written another Html helper method which will help you to put any html element around a particular element.

public static string HtmlTag(this HtmlHelper htmlHelper, HtmlTextWriterTag tag, object htmlAttributes, Func<HtmlHelper, string> action)
{
    var attributes = new RouteValueDictionary(htmlAttributes);

    using (var sw = new StringWriter())
    {
        using (var htmlWriter = new HtmlTextWriter(sw))
        {
            // Add attributes
            foreach (var attribute in attributes)
            {
                htmlWriter.AddAttribute(attribute.Key, attribute.Value != null ? attribute.Value.ToString() : string.Empty);
            }

            htmlWriter.RenderBeginTag(tag);
            htmlWriter.Write(action.Invoke(htmlHelper));
            htmlWriter.RenderEndTag();
        }

        return sw.ToString();
    }

    return string.Empty;
}

An overloaded version which doesn’t accept HtmlAttributes as parameter

public static string HtmlTag(this HtmlHelper htmlHelper, HtmlTextWriterTag tag, Func<HtmlHelper, string> action)
{
    return HtmlTag(htmlHelper, tag, null, action);
}

Below are some examples of using these helpers.

<%= Html.HtmlTag(HtmlTextWriterTag.Div, action => action.ActionLink("Without attributes","About") ) %>

<%= Html.HtmlTag(HtmlTextWriterTag.Div, new { name = "wrapper", @class = "styleclass" }, action => action.ActionLink("With Attributes", "About")) %>

<%= Html.HtmlTag(HtmlTextWriterTag.Div, null, action => action.ActionLink("Null attributes", "About")) %>
  
<%= Html.IfElse(this.Model.Exists, trueAction => trueAction.Encode("Sample"), falseAction => falseAction.TextBox("SampleName")) %>
  
<%--
Nesting 
<div>
   <span>
       <a href="/Home/About">About</a>
   </span>
</div> 
--%>
<%= Html.If(this.Model.Exists, 
    div => div.HtmlTag(HtmlTextWriterTag.Div,
        span => span.HtmlTag(HtmlTextWriterTag.Span,
anchor => anchor.ActionLink("About", "About"))
)) %>

Hope this will help you.