|
-
Jan 6th, 2009, 07:51 AM
#1
Thread Starter
Member
[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.
-
Jan 6th, 2009, 10:51 AM
#2
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.
-
Jan 6th, 2009, 12:22 PM
#3
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)?
-
Jan 6th, 2009, 07:33 PM
#4
Thread Starter
Member
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.
-
Jan 7th, 2009, 05:20 AM
#5
Thread Starter
Member
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.
-
Jan 7th, 2009, 06:18 AM
#6
Re: [2005] GridView Postback and Attributes
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|