Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

ItemCreated V/s. ItemDataBound

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rn5a@rediffmail.com - 19 Sep 2007 13:37 GMT
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
Eliyahu Goldin - 19 Sep 2007 14:26 GMT
> 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?

Signature

Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://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
[quoted text clipped - 84 lines]
>
> Thanks
rn5a@rediffmail.com - 19 Sep 2007 21:04 GMT
On Sep 19, 8:26 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> > then it doesn't work
>
[quoted text clipped - 101 lines]
>
> - 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
Eliyahu Goldin - 20 Sep 2007 09:13 GMT
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.

Signature

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

> On Sep 19, 8:26 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 122 lines]
>
> Ron

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.