Wednesday, 4 September 2013
How to Copy Selected Row One GridView to another GridView
00:37
No comments
How to Copy Selected Row One GridView to another GridView
Source Code for Default.aspx………………………………………..
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="50%">
<tr><td colspan="2">
<asp:Label ID="Label2" runat="server"
Text="How to Copy Selected Row One
GridView to another GridView"></asp:Label>
</td></tr>
<tr><td><asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkall"
runat="server"
AutoPostBack="True"
oncheckedchanged="chkall_CheckedChanged" Text="All"
/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk1"
runat="server"
/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server"
Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname"
runat="server"
Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RollNo">
<ItemTemplate>
<asp:Label ID="lblrollno"
runat="server"
Text='<%# Eval("rollno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td><td>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
</td></tr>
<tr><td>
<asp:Button ID="btnmove"
runat="server"
onclick="btnmove_Click"
Text="GridView Copy" />
</td><td>
</td></tr>
<tr><td>
<asp:Button ID="btncutpaste"
runat="server"
onclick="btncutpaste_Click"
Text="GridView Cut Paste" />
</td><td>
</td></tr>
<tr><td>
<asp:GridView ID="GridView2"
runat="server">
</asp:GridView>
</td><td></td></tr>
</table>
</div>
</form>
</body>
</html>
Source Code for Default.aspx.cs………………………………………..
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
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs
e)
{
con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial
Catalog=Test;Integrated Security=True");
if (!IsPostBack)
{
BindGridView();
}
}
//Code for bind First GridView with Sql Server table
private void
BindGridView()
{
SqlCommand cmd = new SqlCommand("select
* from Student",con );
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
GridView1.DataSource
= ds;
GridView1.DataBind();
}
//Code for bind Second
GridView with sql table when
private void
bindgridview1()
{
SqlCommand com = new SqlCommand("select
* from Student where id in ("+Label1.Text+")",
con);
SqlDataAdapter da = new SqlDataAdapter(com);
ds = new DataSet();
da.Fill(ds);
GridView2.DataSource
= ds;
GridView2.DataBind();
}
string id;
//Code for How to
Copy data of Gridview1 to another GridiView
protected void
btnmove_Click(object sender, EventArgs e)
{
foreach (GridViewRow
gr in GridView1.Rows)
{
CheckBox ck = (CheckBox)gr.FindControl("chk1");
if (ck.Checked)
{
id +=
((Label)gr.Cells[1].Controls[1]).Text + ",";
}
}
if (id != null)
{
Label1.Text
= id.Remove(id.Length - 1);
}
else
{
Label1.Text
= " 0";
}
bindgridview1();
}
//Code for when cheched Header CheckBox all checkbox is
checked......................
protected void
chkall_CheckedChanged(object sender, EventArgs e)
{
CheckBox ch = (CheckBox)sender;
if (ch.Checked)
{
foreach (GridViewRow
grv in GridView1.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = true;
}
}
else
{
foreach (GridViewRow
grv in GridView1.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = false;
}
}
}
//Code for How to
cut data of Gridview1 to another GridiView2
protected void
btncutpaste_Click(object sender, EventArgs e)
{
foreach (GridViewRow
gr in GridView1.Rows)
{
CheckBox ck = (CheckBox)gr.FindControl("chk1");
if (ck.Checked)
{
id +=
((Label)gr.Cells[1].Controls[1]).Text + ",";
gr.Visible
= false;
}
}
if (id != null)
{
Label1.Text
= id.Remove(id.Length - 1);
}
else
{
Label1.Text
= " 0";
}
bindgridview1();
}
}
Tuesday, 3 September 2013
What is Asp.Net Page Life Cycle
03:39
3 comments
What
is Asp.Net Page Life Cycle?
Asp.Net is a powerful platform for building Web
applications. The ASP.NET page life
cycle is a good example to explore so you know how and when page elements are
loaded and corresponding events are fired. When a page is requested, it is
loaded into the server memory, processed and sent to the browser. Then it is
unloaded from the memory. At each of this steps, methods and events are
available, which could be overridden according to the need of the application.
The page
life cycle phases are:
1. Page Initialization (Initialization theme and master
page)
a.
Page_PreInit
b.
Page_Init
c.
Page_InitComplete
2. Page Load (load all control Button,TextBox)
a. Page_PreLoad
b. Page_Load
c.
Page_LoadComplete
3. Page Render (convert server side to client side
(html))
i. Page_Prerender
ii. Page_PreRenderComplete
iii. Page_SaveStateComplete
4. Page Unload (processing destroyed after output)
·
Page_UnLoading
1. Page_PreInit . PreInit is the
first event in page life cycle. means this is the entry point of the ASP.NET
page life cycle - it is the pre-initialization, so you have access to the page
before it is initialized. Controls can be created within this event. Also,
master pages and themes can be accessed. You can check the IsPostBack property
here to determine if it is the first time a page has been loaded.
2. Page_Init . Init event
initializes the control property and the control tree is built. This event can
be handled by overloading the OnInit method or creating a Page_Init handler. This
event fires when all controls on the page have been initialized and skin settings
have been applied.
3. Page_InitComplete . InitComplete event
allows tracking of view state. This event fires once all page and control
initializations complete. This is the last event fired where ViewState is not
set, so ViewState can be manipulated in this event.
4. Page_PreLoad . This event is
triggered before the post back data is loaded in the controls. This event can
be handled by overloading the OnPreLoad method or creating a Page_PreLoad
handler
5. Page_Load . the Load event is
raised for the page first and then recursively for all child controls. The
controls in the control tree are created. This event can be handled by
overloading the OnLoad method or creating a Page_Load handler.
6. Page_LoadComplete . the loading
process is completed, control event handlers are run and page validation takes
place. This event can be handled by overloading the OnLoadComplete method or
creating a Page_LoadComplete handler.
7. Page_PreRender . the PreRender
event occurs just before the output is rendered. By handling this event, pages
and controls can perform any updates before the output is rendered.
8. Page_PreRenderComplete
.
as the PreRender event is recursively fired for all child controls, this event
ensures the completion of the pre-rendering phase.
9. Page_SaveStateComplete
.
state of control on the page is saved. Personalization, control state and view
state information is saved. The HTML markup is generated. This stage can be
handled by overriding the Render method or creating a Page_Render handler.
10. Page_UnLoad . This event fires
for each control and then the page itself. It is fired when the HTML for the
page is fully rendered. This is where you can take care of cleanup tasks, such
as properly closing and disposing database connections..
Example with Page Life Cycle in Asp.net.........................
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_PreInit(object sender, EventArgs e)
{
Response.Write("<i><b><font color='red'> 1.
</font></b></i> Page
PreInit <br/>");
}
protected void Page_Init(object sender, EventArgs
e)
{
Response.Write("<i><b><font color='red'> 2.
</font></b></i> Page Init <br/>");
}
protected void
Page_InitComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='red'> 3.
</font></b></i> Page PreInit Complete </br>");
}
protected void
Page_PreLoad(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Green'> 4.
</font></b></i> Page
PreLoad </br>");
}
protected void Page_Load(object sender, EventArgs
e)
{
Response.Write("<i><b><font color='Green'> 5.
</font></b></i>Page Load <br/>");
}
protected void
Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Green'> 6.
</font></b></i> Page Load Complete </br>");
}
protected void
Page_Prerender(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 7.
</font></b></i> Page PreRender</br>");
}
protected void
Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 8.
</font></b></i> Page PreRender Complete</br>");
}
protected void
Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Blue'> 9.
</font></b></i> Page SaveState Complete</br>");
}
protected void
Button1_Click(object sender, EventArgs e)
{
Response.Write("<i><b><font color='Grey'> 10.
</font></b></i> Button Click </br>");
}
{
Response.Write("<i><b><font color='Pink'> 11.
</font></b></i> Page Unloading Event </br>");
}
}
Monday, 2 September 2013
What is Operator Overloading in C# .Net
02:29
No comments
All the operators in C# have their specific meaning and
functionality; for example the + operator adds two numeric numbers and the –
operator subtracts two numeric values. However, sometimes, you may need to
change the default functionality of an operator. For example, you may want to
use the + operator add two complex numbers. As you know a complex number
comprises a real number and an imaginary number; therefore, you cannot use the
+ operator to directly add two complex numbers. In such a situation, you can
use operator overloading to assign a new meaning and functionality to an
existing operator. Operator overloading is mechanism of assigning new
functionality to an operator, in addition to the already specified
functionality of the operator.
Example
with Operator Overloading
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Polymorphism
{
public partial class OperatorOverloading
: Form
{
public OperatorOverloading()
{
InitializeComponent();
}
class Complex
{
int x, y;
public Complex(int
real, int imag)
{
x =
real;
y =
imag;
}
public static Complex operator
+(Complex c1, Complex
c2)
{
Complex c3 = new Complex(c1.x + c2.x, c1.y + c2.y);
return c3;
}
public override string ToString()
{
return x + "+"
+ y +" "+ "i";
}
}
private void
button1_Click(object sender, EventArgs e)
{
Complex a = new Complex(Convert.ToInt32(txtrno1.Text),Convert.ToInt32(txtino1.Text));
Complex b = new Complex(Convert.ToInt32(txtrno2.Text),
Convert.ToInt32(txtino2.Text));
Complex c = a + b;
lblcomplexno1.Text="First Complex No=" + a.ToString();
lblcomplexno2.Text
= "Second Complex No=" +
b.ToString();
lblcomplexresult.Text="Addition of Complex No ="+
c.ToString();
}
}
}
Subscribe to:
Posts (Atom)




.jpg)










