472,973 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 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 8762
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.