Click here to Skip to main content
15,884,914 members
Articles / Programming Languages / C#

Record Navigation using Events in c#

Rate me:
Please Sign up or sign in to vote.
2.55/5 (8 votes)
3 Nov 20051 min read 52.2K   866   19  
This article explains you how to do Record Navigation in Web Form using events in c#.
<!--------------------------------------------------------------------------->
<!--                           INTRODUCTION                                

 The Code Project article submission template (HTML version)

Using this template will help us post your article sooner. To use, just 
follow the 3 easy steps below:
 
     1. Fill in the article description details
     2. Add links to your images and downloads
     3. Include the main article text

That's all there is to it! All formatting will be done by our submission
scripts and style sheets. 

-->
<!--------------------------------------------------------------------------->
<!--                        IGNORE THIS SECTION                            -->
<html>
	<head>
		<title>The Code Project</title>
		<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</Style>
		<link rel="stylesheet" type="text/css" href="http://www.codeproject.com/styles/global.css">
	</head>
	<body bgcolor="#FFFFFF" color="#000000">
		<!--------------------------------------------------------------------------->
		<!-------------------------------     STEP 1      --------------------------->
		<!--  Fill in the details (CodeProject will reformat this section for you) -->
		<pre>
Title:       Record Navigation in Web Forms using Events in c#
Author:      M. Raja Rajeswari 
Email:       rajee_mr_rajee@hotmail.com
Environment: c#
Keywords:    Event Handling, Record Navigation,View State
Level:       Intermediate
Description: An article on something-or-other
Section      Miscellaneous
SubSection   General
</pre>
		<!-------------------------------     STEP 2      --------------------------->
		<!--  Include download and sample image information.                       -->
		<ul class="download">
			<li>
				<a href="C:\Documents and Settings\m.rajarajeswari\Desktop\WebForm.zip">Download 
					source - XXX Kb</a></li>
		</ul>
		<p><img src="C:\Documents and Settings\m.rajarajeswari\Desktop\Raji\WebForm.jpg" alt="Sample Image - maximum width is 600 pixels"
				width="1000" height="400"></p>
		<!-------------------------------     STEP 3      --------------------------->
		<!--  Add the article text. Please use simple formatting (<h2>, <p> etc)   -->
		<h2>Introduction</h2>
		<p>This article explains you how to do Record Navigation in Web Form using c#. 
			First have a look at my sample web form.
			<h2>Using the code</h2>
		<p>I have used Web User Control for the images to be used in all other web forms. 
			In the User Control Page I have declared the Event.<BR>
			<pre>
Syntax:<br>
public delegate void InsertEventHandler(object sender, EventArgs e);
public event InsertEventHandler NewRecord; 
</pre>
		<p>
			Now I will tell u how to raise the event in the Image Button Click of New.
		</p>
		<pre>
private void btnNewImage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
	OnNew(e);
}		
		</pre>
		<p>
			First u have to write a function to raise the event and then call the function 
			in the Image Button Click. In the function u can write like this.
		</p>
		<pre>
protected virtual void OnNew(EventArgs e)<br>
{
	if(NewRecord!= null)<br>
	{
		NewRecord(this,e);<br>
	}
}
		</pre>
		<p>
			So far u have seen how to decalre an even and raise the event in the 
			UserControl Page. Now u want to use this event in the web form.
			<br>
			<br>
			How do u do that?<br>
			<br>
			It is very simple. Let me explain.<br>
			<br>
			1. To use the Web UserControl in the Web Form just drag the UserControl to the 
			Web Form.<br>
			2. Then give a name to the UserControl. For eg. GenericUserControl.<br>
			3. In the InitalizeComponent() of the Web Form u have to initialize that event.<br>
			Example is shown below.
			<pre>
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
	// CODEGEN: This call is required by the ASP.NET Web Form Designer.
	InitializeComponent();
	base.OnInit(e);
}
		
