Results 1 to 6 of 6

Thread: [2005] GridView Postback and Attributes

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    [2005] GridView Postback and Attributes

    Hi all,

    I was hoping someone would give me a hand with an interesting issue i have run into.

    Basically I have a bit of code as follows

    Code:
    Private Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                e.Row.Attributes.Add("OnMouseOver", "return ChangeMyGridView('" & e.Row.ClientID & "');")
                e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='Transparent'")
            End If
    End Sub
    Pretty self explanatory. However after a postback the mouseover and mouseout javascript events no longer work for the GridRows.

    Now some of you will say that this is likely because my binding is occurring under a if Not IsPostBack condition and you would be correct.

    However the actual syntax for the OnMouseOut and OnMouseOver events in the HTML table row IS THERE after the postback so I am quite confused….

    Any Ideas?
    Last edited by maximg; Jan 6th, 2009 at 07:54 AM.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] GridView Postback and Attributes

    What does the Html look like when it's rendered? If it's still there but it's not working it would sound like there is an issue with referencing the ClientID that you're referencing.

    You could make it not rely on the ID at all and do this:
    Code:
    Private Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                e.Row.Attributes.Add("onmouseover", "return ChangeMyGridView(this);")
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='Transparent'")
            End If
    End Sub
    I changed the first item to pass the current object rather than an ID. Then your method would have the referring object.

    Also a little, tiny, minor note that won't change anything at all: JavaScript is case sensitive and all events are all lower case so technically OnMouseOver and OnMouseOut are not valid JavaScript events. They'll still work because browsers have been developed so events and methods are case insensitive but variables are [usually] not. I have no idea why Microsoft has incorrect client-side events in their intellisense but that's another topic and it's all inconsequential at this point.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] GridView Postback and Attributes

    Add an alert to the ChangeMyGridView method. Yeah, poor javascript debugging, so sue me. But you'll be able to tell if the method is being called at all or not, are there any 'conditional' statements in the ChangeMyGridView method that would prevent you from observing anything (so that it looks like nothing is happened)?

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    Resolved Re: [2005] GridView Postback and Attributes

    Thank you both for your responses.

    mendhak: I have tried exactly that and the function is NOT being called
    the actual javascript function is as follows... (Also isnt alert(); pretty much the only way to debug javascript? - Sorry newbie in web programming.)


    Code:
    function ChangeMyGridView(gridViewRowName) {
            var myGridRow = document.getElementById(gridViewRowName);
            myGridRow.style.backgroundColor = "#ffe4b5";
            myGridRow.style.cursor = "Pointer";
        }

    Kasracer: I was not aware you could use such syntax and pass the actual object, I will try rewriting that and see what happens. (will post back soon and let you know) Either way you have given me an idea as it seems very possible that the ClientID is changing between postbacks....

    Thanks again for your responses.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    Re: [2005] GridView Postback and Attributes

    Kasracer you were spot on. After some further investigation it looks like the clientID was changing between postbacks.

    Still confusing why the actual function was failing to call but I may have just messed something up when testing.

    Interestingly the GridViewRows.ClientID was incremented by one.

    So prior to postback
    Code:
    MyGridView.Rows(0).ClientID = "ctl00_ContentPlaceHolder1_UcTaskArea1_MyGridView_ctl01"
    After Postback
    Code:
    MyGridView.Rows(0).ClientID = "ctl00_ContentPlaceHolder1_UcTaskArea1_MyGridView_ctl02"
    Thanks so much for your help Kasracer and also thank you mendhak.

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] GridView Postback and Attributes

    Quote Originally Posted by maximg
    Kasracer you were spot on. After some further investigation it looks like the clientID was changing between postbacks.
    Ahh the wonderful world of ASP.net . Glad I could be of service.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width