How to do print functionality in Silverlight ?



Hi

Silverlight4.0 support Printing Api, using this api we can do the print functionality in silverlight 4.0 like this
Step1: Create the XAML page design like this

<StackPanel>
<Image x:Name="imgOne" Source="Img/Winter.jpg"
Height="300" Width="500"/>

<Button x:Name="Print" Content="Print" Width="150" Height="40"
Click="Print_Click" />
</StackPanel>

Step2:Write the code on Click Event in Code behind page like this
and make ensure to use “System.Windows.Printing” namespace.

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.Printing;

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

private void Print_Click(object sender, RoutedEventArgs e)
{
PrintDocument pdoc = new PrintDocument();
pdoc.PrintPage += (p, args) =>
{
args.PageVisual = imgOne;
args.HasMorePages = false;

};

pdoc.EndPrint += (p, args) =>
{
MessageBox.Show("Printing operation completed");
};

pdoc.Print("Some Document");

}
}
}

How to use delegate in asp.net ?


Delegate is similar to “C” language function pointer, where functions can be assigned like a variable and called at the run time based on dynamic conditions
Or
Delegate is the type that defines a method signature. To invoke a delegate object, one or more methods are required with the EXACT same signature. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.

Uses of Delegate:
1. If we have to fire or trigger some action in our project on some particular situation like click event, then we can use delegate
2. If we have to access the main page or other usercontrol from usercontrol then we can use delegate in asp.net.

General Syntax

public delegate int Calculate(int x, int y);

Simple Example of delegate Concept with asp.net

Step1:Design the default page like above Img

Step2: Create one class i.e Class1 like this

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

/// <summary>
/// Summary description for Class1
/// </summary>
///

public delegate int Calculate(int x, int y);
public class Class1
{
public int Add(int x, int y)
{
return x + y;
}
}

Step3:Write the code on Click Event like this

protected void BtnSubmit_Click(object sender, EventArgs e)
{

Class1 mc = new Class1();
Calculate opAdd = new Calculate(mc.Add);
int res = opAdd(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
lblResult.Text = res.ToString();

}

Delegate concept with UserControl

Step1: Create one UserControl and Keep one asp.net Button Control like this

<asp:Button ID="btnTest" runat="server" Text="Click Here" OnClick="btnTest_Click" />

Step2:Write the code in Codebehind 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 WebUserControl : System.Web.UI.UserControl
{

// Delegate declaration
public delegate void OnButtonClick(string strValue);

// Event declaration
public event OnButtonClick btnHandler;

// Page load
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnTest_Click(object sender, EventArgs e)
{
// Check if event is null
if (btnHandler != null)
{
btnHandler(string.Empty);
}

}
}

Step3:Keep on dropdown for countrylist and label for displaying selected country and also keep the usercontrol which contain the Button like this code

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem>Nepal</asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>Singapur</asp:ListItem>
</asp:DropDownList>

<br />
<br />
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<br />
<br />

Selected Country is: <asp:Label ID="lblText" Text=""
runat="server"/>

</div>
</form>
</body>
</html>

Step4:Write the code 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 Private : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

WebUserControl1.btnHandler+=new WebUserControl.OnButtonClick(WebUserControl1_btnHandler);

}

void WebUserControl1_btnHandler(string strValue)
{
lblText.Text = ddlCountry.SelectedItem.Text;
}
}

Step5: Now click on button which is in usercontrol,then the selected country will display on label like this

How to create modal popup in silverlight ?


Hi
In Silverlight 2.0, creating modal popup was some tedious work. But in Silverlight 3.0,4.0,5.0, we can create very easily like this.

Step1: Create the new silverlight project

step2:Right clik on Solution Exp of silverlight project and add one Silverlight Child Window like this Img

Step3: Keep one Img in ChildWindow.xaml page like this

<controls:ChildWindow x:Class=”ChildWindow.ChildWindow2″

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

xmlns:controls=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls”

Width=”400″ Height=”300″

Title=”ChildWindow2″>

<Grid x:Name=”LayoutRoot” Margin=”2″>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height=”Auto” />

</Grid.RowDefinitions>

<StackPanel>

<Image x:Name=”img” />

<TextBlock x:Name=”caption” TextWrapping=”Wrap” />

</StackPanel>

<Button x:Name=”CancelButton” Content=”Cancel” Click=”CancelButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,0,0″ Grid.Row=”1″ />

<Button x:Name=”OKButton” Content=”OK” Click=”OKButton_Click” Width=”75″ Height=”23″ HorizontalAlignment=”Right” Margin=”0,12,79,0″ Grid.Row=”1″ />

</Grid>

</controls:ChildWindow>

Step4:Create one button in main page like this

<Grid>

<Button x:Name=”Button1″ Content=”Click Here” Click=”Button1_Click” Height=”28″ Width=”180″></Button>

</Grid>

Step5: Create the code in code behind page mainpage.xaml.cs 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;

using System.Windows.Media.Imaging;

namespace ChildWindow

{

public partial class Test1 : Page

{

public Test1()

{

InitializeComponent();

}

private void Button1_Click(object sender, RoutedEventArgs e)

{

ChildWindow2 win = new ChildWindow2();

win.img.Source = new BitmapImage(new Uri(“Img/Winter.jpg”, UriKind.Relative));

win.Title = “Silverlight popup”;

win.Show();

}

}

}

How to do bookmark functionality in asp.net ?



Hi
While working on asp.net project, we will get requirement to bookmark functionality, we can do like this

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">

function addToBookMarks() {
bookmarkurl = "http://www.chandradev819.wordpress.com&quot;;
bookmarktitle = "Chandradev Blog";
if (document.all)

window.external.AddFavorite(bookmarkurl, bookmarktitle);
else if (window.sidebar) window.sidebar.addPanel(bookmarktitle, bookmarkurl, ”);

}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click here" OnClientClick="addToBookMarks()" />
</div>
</form>
</body>
</html>