473,473 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Datagrid & pager

Ben
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control
for setting the page size. I can get the control into the pager inside the
datagrid itemcreated event by checking for ListItemType.Pager. The problem
I'm having is subscribing to the selectedindex changed event. The datagrid
doesn't even seem fire an itemcommand, but that's ok, just the selectedindex
changed event would do...

Any advice is greatly appreciated!
Thanks,
Ben

Nov 18 '05 #1
2 3986
Ben
As a followup to this, how do i instantiate this control on postback? Do I
need to create it each time or can I find a reference to it in the view
state or something...
"Ben" <be*@online.nospam> wrote in message
news:rB*************@fe37.usenetserver.com...
Hi, I'd like to have a datagrid that has a dropdownlist in the pager control for setting the page size. I can get the control into the pager inside the datagrid itemcreated event by checking for ListItemType.Pager. The problem I'm having is subscribing to the selectedindex changed event. The datagrid doesn't even seem fire an itemcommand, but that's ok, just the selectedindex changed event would do...

Any advice is greatly appreciated!
Thanks,
Ben


Nov 18 '05 #2
Hi Ben,

From your description, you're using a webform datagrid which need to paging
the records in it. Currently you also want to add a DropDownlist into the
DataGrid's Pager at runtime so that when the DropDownList's selected index
changed, do some paging processing in the post back event handler,yes? If
there is anything I misunderstand, please feel free to let me know.

As for this problem, here are my suggestions:
Yes, you're right, there is no buildin event that is for our own dynamic
controls adding in the Pager, so we need to define a own event handler for
the dropdownlist in the codebehind class and register it in the ItemCreated
Event. Also, remember it that the dynamic control need to be created and
add into its parent container eveny time in thte page's request( no matter
IsPostBack or not). It is not stored in ViewState. Here are some tech
articles may provide some detailed instruction on this:

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/de...us/dnaspp/html
/viewstate.asp

#Working with Dynamically Created Controls
http://aspnet.4guysfromrolla.com/articles/082102-1.aspx
http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

Now, the solution for your situation is just create the DropDownList
everytime in the DataGrid's ItemCreated event and register eventhandler for
it and add into DataGrid's Pager. Following code is a demo page I've made,
you can have a try if you have anything unclear:

============aspx page==============
<HTML>
<HEAD>
<title>DynamicPagerItem</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:DataGrid id="dgPage" runat="server" AllowPaging="True"
PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid></td>
</tr>
<tr>
<td><FONT face="ËÎÌå"></FONT></td>
</tr>
</table>
</form>
</body>
</HTML>

========codebehind class============
public class DynamicPagerItem : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgPage;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Session["DATASOURCE"] = GetDataSource();
Bind_Grid();
}
}

protected DataTable GetDataSource()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("price",typeof(double));

DataRow dr = null;
bool[] flags = {true,false};
for(int i=1;i<=27;++i)
{
dr = tb.NewRow();
dr["index"] = i.ToString();
dr["name"] = "Name" + i.ToString();
dr["price"] = 3.434 * (i%3 +1);

tb.Rows.Add(dr);
}

return tb;
}

protected void Bind_Grid()
{
DataTable tb =(DataTable)Session["DATASOURCE"];
dgPage.DataSource = tb;
dgPage.DataBind();

}
#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);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgPage.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.dgPage_ItemCreated);
this.dgPage.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.dgPage_PageIn
dexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgPage_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
dgPage.CurrentPageIndex = e.NewPageIndex;
Bind_Grid();
}

private void dgPage_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Pager)
{
DropDownList lstPager = new DropDownList();
for(int i=1;i<=dgPage.PageCount;i++)
{
lstPager.Items.Add(i.ToString());
}
lstPager.SelectedIndexChanged += new
EventHandler(lstPager_SelectedIndexChanged);
lstPager.AutoPostBack = true;
e.Item.Cells[e.Item.Cells.Count-1].Controls.Add(lstPager);
}
}

private void lstPager_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DropDownList lstPager = (DropDownList)sender;
int NewPageIndex = int.Parse(lstPager.SelectedValue) -1;
dgPage.CurrentPageIndex = NewPageIndex;
Bind_Grid();
}
}
====================================
Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sumit | last post by:
Hi, I want to add one java script function on the click of page button of datagrid. But how to get UI control of page button so that i should be able to add attribute for onClick event. ...
2
by: Roy | last post by:
I am very, very new to ASP.NEt and have never done ASP. I have been trying to get a datagrid to work for about 5 days with very limited results. When I use property builder, bind the control, set...
1
by: strout | last post by:
I know it's dumb ... sometimes frustrating I want the text and a dropdownlist align with both side of a table cell, i.e. text align left and dropdownlist align right. I cannot add more cells to...
0
by: James T. | last post by:
Hello! I am using following code to customize DataGrid Pager. It works fine... My question is, how I can replace"&nbsp" with " | " between each LinkButton control? Thanks! James Dim...
4
by: Sandy | last post by:
Hello - Sorry to have posted this twice, but my question wasn't being answered and I think it might be because my subject wasn't clear. I have a datagrid and a dropdownlist (which is outside...
0
by: Ray | last post by:
I am trying to add a "Next >" and "<Prev" button to the pager of my datagrid. I have been able to add the hyperlinks to the pager. I am now trying to determine the correct postback script to...
1
by: Friso Wiskerke | last post by:
Hi all, is it possible to set a separate style in a datagrid pager for the selected page to make it more clear for the user which page is selected? For example: page 3 is selected out of the 8...
5
by: NuB | last post by:
I have a datagrid on my page and allow paging, how can i add text where my pages numbers are? example: Instead of seeing 1 2 3 4 i want to see Page 1 2 3 4 Show All ...
0
by: moondog | last post by:
Just in case anyone else out there is having the same problem of the page numbers appearing in one column instead of across the row, here's how to fix the problem courtesy of swn...@gmail.com from...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.