ItemDataBoundEventHandler

G

Guest

I have an event ItemDataBoundEventHandler which handles
DataGrid1.ItemDataBound. This event applies special formatting to a webform
datagrid at bind-time. I've noticed, however, that when the page containing
this datagrid is posted-back all formatting is lost. Basically when I open
the page the datagrid looks good, but as soon as a postback occurs the
formatting is wiped-out. How could I prevent postback from doing this?
Thanks!
 
P

pete

I just tried something similar but didn't have any problems. Can you post
the code for the ItemDataBoundEventHandler

Cheers, Pete
 
G

Guest

Sure. Here is the ItemDataBoundEventHandler and the HTML for the affected
column (Column1). Let me explain...
Column(1) of my datagrid is a hyperlink column. Basically if a value exists
in "ImageFilePath" then I want to display the hyperlink in that datagrid row.
If a value does not exist in "ImageFilePath" then I don't want anything to
dispaly. This works correctly, but as soon as the page is posted-back all
columns have the hyperlink. Thanks.

Sub ItemDataBoundEventHandler(ByVal sender As Object, ByVal e As
DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

Dim imagefilepath As String
imagefilepath =
Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ImageFilePath"))

If imagefilepath = "" Then
e.Item.Cells(1).Text = ""
End If

End If
End Sub

<HTML>
<asp:TemplateColumn>
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:HyperLink runat="server" Text="picture" NavigateURL='<%#
DataBinder.Eval(Container, "DataItem.ImageFilePath") %> ' Target="_blank"
ID="HL99" Name="HyperLink1">
</asp:HyperLink>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
</HTML>
 
G

Guest

Sure. Here is the ItemDataBoundEventHandler and the HTML for the affected
column (Column1). Let me explain...
Column(1) of my datagrid is a hyperlink column. Basically if a value exists
in "ImageFilePath" then I want to display the hyperlink in that datagrid row.
If a value does not exist in "ImageFilePath" then I don't want anything to
dispaly. This works correctly, but as soon as the page is posted-back all
columns have the hyperlink. Thanks.

Sub ItemDataBoundEventHandler(ByVal sender As Object, ByVal e As
DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then

Dim imagefilepath As String
imagefilepath =
Convert.ToString(DataBinder.Eval(e.Item.DataItem, "ImageFilePath"))

If imagefilepath = "" Then
e.Item.Cells(1).Text = ""
End If

End If
End Sub

<HTML>
<asp:TemplateColumn>
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:HyperLink runat="server" Text="picture" NavigateURL='<%#
DataBinder.Eval(Container, "DataItem.ImageFilePath") %> ' Target="_blank"
ID="HL99" Name="HyperLink1">
</asp:HyperLink>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
</asp:TemplateColumn>
</HTML>
 
P

pete

Hi Mike,

I've just ran some tests here and can recreate your problem. However, in
order to do so I have to:

1) Bind the datagrid only on the first page load i.e. if
(!Page.IsPostBack)
2) Enable viewstate for the datagrid

Is this what your doing?

Pete
 
P

pete

Glad to help.

The ItemDataBound event fires when you call the DataGridx.DataBind() method
(or the Page.DataBind() method which will make the call for you). Your code
only calls DataBind on first load and not on subsequent postbacks. To get
round this problem you will need to bind the DataGrid on each Page Load.

If you decide to do this, set the EnableViewState to false as the Datagrid
control will no longer need to retrieve its content from viewstate. If you
look at the html which is output to the browser, you should see that the
hidden _Viewstate field is considerably shorter.

Best of luck, Pete
 
G

Guest

Pete, What I've done is added a call to the sub that performs the bind at the
end of this sub. That solved the problem! Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top