How to use tab control in silverlight ?


For using tab control in silverlight, we have to drag and drop into panel and configure the control like this

<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl Height="300" HorizontalAlignment="Left" Margin="10,10,0,0" Name="tabControl1" VerticalAlignment="Top" Width="500">
<sdk:TabItem Header="Asp.net">
<StackPanel Background="BlueViolet">

<TextBlock Text="This is the asp.net tab"/>

</StackPanel>
</sdk:TabItem>

<sdk:TabItem Header="Silverlight">
<StackPanel Background="Brown">

<TextBlock Text="This is the Silverlight"/>

</StackPanel>
</sdk:TabItem>
<sdk:TabItem Header="Asp.net MVC">
<StackPanel Background="Coral">

<TextBlock Text="This is the Asp.net MVC"/>

</StackPanel>
</sdk:TabItem>

</sdk:TabControl>
</Grid>

Databinding in Silverlight


Hi
Data binding is one of very important concept in silverlight.Before doing any data binding operation in silverlight, we should have to know the exact binding syntax in silverlight.

What is databinding in silverlight ?
Binding is the process of creating connection between User interface(UI) and source of data.
OR
It is the process of pulling information out of an object and displaying it in our application.

In Silverlight we can create the binding using 2 different approaches
a.Binding at runtime
b.Binding at design time.

Binding Modes in Silverlight
There are 3 binding modes
a. Onetime: if we have display some constant data then we can use this mode like database name
b. One-way:It is default mode in silverlight.Here the target property is updated when the source property change. Here will be one-way communication between source and target control.
c.TwoWay: Here will be two way communication between source and target control. If target control data will change then that will reflect in source control and vice-verse.

Simple databinding example in silverlight 4.0

Step1:Create one Emp class and declare the property like this

public class Emp
{
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }
}

Step2:Create one silverlight page and Using GridPanel create one simple form for displaying data like this

<UserControl x:Class="Binding_In_Silverlight.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid Name="GridEmp">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="7" Grid.Row="0">EmpId:</TextBlock>
<TextBox Margin="5" Grid.Column="1" Text="{Binding Id}" Grid.Row="0"/>
<TextBlock Margin="7" Grid.Row="1">EmpName:</TextBlock>
<TextBox Margin="5" Grid.Column="1" Text="{Binding EmpName}" Grid.Row="1"/>
<TextBlock Margin="7" Grid.Row="2">EmpSalary:</TextBlock>
<TextBox Margin="5" Grid.Column="1" Text="{Binding EmpSal}" Grid.Row="2"/>

</Grid>

</UserControl>

Step3: Write the binding code in code behind page like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Binding_In_Silverlight
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
FillGridPanel();
}

protected void FillGridPanel()
{
Emp emp = new Emp();
emp.Id = 122;
emp.EmpName = "Ram Mohan";
emp.EmpSal = "35000";
GridEmp.DataContext = emp;

}

}
}

Step4:Compile the code, then you will get op like this

Binding with collection of data
Here we will bind the collection of items using listbox silverlight control

Step1:Take one listbox silverlight control and configure like this

<navigation:Page x:Class="Binding_In_Silverlight.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="640" d:DesignHeight="480"
Title="Page1 Page">
<Grid x:Name="LayoutRoot">
<ListBox ItemsSource="{Binding}" Width="200" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding EmpName}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</navigation:Page>

Step2:Write the code in code behind page for binding like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace Binding_In_Silverlight
{
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();

var employees = new List<Emp>()
{
new Emp{ EmpName = "Chandradev Prasad Sah" },
new Emp { EmpName = "Murli Reddy"},
new Emp { EmpName = "Krishana" }
};
this.DataContext = employees;
}

}
}

Step3:Now compile the code then you will get op like this

Here i have used 2 types of property binding data with listbox and textbox i.e DataContext & Itemsource

What is the difference between DataContext & Itemsource property?
Datacontext is used for binding single data item on silverlight control like textbox but item source is used to display the collection of data item in Silverlight control like listbox and dataGrid

Thank you for reading this artical and feel free to send comment, if have any suggestion.

How to implement style in Silverlight ?



Hi
Style in Silverlight is similar to CSS of asp.net page. Silverlight style allows us to define a common set of formatting characteristics and apply them throughout our application to ensure the consistency in page design.

