GridView PageIndexChanged

  • Thread starter Thread starter Peter
  • Start date Start date
Hi Peter,

As for executing some javascript code on pageIndexChanged event, would you
provide some further information about your page scenario or requirement?

So far based on my understanding, here are some possible approaches we can
add client script behavior at Gridview's paging stage:

1. You can use Gridview's PageIndexChaged event and use
"ClientScript.RegisterStartupScript" to emit some clientscript to
client-side for execution(when the page's output is rendering in client
browser).

=======GridView page index changed===========
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"page_index_script", "alert('page index changed!');", true);

}
====================

2. If you need to do some pre processing(such as right after the user
change Gridview page), I think you may consider customize the Pager of the
gridview. for example, you can define the pager yourself (such as put two
"Move Next" and "Move Previous" buttons ) and you can add script functions
for the two button's click event(one is to do some tasks you want
client-side, another is postback to change page index).

For custom paging, here is an example:

#GridView Custom Paging
http://www.codeproject.com/KB/webforms/GridViewCustomPaging.aspx

If you have any other concerns or any other questions, welcome to post here
for discussion.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.





--------------------
 
The suggested code 'ClientScript.RegisterStartupScript(this.GetType(), ' is
not working nothing happens - when I click on #2 page in the GridView
the event fires but alert never appears.


I have JavaScript resizing code, when the window is resized I use JavaScript
to resize the GridView so it is resized with the window. But when a user
clicks on the pager the GridView resizes to its original size which does not
fit into the window. So I want to call the client JavaScript after user
clicks on the pager just like I do when the window is resized.
 
Thanks for your prompt reply Peter,

Now I got that you want to do some resizing work on the GridView after page
index changes, then I think the first approach I mentioned earlier (about
emiting script in "PageIndexChanged" event should work.

As you said that the "alert" didn't popup, I think there might be something
else cause this. Is your page a standard ASP.NET page, or have you used any
AJAX feature such as updatepanel? There might be some problem with some
GridView events in updatepanel.

Here is my complete page code for the test(a standard simple aspx page):
======================
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1"
onpageindexchanged="GridView1_PageIndexChanged"
onprerender="GridView1_PreRender" PageSize="2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
</Columns>
</asp:GridView>

</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT [id], [name], [age] FROM [persons]">
</asp:SqlDataSource>
</form>
============================

========code behind=============
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"page_index_script", "alert('page index changed!');", true);
}
=================

Also, you can just define a javascript function statically in the aspx
template and reference it in the "RegisterStartupScript" function's
parameter. If necessary, I can send you the web project contains the page
for testing. Please feel free to let me know if there is anything else
different.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: "Peter" <[email protected]>
References: <uar#[email protected]>
Subject: Re: GridView PageIndexChanged
Date: Thu, 24 Jul 2008 00:13:16 -0500
 
Sorry I did not mention it before, yes I am using AJAX update panel.

And that's probably why any "JavaScript" code in the
GridView1_PageIndexChanged event: does not work



I was looking for something like this:
GridView1.Attributes.Add("PageIndexChange", "ResizeGrid();");

but this code does not work either, no errors and nothing happens.


Steven Cheng said:
Thanks for your prompt reply Peter,

Now I got that you want to do some resizing work on the GridView after
page
index changes, then I think the first approach I mentioned earlier (about
emiting script in "PageIndexChanged" event should work.

As you said that the "alert" didn't popup, I think there might be
something
else cause this. Is your page a standard ASP.NET page, or have you used
any
AJAX feature such as updatepanel? There might be some problem with some
GridView events in updatepanel.

Here is my complete page code for the test(a standard simple aspx page):
======================
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1"
onpageindexchanged="GridView1_PageIndexChanged"
onprerender="GridView1_PreRender" PageSize="2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
</Columns>
</asp:GridView>

</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString %>"
SelectCommand="SELECT [id], [name], [age] FROM [persons]">
</asp:SqlDataSource>
</form>
============================

========code behind=============
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(),
"page_index_script", "alert('page index changed!');", true);
}
=================

Also, you can just define a javascript function statically in the aspx
template and reference it in the "RegisterStartupScript" function's
parameter. If necessary, I can send you the web project contains the page
for testing. Please feel free to let me know if there is anything else
different.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


--------------------
From: "Peter" <[email protected]>
References: <uar#[email protected]>
Subject: Re: GridView PageIndexChanged
Date: Thu, 24 Jul 2008 00:13:16 -0500
The suggested code 'ClientScript.RegisterStartupScript(this.GetType(), ' is
not working nothing happens - when I click on #2 page in the GridView
the event fires but alert never appears.


I have JavaScript resizing code, when the window is resized I use JavaScript
to resize the GridView so it is resized with the window. But when a user
clicks on the pager the GridView resizes to its original size which does not
fit into the window. So I want to call the client JavaScript after user
clicks on the pager just like I do when the window is resized.



http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
 
Thanks for your reply Peter,

For AJAX updatepanel scenario, the Page.ClientScript.XXx method will not
work. You can use the "ScriptManager.RegisterStartupScript" method to
register script for running. I've tested it with GridView.PageIndexChanged
event and it works well. here is the modified example page:

#I host the GridView in updatepanel and change to use ScriptManager's
static register scrpit method:

===============aspx =====================
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"
RenderMode="Inline">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1"
onpageindexchanged="GridView1_PageIndexChanged"
onprerender="GridView1_PreRender" PageSize="2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

===========code behind===============
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
//ClientScript.RegisterStartupScript(this.GetType(),
"page_index_script", "alert('page index changed!');", true);

ScriptManager.RegisterStartupScript(this, this.GetType(),
"ajax_client_script", "alert('page index changed!');", true);

}
======================

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Peter" <[email protected]>
References: <uar#[email protected]>
<[email protected]>
 
The suggested code works (Thank You very much), but I have another problem:

I have the following code which highlites the row when user clicks on a row.
The problem is when I go to another page in the GridView the
GridView1_RowCreated event never fires again so the related JavaScript
functions never execute and the Row highliting never occurs after going to
another GridView page.


protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" +
m_iRowIdx.ToString() + "')");
}
m_iRowIdx++;
}
 
Thanks for your reply Peter,

I'm glad that the previous suggestion helped you.

As for the new problem you mentioned, I've performed some tests on my side.
It seems the "ItemCreated" event can fire correctly even when used in
updatepanel. Here is the test page and code behind I've used:

====================aspx template===================
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"
RenderMode="Inline">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" />

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1"
onpageindexchanged="GridView1_PageIndexChanged"
onprerender="GridView1_PreRender" PageSize="2"
onrowcreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="age" HeaderText="age"
SortExpression="age" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

</form>
======================================

===============
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "alert('row " + e.Row.RowIndex
+ " is clicked');");
}


Label1.Text = "GridView1_RowCreated " +
DateTime.Now.ToLongTimeString();
}
==================

The "rowCreated" event will fire whenever I changed page index, also when I
click on a certain row, the javascript alert dialog box will popup.
therefore, I'm wondering whether there is anything else on the page or in
the updatepanel which may affect the Gridview's behavior.

You can compare the page and code to see whether there is any particular
difference.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Peter" <[email protected]>
Subject: Re: GridView PageIndexChanged
Date: Fri, 25 Jul 2008 13:07:35 -0500
 
Back
Top