Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ItemCreated V/s. ItemDataBound

Reply
Thread Tools

ItemCreated V/s. ItemDataBound

 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      09-19-2007
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:

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

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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      09-19-2007
> 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/egoldin
http://usableasp.net


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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:
>
> ---------------------------------------------------
> <aspataGrid 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>
> ........
> </aspataGrid>
> ---------------------------------------------------
>
> 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
>



 
Reply With Quote
 
 
 
 
rn5a@rediffmail.com
Guest
Posts: n/a
 
      09-19-2007
On Sep 19, 8:26 am, "Eliyahu Goldin"
<(E-Mail Removed)> wrote:
> > 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
>
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
>
>
> > 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:

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

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

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      09-20-2007
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On Sep 19, 8:26 am, "Eliyahu Goldin"
> <(E-Mail Removed)> wrote:
>> > 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
>>
>> <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed) oups.com...
>>
>>
>>
>> > 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:

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

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Repeater - ItemDataBound and ItemCreated Dennis E. Jones, Jr. ASP .Net 4 11-18-2005 05:45 PM
Repeater ItemCreated vs ItemDataBound =?Utf-8?B?S2V2aW4gSGVjdG9y?= ASP .Net 1 04-11-2004 12:23 PM
DataList's ItemCreated event Hardy Wang ASP .Net 3 10-27-2003 09:39 PM
Re: getting column value in ItemCreated datagrid event Learning SQL Server ASP .Net 0 08-12-2003 01:38 PM
Why dropdown list created in ItemCreated can't selected lucy ASP .Net 0 08-05-2003 09:12 PM



Advertisments