views:

28

answers:

3

I'm trying to modify the output of my GridView in the RowDataBound event handler, but it's not working - nothing happens.

My code:

Private Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles MyGridView.RowDataBound
    e.Row.Attributes.Add("data-xkoordinat", 0)
End Sub

What I expect to see:

<tr data-xkoordinat="0">..</tr>

What comes out

<tr>...</tr>

How do I add the attribute?

Update:
I've researched some more, and noticed that there is nothing in the trace about the RowDataBound method - should there be? My Page_Load routine, where the databinding happens, is as follows:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Using db As IDatabaseAdapter = Locator.GetDatabaseAdapter()
    db.Open()
        MyGridView.DataSource = db.ExecuteReader("MatpunktLista", True, {db.CreateParameter("id_uppdrag", Request.QueryString("id_uppdrag"))})
        MyGridView.DataBind()
    End Using
End Sub

Is there anything here that would cause the rows not to be databound? I'm reloading via F5 or by selecting the address field in my browser and hitting Enter, so I don't think postbacks should be an issue.

A: 

This example should point you in the right direction:

http://www.highoncoding.com/Articles/172_Changing_GridView_Row_Color_OnMouseOver.aspx

Leniel Macaferi
If you read the article and compare with my code, you'll notice I'm already doing exactly what they are doing...
Tomas Lycken
There is a difference between ignored by **the browser** and ignored by **the rendering engine** - as long as the attributes are tucked on and sent to the browser, I don't care if it ignores it. I expect it to. (This type of attribute *is* btw supported from HTML5...). My problem here is that the attribute *doesn't even reach the browser*.
Tomas Lycken
Just rolled back my answer correcting the misconception I had. Thanks for explaining this.
Leniel Macaferi
A: 

Hummm... This should work.

Things to check :

  • How are you watching the source ? If you use a tool like Firebug, the source displayed is not really the source your browser received. It may have removed the attribute from display because it doesn't exist in "standard" HTML. Check the source with you're browser's "See html source" functionality to be sure it is not rendered.
  • Maybe you have a library that edits the rendered HTML (I used one once, it generated a more CSS friendly HTML), it may also have removed the unknown attribute.
  • I know it's obvious but be sure there is nowhere a call to Attributes.Clear()

I'm thinking about it but this snippet should work...
See this post, but I'm not sure it'd help.

UPDATE : Please note that you need to declare the row data bound method in you Gridview declaration, it doesn't happen automatically :

<asp:GridView ID="gvMyGrid" runat="server" onrowdatabound="gvMyGrid_RowDataBound">
...
</asp:GridView>
Julien N
I was using the developer tools (i.e. firebug copy) in Chrome, but if I say "View Source" the attribute is still not there. And when my javascript tries to read it, an exception is thrown. I have no libraries that modify the html, and no calls to Attributes.Clear(). I've noticed in the trace that there is no entry for the RowDataBound method, but I don't know if there should be.
Tomas Lycken
@Your update: Turns out it acutally *does* happen automatically. Or well, not automatically, but you'll notice in my code that I'm hooking the event handler up on the codebehind side instead of on the aspx side. Either is OK, as long as one is there. (If you do both, the event handler might get fired twice, which will sometimes give very unexpected results...) However, as you see in my own answer this wasn't the issue either.
Tomas Lycken
Oh OK. I didn't see the binding. Sorry I'm not used to VB syntax.In C# you don't have the possibility to bind the event method to the event in the declaration (or I never saw it).
Julien N
+1  A: 

I don't know why, but now it works. I believe it might have something to do with the server serving up old versions of the code, because when I added trace comments to see what happened, nothing showed up - even after I compiled and reloaded.

Now, suddenly, everything works as expected. The only thing I did was to recompile again, so I assume the problem was old code. For anyone else that finds this: The code supplied in the question should work - just make sure it's actually that code that's being served to the browser...

Tomas Lycken