Click here to Skip to main content
Click here to Skip to main content

Record Navigation using Events in c#

, 3 Nov 2005
Rate this:
Please Sign up or sign in to vote.
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();    
}
        

<!------------------------------- That's 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

Share

About the Author

Raji @ Pottu
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 Pinmemberpnnitin5-Jul-13 0:45 
GeneralNavigation in C# PinmemberNiaraj11-Nov-10 22:18 
Generalthanks alot PinmemberavrailRofa24-Feb-09 7:11 
Generaljust 2 problems Pinmemberjohn ingram4-Nov-05 8:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by Raji @ Pottu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid