ItemCreated V/s. ItemDataBound

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Sep 19, 2007.

  1. Guest

    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
     
    , Sep 19, 2007
    #1
    1. Advertising

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


    <> wrote in message
    news:...
    > 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, Sep 19, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 19, 8:26 am, "Eliyahu Goldin"
    <> 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
    >
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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- 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
     
    , Sep 19, 2007
    #3
  4. 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


    <> wrote in message
    news:...
    > On Sep 19, 8:26 am, "Eliyahu Goldin"
    > <> 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
    >>
    >> <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >> > 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- 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
    >
     
    Eliyahu Goldin, Sep 20, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucy
    Replies:
    0
    Views:
    323
  2. Learning SQL Server

    Re: getting column value in ItemCreated datagrid event

    Learning SQL Server, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    556
    Learning SQL Server
    Aug 12, 2003
  3. Hardy Wang

    DataList's ItemCreated event

    Hardy Wang, Oct 27, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,609
    Hardy Wang
    Oct 27, 2003
  4. =?Utf-8?B?S2V2aW4gSGVjdG9y?=

    Repeater ItemCreated vs ItemDataBound

    =?Utf-8?B?S2V2aW4gSGVjdG9y?=, Apr 11, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    10,203
    Teemu Keiski
    Apr 11, 2004
  5. Dennis E. Jones, Jr.

    Repeater - ItemDataBound and ItemCreated

    Dennis E. Jones, Jr., Nov 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    15,985
    Dennis E. Jones, Jr.
    Nov 18, 2005
Loading...

Share This Page