Thursday, August 19, 2010

Partial Class in C#

Partial class:


It is possible to split the definition of a class over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

Using the partial keyword indicates that other parts of the class, struct, or interface can be defined within the namespace. All the parts must use the partial keyword. All of the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

Eg;

using System;
partial class Test
{
private int x;
}


partial class Test
{
public Test()
{
x = 10;
}
}


partial class Test
{
public void Print()
{
Console.WriteLine("X is : {0}", x);
}
}

class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Print();
}
}

Using a LinearGradientBrush in Silverlight

Using a LinearGradientBrush


LinearGradientBrush elements are similar to the SolidColorBrush. They define a linear gradient that flows between two or more colors. You can apply them to the properties of Silverlight controls the same way that the SolidColorBrush is applied.

To apply a LinearGradientBrush to a Silverlight control, you need to define the Linear- GradientBrush element and give it an EndPoint and a StartPoint that define the linear direction of the brush. The EndPoint and StartPoint are set on a 0 to 1 coordinate system separated by a comma.

Then you need to define two or more GradientStop elements inside the LinearGradient- Brush. Each GradientStop element needs the Color property set. You can also define the offset between 0 and 1 where that color is set using the Offset property. Silverlight applies the gradient linearly along the path defined by the StartPoint and EndPoint values matching the colors at each GradientStop along the way.

Eg:

< Rectangle Width="200" Height="200" Stroke="Black" >
< Rectangle.Fill >
< LinearGradientBrush StartPoint="0.0 0.0" EndPoint="0 1" >
< GradientStop Color="Red" Offset=""/ >
< GradientStop Color="Green" Offset="0.33"/ >
< GradientStop Color="Blue" Offset="0.67"/ >
< /LinearGradientBrush >
< /Rectangle.Fill >
< /Rectangle >

Eg:

< TextBlock Height="100"
TextAlignment="Center"
FontSize="30" >
< Run Text="Linear Gradient brush" >
< Run.Foreground >
< LinearGradientBrush >
< GradientStop Color="Blue" Offset=".3"/ >
< GradientStop Color="Red" Offset=".5"/ >
< GradientStop Color="Green" Offset=".8"/ >
< /LinearGradientBrush >
< /Run.Foreground >
< / Run >
< /TextBlock >

Adding a SolidColorBrush in Silverlight

Adding a SolidColorBrush


The SolidColorBrush is the most common type that you can apply to Silverlight objects. A SolidColorBrush is automatically created for you when you set the color in one of the properties of a Silverlight object. For example, the following code automatically creates a SolidColorBrush implicitly for the Background property of a Canvas control:

< Canvas Background="Lavender"
Height="200"
Width="200" >
< / Canvas >

The previous code could also be written explicitly as follows:

< Canvas Height="200"
Width="200" >
< Canvas.Background >
< SolidColorBrush Color="Lavender"/ >
< /Canvas.Background >
< /Canvas >

Adding the SolidColorBrush explicitly takes considerably more code; however, you can set some additional properties of the SolidColorBrush element to enhance the look and feel of the control. For example, you can make the SolidColorBrush appear semitransparent by setting the Opacity property, as shown in the following code:

< Canvas Height="200"

Width="200" >
< Canvas.Background >
< SolidColorBrush Color="Red" Opacity=".3"/ >
< /Canvas.Background >
< /Canvas >

Understanding the Silverlight Presentation Framework

Understanding the Silverlight Presentation Framework


The main purpose of the Silverlight presentation framework is to provide you with the UI controls and functionality to implement rich Internet applications. However, the presentation framework is much more than just the UI controls; it also encompasses browser interaction, threading, and system calls.

Namespaces that are part of the Silverlight presentation framework are:

System.Windows:

Provides the general presentation and base classes for Silverlight development. For example, the Application, UIElement, EventTrigger, and FontStyles.

System.Windows.Browser:

Provides the classes used to interact with the browser. For example, the HtmlDocument, HtmlElement, HtmlPage, and ScriptObject.

System.Windows.Controls:

Provides most of the UI controls. For example, TextBlock, Image, Grid, and UserControl.

System.Windows.Documents:

Provides the classes that support basic document concepts. For example, Fontsource, LineBreak, Inline, and Run.

System.Windows.Markup:

Provides the classes that support XAML processing. For example, XamlReader, ContentPropertyAttribute, and XmlLanguage.

System.Windows.Media:

Provides the classes that enable you to apply rich functionality to your applications. For example, GradientBrush, ScaleTransform, Colors, FontFamily, and PathGeometry.

