Record Navigation using Events in c#






2.55/5 (8 votes)
Nov 4, 2005
1 min read

52726

866
This article explains you how to do Record Navigation in Web Form using events in c#.
Introduction
This article explains you how to do Record Navigation in Web Form using c#.
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.
Syntax: public delegate void InsertEventHandler(object sender, EventArgs e); public event InsertEventHandler NewRecord;
InsertEventHandler is the EventHandler Name and NewRecord is the Event Name.
Now I will tell you how to raise the event in the Image Button Click of New.
private void btnNewImage_Click(object sender, System.Web.UI.ImageClickEventArgs e) { OnNew(e); }
First you have to write a function to raise the event and then call the function in the Image Button Click. In the function you can write like this.
protected virtual void OnNew(EventArgs e) { if(NewRecord!= null) { NewRecord(this,e); } }
So far you have seen how to decalre an even and raise the event in the UserControl Page. Now you want to use this event in the web form.
How do you do that?
It is very simple. Let me explain.
1. To use the Web UserControl in the Web Form just drag the UserControl to the Web Form.
2. Then give a name to the UserControl. For eg. GenericUserControl.
3. In the InitalizeComponent() of the Web Form you have to initialize that event.
Example is shown below.
#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
4. Then in the Web Form you have to write the function for GenericUserControl_NewRecord.
private void GenericUserControl_NewRecord(object sender, EventArgs e) { // ur code comes here... // what u want to do in the button click event write here. }
Now coming to the Record Navigation, how to find the
I . First Record in the Datatable
Follow the above steps mentioned:
1. Initialize an event for the FirstRecord.
2. Then write the function for that event.
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(); }
What is ViewState and Why are we using viewstate?
ViewState gets the dictionary of state of information that allows you to save and restore the viewstate of a server control across multiple requests for the same page.
II. Next Record in the DataTable
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(); } }
III. Previous Record in the DataTable
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"]= totRow - 1; } SetNavigationRecord(); } }
IV. Last Record in the DataTable
private void GenericUserControl_MoveLastRecord(object sender,EventArgs e) { if(dsDataSet!=null) { dtDataTable = dsDataSet.Tables[0]; totRow=dtDataTable.Rows.Count; ViewState["currRow"] =totRow -1; } SetNavigationRecord(); }