How to do alphabetical paging in ASP.NET

Platform: ASP.NET| Category: DataGrid
  • Step 1: Create linkbuttons to display the alphabets at the footer of the datagrid
  • Step 2: Add this buttons in the ItemCreated event . Use the CommandName and CommandArgument properties of the LinkButton to identify them
  • Step 3: To handle the Paging , In the ItemCommand Event respond to action based on the CommandName and CommandArgument of LinkButton that caused event to be raised.

<asp:DataGrid ShowFooter='True' OnItemCreated='ItemCreated' OnItemCommand='ItemCommand' id='DataGrid1' runat='server'></asp:DataGrid>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’ Put user	code to	initialize the page	here
	If Not Page.IsPostBack Then
		BindGrid('')
	End If
End Sub

Sub BindGrid(ByVal stralpha As String)
	Dim cn As SqlConnection
	Dim da As SqlDataAdapter
	Dim ds As DataSet
	cn = New SqlConnection('Server=localhost;uid=sa;pwd=;database=pubs')
	Dim strsql As String = 'Select * from authors '
	If stralpha = '' Then
		strsql = strsql
	Else
		strsql = strsql + '	where au_lname like’' + stralpha + '%’'
	End If
	da = New SqlDataAdapter(strsql, cn)
	ds = New DataSet
	da.Fill(ds, 'Product')
	If (ds.Tables(0).Rows.Count = 0) Then
		Response.Write('No Data Found')
		DataGrid1.DataSource = Nothing
		DataGrid1.DataBind()
	Else
		DataGrid1.DataSource = ds
		DataGrid1.DataBind()
	End If
End Sub

Protected Sub ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
	If e.Item.ItemType = ListItemType.Footer Then
		e.Item.Cells.Clear()
		Dim tc As TableCell = New TableCell
		tc.ColumnSpan = 2
		e.Item.Cells.Add(tc)
		Dim lc As LiteralControl
		Dim lb As LinkButton
		Dim i As Integer
		For i = 65 To 65 + 25
			lc = New LiteralControl
			lb = New LinkButton
			lc.Text = ' '
			lb.Text = Chr(i)
			lb.CommandName = 'alpha'
			lb.CommandArgument = Chr(i)
			tc.Controls.Add(lb)
			tc.Controls.Add(lc)
		Next
	End If
End Sub

Protected Sub ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
	If (e.CommandName = 'alpha') Then
		BindGrid(e.CommandArgument.ToString())
	End If
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to	initialize the page here
	if (!Page.IsPostBack )
	{
		BindGrid('');
	}
}

void BindGrid(string stralpha)
{
	SqlConnection cn;
	SqlDataAdapter da;
	DataSet	ds;
	cn = new SqlConnection ('Server=localhost;uid=sa;pwd=;database=pubs');
	string strsql ='Select * from authors ';
	if (stralpha=='')
	{
		strsql = strsql ;
	}
	else
	{
		strsql = strsql + ' where au_lname like’' + stralpha + '%’';
	}
	da= new SqlDataAdapter (strsql,cn);
	ds= new DataSet ();
	da.Fill (ds, 'Product');
	if (ds.Tables [0].Rows.Count ==0)
	{
		Response.Write ('No Data Found');
		DataGrid1.DataSource=null;
		DataGrid1.DataBind ();
	}
	else
	{
		DataGrid1.DataSource =ds;
		DataGrid1.DataBind (); 
	}
}

protected void ItemCreated(Object sender   , System.Web.UI.WebControls.DataGridItemEventArgs e	 ) 
{
	if (e.Item.ItemType	== ListItemType.Footer)
	{
		e.Item.Cells.Clear();
		TableCell tc = new TableCell();
		tc.ColumnSpan = 2;
		e.Item.Cells.Add(tc);
		LiteralControl lc;
		LinkButton lb;
		string s='' ;
		for (char c=’A’ ; c<= ’Z’ ; )
		{
			lc = newLiteralControl ();
			lb = new LinkButton ();
			s = c.ToString() ;
			lc.Text =' ';
			lb.CommandName	='alpha';
			lb.CommandArgument= s;
			lb.Text=s;
			c =(char)((int)c +1) ;
			tc.Controls.Add(lb);
			tc.Controls.Add(lc);
		}
	}
}
protected void ItemCommand(Object source   , System.Web.UI.WebControls.DataGridCommandEventArgs e) 
{
	if (e.CommandName == 'alpha' )
	{
		BindGrid(e.CommandArgument.ToString	());
	}
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.