System.Windows.Media. Animation:

Provides the classes used to animate Silverlight controls. For example, Begin Storyboard, Storyboard, DoubleAnimation, SplinePointKeyFrame, Timeline, and TimeLineMarker.

System.Windows.Shapes:

Provides the classes used to implement basic XAML shapes. For example, Ellipse, Path, and Rectangle.

System.Windows.Threading:

Provides the classes used to implement threads in your Silverlight applications. For example, Dispatcher, DispatcherOperation, and DispatcherTimer.

System.ComponentModel:

Provides the classes used to implement runtime and design-time behavior of components and controls. For example, ProgressChangedEventArgs, PropertyChangedEventArgs, BackgroundWorker, AsyncCompletedEventArgs, and TypeConverter.

Implementing Data Binding in Silverlight Applications

Implementing Data Binding in Silverlight Applications


An extremely useful feature of Silverlight is the ability to bind properties of Silverlight controls to data sources. When the values of the data sources change, the property values of the controls change as well.

Data binding can be implemented as one-time, one-way, or two-way. One-way data binding only changes the value of the control based on the data source. In two-way binding, if either the control value or the source value changes, the other is updated as well.

To implement data binding in XAML code, you first need to implement a Binding statement encased in {} brackets in place of the property value. The Binding statement uses the following syntax:

{Binding < value >, < options >}

The value setting in the Binding statement is the property of the data source that is used to fill in the value for the control property. You can also specify the following options in the Binding statement:

 Converter: Specifies the converter object that is called by the binding engine to modify the data as it is passed between the data source and target property

 ConverterCulture: Specifies the culture used by the converter to implement conversion of data

 ConverterParamter: Specifies a parameter that can be passed to the converter

 Mode: Specifies whether the data flow in the binding is one-way or two-way.

 Source: Specifies the source object for the binding

The Binding statement tells the control only what property of the data source the value is bound to. You also need to set the value of the DataContext property of the control to assign a specific object as the data source.


To implement a class as a data source, the class must implement the INotifyProperty Changed interface and have an event of type PropertyChangedEventHandler named PropertyChanged.


The DataContext property is inheritable by child elements in the Silverlight application. If you have several controls that are bound to the same data source, then you can group those controls in a parent container such as a Canvas or Grid and then set the DataContext property of the parent. The data context flows down to the children without the need to set the property for each child.

Eg:
 
< TextBox Height="24" HorizontalAlignment="Left" Margin="60,72,0,0" VerticalAlignment="Top" Width="150" Text="{Binding Name, Mode=TwoWay}" TextWrapping="Wrap" x:Name="txtName" / >
 
 
write the following code
 
public class Employee : INotifyPropertyChanged

{
string name;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyChange(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}

public string Name
{
set
{
name = value;
NotifyChange("Name");
}
get
{
return name;
}
}
}


-------------

public partial class Page : UserControl

{
Employee emp;


public Page()
{
InitializeComponent();


emp = new Employee{ Name = "Jobin John" };


LayoutRoot.DataContext = emp;


}
}
}


Hope its Helpfull for silverlight programmers.......

Wednesday, August 18, 2010

Perform Editing and Deleting with GridView Control

Editing and Deleting with GridView Control
Follow the following steps
  • First you place a GridView Control on the web form.
  • Under Add/Edit Column window, add apropriate columns ( I have added BoundColumns) to display data. Set its DataField property too.
  • Set AutoGenerateEditButton and AutoGenerateDeleteButton property into true.
  • Set GridView's apropriate events.
I have used Employee table to demonstrate this example. It has EmpId, Name, JobTitile and Salary columns.

public partial class GridViewAllCode : System.Web.UI.Page

{
SqlConnection con;
SqlCommand cmd;
DataTable dt;
SqlDataAdapter adp;


protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename="+Server.MapPath("App_Data")+"\\jobin.mdf"+";Integrated Security=True;User Instance=True");
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
if (!IsPostBack)
BindGrid();
}


private void BindGrid()
{
adp = new SqlDataAdapter("select * from Employee", con);
dt = new DataTable("Emp");
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}


protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}


protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex =-1;
BindGrid();
}


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int empId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name=((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string jobTitle=((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
int salary = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
cmd.CommandText = "update Employee set Name='" + name + "', JobTitle='" + jobTitle + "', Salary=" + salary + " where EmpId=" + empId;
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGrid();
}


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int empId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
cmd.CommandText = "delete Employee where EmpId=" + empId;
cmd.ExecuteNonQuery();
BindGrid();
}
}