Saturday 18 February 2012

Printing GridView from ASp.net




<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  private DataTable GetDataTable()
  {
      //create table
      DataTable dt = new DataTable("Product");
      dt.Columns.Add("ProductID", Type.GetType("System.Int32"));
      dt.Columns.Add("ProductName", Type.GetType("System.String"));

      //create fields
      DataColumn[] pk = new DataColumn[1];
      pk[0] = dt.Columns["ProductID"];
      dt.PrimaryKey = pk;
      dt.Columns["ProductID"].AutoIncrement = true;
      dt.Columns["ProductID"].AutoIncrementSeed = 1;
      dt.Columns["ProductID"].ReadOnly = true;

      //fill rows
      DataRow dr;
      for (int x = 1; x <= 10; x++)
      {
          //make every other one different
          if (Math.IEEERemainder(x, 2) == 0)
          {
              dr = dt.NewRow();
              dr["ProductName"] = "Riss";

              dt.Rows.Add(dr);
          }
          else
          {
              dr = dt.NewRow();
              dr["ProductName"] = "Product";

              dt.Rows.Add(dr);

          }
      }

      return dt;
  }

  protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {

          GridView1.DataSource = GetDataTable();
          GridView1.DataBind();
      }
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>

  <script type="text/javascript">
  function CallPrint(strid)
  {
      var prtContent = document.getElementById(strid);
      var WinPrint = window.open('','','letf=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
      WinPrint.document.write(prtContent.innerHTML);
      WinPrint.document.close();
      WinPrint.focus();
      WinPrint.print();
      WinPrint.close();

}
  </script>

</head>
<body>
  <form id="form1" runat="server">
      <div id="divPrint">
          <asp:GridView ID="GridView1" runat="server" />
      </div>
      <input type="button" value="print " id="btnPrint" runat="Server" onclick="javascript:CallPrint('divPrint')" />
  </form>
</body>
</html>



use html (<input type="button">)  button instead of <asp:button>>. 

Friday 17 February 2012

ASP.NET Open PopUp Window Update Refresh Values


In this example i am going to describe how to open popup window from aspx page with values from parent page, and update or refresh values in parent window from child or popup window using javascript and ClientScript.RegisterStartupScript method in ASP.NET

I have added to labels in Default.aspx page and one button to open popup window.

I've also added a PopUp.aspx page which is having two textboxes and a button to update lable values of parent page.


The textboxes in popup window are populated with Text values of lables in parent page (Default.aspx), after making changes in textbox values i'm updating values back in parent page.

















HTML source of Default.aspx (parent) page is
<form id="form1" runat="server">
<div>
First Name :
<asp:Label ID="lblFirstName" runat="server" Text="amiT">
</asp:Label><br />
 <br />
Last Name:&nbsp;
<asp:Label ID="lblLastName" runat="server" Text="jaiN">
</asp:Label><br />
<br />
<asp:Button ID="btnPop" runat="server" Text="Click To Edit Values" />
</div>
</form>
Write this Javascript in Head section of Default.aspx page
In this i m getting values of lables and passing them to popuup page as querystrings
write this code Page_Load event of Default.aspx (parent) page
C# code behind
01protected void Page_Load(object sender, EventArgs e)
02{
03 string updateValuesScript =
04@"function updateValues(popupValues)
05{
06 document.getElementById('lblFirstName').innerHTML=popupValues[0];
07 document.getElementById('lblLastName').innerHTML=popupValues[1];
08}";
09 
10this.ClientScript.RegisterStartupScript(Page.GetType(),
11"UpdateValues", updateValuesScript.ToString(), true);
12btnPop.Attributes.Add("onclick", "openPopUp('PopUp.aspx')");
13}

VB.NET code behind
1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
2    Dim updateValuesScript As String = "function updateValues(popupValues)" & vbCr & vbLf & "{" & vbCr & vbLf & " document.getElementById('lblFirstName').innerHTML=popupValues[0];" & vbCr & vbLf & " document.getElementById('lblLastName').innerHTML=popupValues[1];" & vbCr & vbLf & "}"
3 
4    Me.ClientScript.RegisterStartupScript(Page.[GetType](), "UpdateValues", updateValuesScript.ToString(), True)
5    btnPop.Attributes.Add("onclick", "openPopUp('PopUp.aspx')")
6End Sub

And this is the HTML code for PopUp.aspx(child) page
<form id="form1" runat="server">
<div>
First Name :
<asp:TextBox ID="txtPopFName" runat="server" Width="113px">
</asp:TextBox><br />
<br />
Last Name:<asp:TextBox ID="txtPopLName" runat="server" 
                       Width="109px">
</asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
            Text="Button" /></div>
</form>
Code behind for PopUp.aspx
C# code behind
01protected void Page_Load(object sender, EventArgs e)
02{
03 string updateParentScript =
04 @"function updateParentWindow()
05 {                                                                              
06   var fName=document.getElementById('txtPopFName').value;    
07   var lName=document.getElementById('txtPopLName').value;  
08   var arrayValues= new Array(fName,lName);
09   window.opener.updateValues(arrayValues);      
10   window.close();
11 }";
12 this.ClientScript.RegisterStartupScript(this.GetType(),
13     "UpdateParentWindow", updateParentScript, true);
14 if (!IsPostBack)
15 {
16   txtPopFName.Text = Request["fn"];
17   txtPopLName.Text = Request["ln"];
18 }
19   Button1.Attributes.Add("onclick", "updateParentWindow()");
20}
VB.NET code behind
1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
2    Dim updateParentScript As String = "function updateParentWindow()" & vbCr & vbLf & " {                                                                               " & vbCr & vbLf & "   var fName=document.getElementById('txtPopFName').value;     " & vbCr & vbLf & "   var lName=document.getElementById('txtPopLName').value;   " & vbCr & vbLf & "   var arrayValues= new Array(fName,lName);" & vbCr & vbLf & "   window.opener.updateValues(arrayValues);       " & vbCr & vbLf & "   window.close(); " & vbCr & vbLf & " }"
3    Me.ClientScript.RegisterStartupScript(Me.[GetType](), "UpdateParentWindow", updateParentScript, True)
4    If Not IsPostBack Then
5        txtPopFName.Text = Request("fn")
6        txtPopLName.Text = Request("ln")
7    End If
8    Button1.Attributes.Add("onclick", "updateParentWindow()")
9End Sub

Thursday 16 February 2012

What is difference between Web site and Web application ?


Both function and perform similarly, but still differ in following ways.

Web application:


a) We can't include c# and vb page in single web application.

