Friday, May 10, 2013

How to Register Controls in Web.config

In web.config, we can register User controls,custom controls and Third party controls.
just like below


<pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>


Above lines are responsible to register asp.net controls in web.config,so that we are using those controls in all pages across the application without registering them in each individual Page.

if we developed a custom control MyControl

we have to register just like below

<pages>
  <controls>
   <add tagPrefix="Me" namespace="Me.Sample" assembly="Myassembly">
  </controls>
</pages>



Thursday, May 9, 2013

TryParse() in C#

in c#,we often convert data from one type to another.
Sometimes conversions may fail,to manage we will put conversion code block in try catch.
we can get rid off this by using TryParse().

Advantage:
No need to handle exception handling in conversions.

see below example


int quantity=0;
if (int.TryParse(txtQuantity.Text, out quantity) == false)
{
 quantity = 0;
}