By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,385 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

Datagrid & pager

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.