 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 2nd, 2011, 06:26 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
Javascript functions from Gridview RowCommand Event
Hi
Does anyone know how to call Javascript functions from a Gridview RowCommand Event in ASP?
Thanks
|
|

May 3rd, 2011, 04:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
What exactly are you trying to accomplish? RowCommands fire at the server, while JavaScript runs at the client. This means you cannot "call Javascript functions from a Gridview RowCommand". However, there are ways to fire JavaScript before the command is triggered (that is, before the postback) or after it has run when the page reloads.
Knowing what it is you're after helps me recommending a good strategy.
Cheers,
Imar
|
|

May 3rd, 2011, 05:00 AM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
|
|
same problem
same problem with me.
|
|

May 3rd, 2011, 05:15 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
Gridview RowCommand Event
Hi Imar
Thank you for the quick reply.
I have a gridview in my project which I have added ButtonField controls too. I have got the control to fire an associated procedure but I want it to confirm the procedure with a popup message to the user. I.e. "this publication reservation has now been cancelled".
I've found alot of references for calling Javascript functions from code behind but absolutely none for calling them from code behind within Gridview RowCommand especially when working with buttonfield controls?
Code:
If (e.CommandName = "Cancel") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button from the Rows collection.
Dim row As GridViewRow = GridViewReservations.Rows(index)
PublicationID = row.Cells(0).Text
' Retrieve the row that contains the button from the Rows collection.
Dim rowTwo As GridViewRow = GridViewReservations.Rows(index)
Status = rowTwo.Cells(2).Text
If Status = "Out on Loan\Reserved for Loan" Then
Using myLiteratureCatalogue As New Model1.Literature_Cataloguing_SystemEntities
Dim reservationStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = (PublicationID) Select publication).First()
' Change the status of the publication.
reservationStatus.ReservationID = Nothing
reservationStatus.StatusID = (2)
myLiteratureCatalogue.SaveChanges()
End Using
POPUP CONFIRMATION GOES HERE
System.Threading.Thread.Sleep(5000)
Response.Redirect(Request.RawUrl)
ElseIf Status = "Reserved for Loan" Then
Using myLiteratureCatalogue As New Model1.Literature_Cataloguing_SystemEntities
Dim reservationStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = (PublicationID) Select publication).First()
' Change the status of the publication.
reservationStatus.ReservationID = Nothing
reservationStatus.StatusID = (1)
myLiteratureCatalogue.SaveChanges()
End Using
POPUP CONFIRMATION GOES HERE
System.Threading.Thread.Sleep(5000)
Response.Redirect(Request.RawUrl)
End If
End If
Thanks
|
|

May 3rd, 2011, 05:22 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
Quote:
POPUP CONFIRMATION GOES HERE
System.Threading.Thread.Sleep(5000)
Response.Redirect(Request.RawUrl)
|
This will never work; all three lines of code execute at the server, and no response is sent back to the client until the page is done loading or you do a Response.Redirect.
I see a few options:
1. Ditch the redirect and replace it with a RegisterStartupScript that sends back *client side JavaScript* to the browser which can then present a popup or an alert box.
2. Pass some status in the query string when redirecting to the new page. Then in the new page, use client side JavaScript to look at the query string and display a pop up.
Hope this helps,
Imar
|
|

May 3rd, 2011, 05:24 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
Additional Comments
Hi Imar
I forgot to mention I have a second buttonfield control on my gridview which I need to display completely different messages to the user depending on the evaulation of an IF statement, see below.
Code:
If (e.CommandName = "Loan") Then
' Retrieve the row index stored in the CommandArgument property.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
' Retrieve the row that contains the button from the Rows collection.
Dim row As GridViewRow = GridViewReservations.Rows(index)
PublicationID = row.Cells(0).Text
' Retrieve the row that contains the button from the Rows collection.
Dim rowOne As GridViewRow = GridViewReservations.Rows(index)
PubTitle = rowOne.Cells(1).Text
' Retrieve the row that contains the button from the Rows collection.
Dim rowTwo As GridViewRow = GridViewReservations.Rows(index)
Status = rowTwo.Cells(2).Text
If Status = "Available for Loan" Then
Call UpdateDatabase()
Call UserConfirmation()
Call CBULoanConfirmation()
Using myLiteratureCatalogue As New Model1.Literature_Cataloguing_SystemEntities
Dim reservationStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = (PublicationID) Select publication).First()
' Change the status of the publication.
reservationStatus.ReservationID = Nothing
reservationStatus.StatusID = (2)
myLiteratureCatalogue.SaveChanges()
End Using
MESSAGE TO SAY PUBLICATION IS AVAILABLE FOR COLLECTION
System.Threading.Thread.Sleep(5000)
Response.Redirect(Request.RawUrl)
ElseIf Status = "Out on Loan\Reserved for Loan" Or "Out on Loan" Or "Reserved for Loan" Then
MESSAGE TO SAY PUBLICATION IS NOT AVAILABLE
System.Threading.Thread.Sleep(5000)
Response.Redirect(Request.RawUrl)
Stop
End If
End If
|
|

May 3rd, 2011, 05:34 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
Response.Redirect(Request.RawUrl)
Hi Imar
If I take out Response.Redirect(Request.RawUrl) will the RegisterStartupScript refresh the page after the 5 second pause? as that was the reason why I place the Response.Redirect(Request.RawUrl) in the code at that position?
Phil
|
|

May 3rd, 2011, 05:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
No. The page just completes as it nomally would after a postback from the GridView. The Thread.Sleep just pauses the execution of the page at the server for 5 seconds; all the client sees is a blank screen (or the current page that is being submitted).
Does that help?
Imar
|
|

May 3rd, 2011, 06:20 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
Thank you
Hi Imar
Thank you for the confirmation. I don't think the buttonfield control embedded within gridview fires a post back of the page or at least mine wasn't hence the reason why I put in the 'Response.Redirect'. I suppose I will have to include this in the RegisterStartupScript.
Thank you for all your help and I hope it was also useful to the other thread user.
Phil
|
|

May 3rd, 2011, 06:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
I don't think the buttonfield control embedded within gridview fires a post back of the page or at least mine wasn't
|
I think it does, or it wouldn't fire a RowCommand.
Imar
|
|
 |