Here i have created style for button like this

<UserControl x:Class="SilverlightApplication3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
mc:Ignorable="d" Height="300" Width="400">
<UserControl.Resources>
<Style x:Key="ButtonCSS" TargetType="Button">
<Setter Property="Foreground" Value="SeaGreen"/>
<Setter Property="FontSize" Value="28"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="200"/>
<Setter Property="FontStyle" Value="Italic"/>
</Style>
</UserControl.Resources>

<Grid>
<Button Content="Submit" Style="{StaticResource ButtonCSS}"/>
</Grid>

</UserControl>

How to create asynchronous web service in asp.net ?



Hi
if we are going to consume some method of web service and it is taking some time and in mean while if we have to perform some other task then we can use asynchronous web service.

Here is the simple steps to create asynchronous web service.
Step1: Create the simple web service like this

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

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/&quot;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
System.Threading.Thread.Sleep(1000);
return "Hello World";
}

}

Step2:Now consume the webservice and call the method in code behind page like this

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

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
localhost.WebService ws = new localhost.WebService();
IAsyncResult myvar = ws.BeginHelloWorld(null, null);
int x = 0;
while (myvar.IsCompleted==false)
{
x += 1;

}
Label1.Text = "Result from webservice: " + ws.EndHelloWorld(myvar) + "<br> Local count while waiting:" + x.ToString();

}
}

Note: Here i have written to perform other task between being and End service method.

What is interface in C# ?


Interface is only the signature of methods, delegates or events. The implementation of methods in done in the class that implement the interface.
Feature of interface
1.It contains the interface Keyword.
2.By default all member of interface will be public
3.We can’t use private or sealed in interface method.
Interface Iclass
{
Private void dowork(); //It will show error.
}
4.To implement interface we have to use colon ( : ) similar to inheritance.
5.Interface also contains the property declarations. The class implement these interface must provide the implementation for these properties.

Syntax of Simple interface in asp.net

Step1: Create one interface in asp.net like this

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

/// <summary>
/// Summary description for IClass1
/// </summary>
public interface IClass1
{
string SayHello();
}

Step2:Implement the interface like this

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

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1:IClass1
{

public string SayHello()
{
string str="I m from Inferface";
return str;
}
}

Step3: Call the method in asp.net code behind like this

protected void BtnClick_Click(object sender, EventArgs e)
{
Class1 obj = new Class1();
Lblmsg.Text = obj.SayHello();
}

Then you wull get op like this

How to use multiple inheritance using interface
For multiple inheritance we have to create 3 interface, we have already created one interface, now we will create 2 more interface like this

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

/// <summary>
/// Summary description for IClass2
/// </summary>
public interface IClass2
{
string SayHello1();

}

Now we will inherit Fist and second interface in third interface like this

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

/// <summary>
/// Summary description for IClass3
/// </summary>
public interface IClass3:IClass1,IClass2
{
string SayHello3();

}

Now we implement the method in Multiple class like this

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

/// <summary>
/// Summary description for MultipleClass
/// </summary>
public class MultipleClass:IClass3
{

public string SayHello3()
{
string str1 = "I m from third interface";
return str1;
}

public string SayHello()
{
string str1 = "I m from first interface";
return str1;
}

public string SayHello1()
{
string str1 = "I m from Second interface";
return str1;
}
}

Now call the method in asp.net code behind page like this

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

public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg1.Text = obj.SayHello();
}
protected void Button2_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg2.Text = obj.SayHello1();
}
protected void Button3_Click(object sender, EventArgs e)
{
MultipleClass obj = new MultipleClass();
Lblmsg3.Text = obj.SayHello3();

}
}

Now complile code you will get op like this

What is the exact use of interface

1.Since C# does not support multiple inheritance so using interface we can do this task

2.If we have to design some big project and if we have to distribute the work into multiple developer then we can go for this approach.

3.We can use in the data hiding scenario. Suppose if we don’t want to give full access of some class then we can use interface.

4.Interfaces also allow us to program against a contract rather than a concrete implementation. That helps with loose coupling. For more detail on this read this artical
http://fatagnus.com/program-to-an-interface-not-an-implementation/