b) We can set up dependencies between multiple projects.
c) Can not edit individual files after deployment without recompiling.
d) Right choice for enterprise environments where multiple developers work unitedly for creating,testing and deployment.


Web site:


a) Can mix vb and c# page in single website.

b) Can not establish dependencies.
c) Edit individual files after deployment.
d) Right choice when one developer will responsible for creating and managing entire website.

Saturday 11 February 2012

How to create Image Gallery in asp.net 3.5


Hi
When I was creating Image gallery in asp.net 2.0 using Datalist control, it was taking so much time. But in asp.net 3.5 and 4.0, Microsoft added the ListView control. Using this control we can create within few minute very nice image galleries.
It is the totally template based control like repeater control. It has 11 templates to customize.
Using listview and datapager control we can create very nice image gallery with paging functionality.
Here is no need to write the custom coding for paging functionality.
We can do like this
Step1: Create the database table like this

Step2: take the listview and Datapager control and arrange the HTML code 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.main{
float:left;
padding:0px;
margin:0px;
width:100%;
}
.imges{
float:left;
padding:0px;
margin:0px;
width:100%;
}
.numbers{
float:left;
margin:0px;
padding:0px;
margin-left:60px;
}
.ProductList Li
{
display :inline;
float:left;
margin-left:25px;
margin-bottom:25px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="main">
<div class="imges">
<h3>Students</h3>
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<ul class="ProductList">
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><asp:Image ID="Img1" ImageUrl=’<%#Eval("Image")%>’ runat="server"
Height="200px" Width="150px" /><br /><%#Eval("StudentName")%></li>
</ItemTemplate>
<EmptyItemTemplate>
<div>
Sorry! No Item found found.
</div>
</EmptyItemTemplate>
</asp:ListView>
</div>
<div class="numbers">
<asp:DataPager ID="DataPager1" PageSize="6" PagedControlID="ListView1"
runat="server" onprerender="DataPager1_PreRender">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</div>
</div>
</form>
</body>
</html>
Step3:Write the code In code behind like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
fillData();
}
protected void fillData()
{
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
{
//for better performance write the store procedure
using (SqlCommand cmd = new SqlCommand("Select StudentName,Image from tblStudent", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ListView1.DataSource = dt;
ListView1.DataBind();
}
}
}
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
fillData();
}
}

Total Pageviews