473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

GridView PageIndexChanged

I want to call a JavaScript on PageIndexChanged event, how do I do that?

Thank You
Peter
Jul 23 '08 #1
4 8796
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/webfor...tomPaging.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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Peter" <cz****@nospam.nospam>
Subject: GridView PageIndexChanged
Date: Wed, 23 Jul 2008 14:42:07 -0500
>
I want to call a JavaScript on PageIndexChanged event, how do I do that?

Thank You
Peter
Jul 24 '08 #2
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.

"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:a6**************@TK2MSFTNGHUB02.phx.gbl...
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/webfor...tomPaging.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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
>>From: "Peter" <cz****@nospam.nospam>
Subject: GridView PageIndexChanged
Date: Wed, 23 Jul 2008 14:42:07 -0500
>>
I want to call a JavaScript on PageIndexChanged event, how do I do that?

Thank You
Peter

Jul 24 '08 #3
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:
ms****@microsoft.com.

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

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Peter" <cz****@nospam.nospam>
References: <ua**************@TK2MSFTNGP03.phx.gbl>
<a6**************@TK2MSFTNGHUB02.phx.gbl>
>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.

"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:a6**************@TK2MSFTNGHUB02.phx.gbl...
>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/webfor...tomPaging.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:
ms****@microsoft.com.

================================================= =
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
>
Jul 24 '08 #4
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 [MSFT]" <st*****@online.microsoft.comwrote in message
news:gh**************@TK2MSFTNGHUB02.phx.gbl...
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:
ms****@microsoft.com.

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

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
>>From: "Peter" <cz****@nospam.nospam>
References: <ua**************@TK2MSFTNGP03.phx.gbl>
<a6**************@TK2MSFTNGHUB02.phx.gbl>
>>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.

"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:a6**************@TK2MSFTNGHUB02.phx.gbl.. .
>>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/webfor...tomPaging.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:
ms****@microsoft.com.

================================================ ==
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
>>

Jul 24 '08 #5

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

Similar topics

2
by: pittster | last post by:
Hi, I've bound a Gridview control to the GetFiles method Here is a code snippet: DirectoryInfo objDi = new DirectoryInfo(Server.MapPath("./somepath")); FileInfo arrFileInfo =...
2
by: WebBuilder451 | last post by:
I have a grid view with a filter based on a dropdown list. when i select a page on the gridview the filter gets dropped. What is the correct way to handle paging when using a filter? asp.net 2.0...
0
by: ThePurpleCat | last post by:
Hi, I'm a newbie to ASP.NET programming but not to Visual Studio. I'm having trouble getting my Master-Details page to work. I have a page enabled GridView which is linked to a FormView control...
0
by: Q. John Chen | last post by:
I have a GridView and a FormView serve as master/detail relation. The GridView has paging enabled - 10 rows/page. The formView is linked to gridView's SelectedValue as the sole parameter. ...
1
by: Rick Droske | last post by:
In the PageIndexChanged event of an ASP.Net 2.0 GridView the Rows property has rows but the DataKeys property has 0 Items ? This definitely seems like a bug to me. Has anyone come up with a good...
3
by: Marc Grutte | last post by:
Hi I am trying to bind a custom datasource to a gridview whilst paging is enabled. What is missing in this code to make the paging + binding work? thanks M <%@ Page Language="VB" %>
4
by: Jonathan Wood | last post by:
Okay, I spent about 3 hours getting the GridView control to do what I needed. Basically, I set the control's AutoGenerateColumns to False and then defined a single template column (which I will...
0
by: shapper | last post by:
Hello, I am trying to create a ItemTemplate at runtime for a ComponentArt Grid. This GridView works in a very similar way to the Asp.Net 2.0 GridView so the implementation method should be...
2
by: ami | last post by:
In my code, I need to implement x numbers of Gridviews dynamically based on user selection. Because I am unaware of the number of gridviews, this is done after the user enters input. I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.