Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#
Article

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   4
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();    
}

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

 
GeneralMy vote of 5 Pin
pnnitin4-Jul-13 23:45
pnnitin4-Jul-13 23:45 
GeneralNavigation in C# Pin
Niaraj11-Nov-10 21:18
Niaraj11-Nov-10 21:18 
Generalthanks alot Pin
avrailRofa24-Feb-09 6:11
avrailRofa24-Feb-09 6:11 
Generaljust 2 problems Pin
john ingram4-Nov-05 7:11
john ingram4-Nov-05 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.