p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   Javascript functions from Gridview RowCommand Event (http://p2p.wrox.com/showthread.php?t=83511)

Phil Critchley May 2nd, 2011 06:26 PM

Javascript functions from Gridview RowCommand Event
 
Hi

Does anyone know how to call Javascript functions from a Gridview RowCommand Event in ASP?

Thanks

Imar May 3rd, 2011 04:51 AM

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

vedant123 May 3rd, 2011 05:00 AM

same problem
 
same problem with me.

Phil Critchley May 3rd, 2011 05:15 AM

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

Imar May 3rd, 2011 05:22 AM

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

Phil Critchley May 3rd, 2011 05:24 AM

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


Phil Critchley May 3rd, 2011 05:34 AM

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

Imar May 3rd, 2011 05:49 AM

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

Phil Critchley May 3rd, 2011 06:20 AM

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

Imar May 3rd, 2011 06:27 AM

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


All times are GMT -4. The time now is 11:55 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.