private void InitializeComponent()
{    
	this.GenericUserControl.NewRecord +=new SamWebApp.frmUserControl.InsertEventHandler(GenericUserControl_NewRecord);
	this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
			</pre>
		<p>
			4. Then in the Web Form u have to write the function for 
			GenericUserControl_NewRecord.
			<pre>
private void GenericUserControl_NewRecord(object sender, EventArgs e)
{
//   ur code comes here...
//   what u want to do in the button click event write here.
}
			</pre>
		</p>
		<p>
			Now coming to the Record Navigation, how to find the
			<br>
			<b>I . First Record in the Datatable?</b>
			<br>
			Follow the above steps mentioned:
			<br>
			1. Initialize an event for the FirstRecord.
			<br>
			2. Then write the function for that event.
			<pre>
private void GenericUserControl_MoveFirstRecord(object sender,EventArgs e)
{
	DataSet dsDataSet = new DataSet();
	DataTable dtDataTable = new DataTable();
	if(dsDataSet!=null)
	{
		dtDataTable = dsDataSet.Tables[0];
		ViewState["currRow"] =0;
	}
	SetNavigationRecord();
}

private void SetNavigationRecord()
{
	txtCustId.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["CustomerID"].ToString();
	txtCompName.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["CompanyName"].ToString();
	txtContName.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["ContactName"].ToString();
	txtContTitle.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["ContactTitle"].ToString();
	txtAddress.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["Address"].ToString();
	txtCity.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["City"].ToString();
	txtRegion.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["Region"].ToString();
	txtPostalCode.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["PostalCode"].ToString();
	txtCountry.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["Country"].ToString();
	txtPhone.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["Phone"].ToString();
	txtFax.Text=dtDataTable.Rows[(int)ViewState["currRow"]]["Fax"].ToString();
}
			</pre>
		<p>
			What is ViewState and Why are we using viewstate?
			<br>
			<br>
			ViewState gets the dictionary of state of information that allows u to save and 
			restore the viewstate of a server control across multiple requests for the same 
			page.
		</p>
		<p>
			<b>II. Next Record in the DataTable</b>
			<pre>
private void GenericUserControl_MoveNextRecord(object sender,EventArgs e)
{
	if(dsDataSet!=null)
	{
		dtDataTable = dsDataSet.Tables[0];
		totRow=dtDataTable.Rows.Count;
		ViewState["currRow"] =((int)ViewState["currRow"]) + 1;
		if ((int)ViewState["currRow"] >= totRow) 
		{ 
			ViewState["currRow"] =0;			
		}
			SetNavigationRecord();	 
	}
}
	</pre>
		</p>
		<p>
		<b>III. Previous Record in the DataTable</b>
		<pre>
private void GenericUserControl_MoveNextRecord(object sender,EventArgs e)
{
	if(dsDataSet!=null)
	{
		dtDataTable = dsDataSet.Tables[0];
		totRow=dtDataTable.Rows.Count;
		ViewState["currRow"] =((int)ViewState["currRow"]) + 1;
		if ((int)ViewState["currRow"] >= totRow) 
		{ 
			ViewState["currRow"] =0;			
		}
				SetNavigationRecord();	 
	}
}
		</pre>
		</p>
		<p>
		<b>IV. Last Record in the DataTable</b>
		<pre>
		
private void GenericUserControl_MoveLastRecord(object sender,EventArgs e)
{
	if(dsDataSet!=null)
	{
		dtDataTable = dsDataSet.Tables[0];
		totRow=dtDataTable.Rows.Count;
		ViewState["currRow"] =totRow -1;
	}
		SetNavigationRecord();	
}
		</pre>
		</p>
		<!-------------------------------    That's it!   --------------------------->
	</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Hai,

I am Rajee. In .NET I have nearly 3 years of experience. Currently working in C#.

With Regards,
Rajee

Comments and Discussions