Sunday, December 05, 2010

Javascript Tip: Close Pop-up - Refresh Parent

Sometimes we need to force parent refresh on closing pop-up window which was opened from this parent window. There are many ways to make it work, however the most elegant way will be using location.href

function Refresh() 
{
    window.opener.location.href = window.opener.location.href;
    window.close();
}

Smile

ASP.NET Tip: Render Control into HTML String

How to get string representation of ASP.NET control or in other words render it into string instead of let it be rendered on the page. Here is the sample code to Render control

using System.Text;
using System.IO;
using System.Web.UI;
public string RenderControl(Control ctrl) 
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    ctrl.RenderControl(hw);
    return sb.ToString();
}

Saturday, December 04, 2010

How to Create Dynamically a Property Class from T-SQL

Some times we need to write huge classes with properties, its time consuming and boring . Here is a work around how you can do this from T-SQL.

use master
go
Declare @tableName varchar(50)
SET @tableName = 'bangaram'
SELECT case when sc.isnullable<>0 AND st.name NOT LIKE 'varchar' AND st.name NOT LIKE 'text' then  
'private System.Nullable<'+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ '> _'  +sc.name +';
 public System.Nullable<'+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ '> '+sc.name
else 
'private '+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ ' _'  +sc.name +';
 public '+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ ' '+sc.name  
  end ,
        '{
            get { return this._'+sc.name+'; }
            set { if ((this._'+sc.name+' != value)) { this._'+sc.name+' = value; } }
        }'
FROM Syscolumns sc INNER JOIN Systypes st on st.xtype=sc.xtype
WHERE sc.id in(SELECT id FROm sysobjects WHERE name=@tableName)
ORDER BY sc.id

Replace @tablename with your table name in your database table. One more thing I have give @tablename size as varchar(50) increase if your tablename is bigger. That’s all you need to do apart from little bit of formatting..Good luck guys. Have fun..Smile

Friday, November 19, 2010

How Do I Set Textbox Focus in Silverlight4.0?

Here is an example to set focus for a text box in Silverlight application.

In Page.xaml

<UserControl x:Class="TextboxFocusTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Loaded="UserControl_Loaded" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">        
        <StackPanel Width="150" VerticalAlignment="Center">            
            <TextBox x:Name="UserNameTextBox" IsTabStop="True" />    
        </StackPanel>        
    </Grid>
</UserControl>

In Page.xaml.cs

using System.Windows;
using System.Windows.Controls;
namespace TextboxFocusTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
	   System.Windows.Browser.HtmlPage.Plugin.Focus();
           UserNameTextBox.Focus();
        }
    }
}

Happy Coding..Smile

How to Escape Special Characters in XML from C#

I have written a code which escape special characters in XML while posting an XML Request. Here is the sample code sample.

private string EscapeXMLillegalCharacters (string strValue)
{
    return strValue.Replace("&", "&amp;").Replace("'", "&apos;").Replace("\"", "&quot;").Replace("<", "&lt;").Replace(">", "&gt;");
}

Hope this is useful. Happy coding Smile