ItemCreated V/s. ItemDataBound

R

rn5a

Assume that a ASPX page displays the products, product id, description
& price of each product in a DataGrid. Also assume that users have
been given the option to sort the DataGrid. In other words, the
headers Product Name, ProductID, Description & Price will be
hyperlinks clicking which the DataGrid will be sorted accordingly.
Each of the 4 headers in the DataGrid is accompanied with an image
which is hidden when the page loads for the first time. This is the
DataGrid code:

---------------------------------------------------
<asp:DataGrid ID="dgProducts" OnItemCommand="dg_ItemCommand"
OnItemDataBound="dg_ItemDataBound" AllowSorting="true"
EnableViewState="true" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkID" CommandArgument="Products.ProductID"
Text="ID" runat="server"/>&nbsp;<img id="imgID" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Container.DataItem("ProductID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkName" CommandArgument="ProductName" Text="NAME"
runat="server"/>&nbsp;<img id="imgName" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
.........
</asp:DataGrid>
---------------------------------------------------

This is the dg_ItemDataBound sub which not only makes the images
visible (depending upon which header a user clicks to sort the
DataGrid) but also changes the image so that users can easily make out
whether the DataGrid has been sorted ascendingly or descendingly. This
is code of dg_ItemDataBound:

---------------------------------------------------
Sub dg_ItemDataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (Page.IsPostBack) Then
If (ea.Item.ItemType = ListItemType.Header) Then
If (Session("Sort") = "Products.ProductID ASC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
ElseIf (Session("Sort") = "Products.ProductID DESC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
End If

If (Session("Sort") = "ProductName ASC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
ElseIf (Session("Sort") = "ProductName DESC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
End If
End If
End If
End Sub
---------------------------------------------------

As such the above code works fine but if I place the ItemDataBound
code (shown above) which makes the images visible/invisible & changes
it depending upon whether the DataGrid is sorted ascendingly or
descendingly in an ItemCreated event function, then it doesn't work.

Why so?

Thanks
 
R

rn5a

then it doesn't work

What doesn't work? What do you observe? Did you try to debug?

Is Session("Sort") set to correct value?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net




Assume that a ASPX page displays the products, product id, description
& price of each product in a DataGrid. Also assume that users have
been given the option to sort the DataGrid. In other words, the
headers Product Name, ProductID, Description & Price will be
hyperlinks clicking which the DataGrid will be sorted accordingly.
Each of the 4 headers in the DataGrid is accompanied with an image
which is hidden when the page loads for the first time. This is the
DataGrid code:
---------------------------------------------------
<asp:DataGrid ID="dgProducts" OnItemCommand="dg_ItemCommand"
OnItemDataBound="dg_ItemDataBound" AllowSorting="true"
EnableViewState="true" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkID" CommandArgument="Products.ProductID"
Text="ID" runat="server"/>&nbsp;<img id="imgID" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Container.DataItem("ProductID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkName" CommandArgument="ProductName" Text="NAME"
runat="server"/>&nbsp;<img id="imgName" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
........
</asp:DataGrid>
---------------------------------------------------
This is the dg_ItemDataBound sub which not only makes the images
visible (depending upon which header a user clicks to sort the
DataGrid) but also changes the image so that users can easily make out
whether the DataGrid has been sorted ascendingly or descendingly. This
is code of dg_ItemDataBound:
---------------------------------------------------
Sub dg_ItemDataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (Page.IsPostBack) Then
If (ea.Item.ItemType = ListItemType.Header) Then
If (Session("Sort") = "Products.ProductID ASC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
ElseIf (Session("Sort") = "Products.ProductID DESC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
End If
If (Session("Sort") = "ProductName ASC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
ElseIf (Session("Sort") = "ProductName DESC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
End If
End If
End If
End Sub
---------------------------------------------------
As such the above code works fine but if I place the ItemDataBound
code (shown above) which makes the images visible/invisible & changes
it depending upon whether the DataGrid is sorted ascendingly or
descendingly in an ItemCreated event function, then it doesn't work.
Thanks- Hide quoted text -

- Show quoted text -

By "it doesn't work", I meant the visibility & change in the images
does not work but sorry to say that it does work.There was a mistake
in the code which is why it wasn't working but I have a related
question.

Now since the images functionality can be achieved using both
ItemCreated & ItemDataBound events, how do I understand when the
ItemCreated event should be used & when the ItemDataBound event should
be used? If I am not wrong, when an item gets created, it also gets
bound to the server control (using the DataBind method). So what's the
difference between the ItemCreated event & the ItemDataBound event?

Thanks,

Regards,

Ron
 
E

Eliyahu Goldin

The difference is quit simple. ItemCreated event doesn't have any access to
the data obtained from the datasource. In your case, the header is not
databound that's why you can use either event. But for databound you must
use ItemDataBound event if you need to access any data.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


then it doesn't work

What doesn't work? What do you observe? Did you try to debug?

Is Session("Sort") set to correct value?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP
[ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net




Assume that a ASPX page displays the products, product id, description
& price of each product in a DataGrid. Also assume that users have
been given the option to sort the DataGrid. In other words, the
headers Product Name, ProductID, Description & Price will be
hyperlinks clicking which the DataGrid will be sorted accordingly.
Each of the 4 headers in the DataGrid is accompanied with an image
which is hidden when the page loads for the first time. This is the
DataGrid code:
---------------------------------------------------
<asp:DataGrid ID="dgProducts" OnItemCommand="dg_ItemCommand"
OnItemDataBound="dg_ItemDataBound" AllowSorting="true"
EnableViewState="true" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkID" CommandArgument="Products.ProductID"
Text="ID" runat="server"/>&nbsp;<img id="imgID" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Container.DataItem("ProductID") %>'
runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:LinkButton ID="lnkName" CommandArgument="ProductName" Text="NAME"
runat="server"/>&nbsp;<img id="imgName" src="IMAGES\Up.gif"
visible="false" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Container.DataItem("ProductName")
%>' runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
........
</asp:DataGrid>
---------------------------------------------------
This is the dg_ItemDataBound sub which not only makes the images
visible (depending upon which header a user clicks to sort the
DataGrid) but also changes the image so that users can easily make out
whether the DataGrid has been sorted ascendingly or descendingly. This
is code of dg_ItemDataBound:
---------------------------------------------------
Sub dg_ItemDataBound(ByVal obj As Object, ByVal ea As
DataGridItemEventArgs)
If (Page.IsPostBack) Then
If (ea.Item.ItemType = ListItemType.Header) Then
If (Session("Sort") = "Products.ProductID ASC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
ElseIf (Session("Sort") = "Products.ProductID DESC") Then
CType(ea.Item.FindControl("imgID"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgID"), HtmlImage).Visible
= True
End If
If (Session("Sort") = "ProductName ASC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Up.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
ElseIf (Session("Sort") = "ProductName DESC") Then
CType(ea.Item.FindControl("imgName"), HtmlImage).Src =
"IMAGES\Down.gif"
CType(ea.Item.FindControl("imgName"),
HtmlImage).Visible = True
End If
End If
End If
End Sub
---------------------------------------------------
As such the above code works fine but if I place the ItemDataBound
code (shown above) which makes the images visible/invisible & changes
it depending upon whether the DataGrid is sorted ascendingly or
descendingly in an ItemCreated event function, then it doesn't work.
Thanks- Hide quoted text -

- Show quoted text -

By "it doesn't work", I meant the visibility & change in the images
does not work but sorry to say that it does work.There was a mistake
in the code which is why it wasn't working but I have a related
question.

Now since the images functionality can be achieved using both
ItemCreated & ItemDataBound events, how do I understand when the
ItemCreated event should be used & when the ItemDataBound event should
be used? If I am not wrong, when an item gets created, it also gets
bound to the server control (using the DataBind method). So what's the
difference between the ItemCreated event & the ItemDataBound event?

Thanks,

Regards,

Ron
 

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

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top