Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 2nd, 2011, 07:26 PM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default Javascript functions from Gridview RowCommand Event

Hi

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

Thanks
Reply With Quote
  #2 (permalink)  
Old May 3rd, 2011, 05:51 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old May 3rd, 2011, 06:00 AM
Registered User
 
Join Date: Apr 2011
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default same problem

same problem with me.
Reply With Quote
  #4 (permalink)  
Old May 3rd, 2011, 06:15 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #5 (permalink)  
Old May 3rd, 2011, 06:22 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #6 (permalink)  
Old May 3rd, 2011, 06:24 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #7 (permalink)  
Old May 3rd, 2011, 06:34 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #8 (permalink)  
Old May 3rd, 2011, 06:49 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #9 (permalink)  
Old May 3rd, 2011, 07:20 AM
Authorized User
 
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #10 (permalink)  
Old May 3rd, 2011, 07:27 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't find control in GridView RowCommand rsearing ASP.NET 2.0 Professional 8 September 10th, 2011 08:10 PM
The GridView 'GridView' fired event PageIndexChanging which wasn't handled Phil Critchley ASP.NET 4 General Discussion 10 February 19th, 2011 09:55 AM
Javascript functions and xslt 1.0 variable mlohokare XSLT 18 December 3rd, 2008 02:22 AM
Executing from JavaScript C# functions see07 ASP.NET 1.x and 2.0 Application Design 1 March 1st, 2005 07:02 PM
javascript functions Moharo Javascript 1 September 23rd, 2003 02:04 AM



All times are GMT -4. The time now is 09:23 AM.


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