开发者

Modifying the html output of a gridview

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()
   开发者_JS百科     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.


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...


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