Programmatically Adding An Item To The DataList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Umut K.

    #1

    Programmatically Adding An Item To The DataList

    Hi all,
    I've a DataList control and it's DataBound by a SqlDataReader.. .
    The Reader returns say 3 records and as expected the datalist shows 3 rows.
    What i want to do is to add another extra Record (which is not neccesarily a
    database record..) but it'll be shown up as an ordinary item in the datalist
    rendered as an Item or AlternatingItem ...
    Is there a way to do this?

    Thanks


  • Bench Wang

    #2
    Re: Programmaticall y Adding An Item To The DataList

    Try to fill DataList with a loop and then add the extra record.
    Or override PreRender event of DataList and add the extra record.
    Hope this helps.


    Comment

    • Bench Wang

      #3
      Re: Programmaticall y Adding An Item To The DataList

      Try to fill DataList with a loop and then add the extra record.
      Or override PreRender event of DataList and add the extra record.
      Hope this helps.


      Comment

      • Umut K.

        #4
        Re: Programmaticall y Adding An Item To The DataList

        Thank you,
        That's what i'm avoiding to do... (but seems like the only way i have .. )
        Why isn't it allowed myDataList.Item s.Add(new DataListItem(). ..... ) or
        smtg. like this..
        ??
        Any other ideas?

        "Bench Wang" <benchwang@sohu .com> wrote in message
        news:OyVp5aCdDH A.2508@tk2msftn gp13.phx.gbl...[color=blue]
        > Try to fill DataList with a loop and then add the extra record.
        > Or override PreRender event of DataList and add the extra record.
        > Hope this helps.
        >
        >[/color]


        Comment

        • Umut K.

          #5
          Re: Programmaticall y Adding An Item To The DataList

          Thank you,
          That's what i'm avoiding to do... (but seems like the only way i have .. )
          Why isn't it allowed myDataList.Item s.Add(new DataListItem(). ..... ) or
          smtg. like this..
          ??
          Any other ideas?

          "Bench Wang" <benchwang@sohu .com> wrote in message
          news:OyVp5aCdDH A.2508@tk2msftn gp13.phx.gbl...[color=blue]
          > Try to fill DataList with a loop and then add the extra record.
          > Or override PreRender event of DataList and add the extra record.
          > Hope this helps.
          >
          >[/color]


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Programmaticall y Adding An Item To The DataList

            Umut,
            For a System.Web.UI.W ebControls.Drop DownList, I use something similar to:

            Dim Data As DataSet
            Dim lst As DropDownList
            lst.DataMember = "MyMember"
            lst.DataTextFie ld = "FieldName"
            lst.DataSrouce = Data

            DataBind()

            lst.Items.Inser t(0, New ListItem("<sele ct item>", ""))

            I use the above in the Page Load event, which adds the <select item> to the
            top of the drop down list. Notice that I call DataBind before I add the
            extra item.

            I would expect a DataList to work the same way.

            Hope this helps
            Jay

            "Umut K." <ukaraba@yahoo. com> wrote in message
            news:udKD%23QCd DHA.880@TK2MSFT NGP09.phx.gbl.. .[color=blue]
            > Hi all,
            > I've a DataList control and it's DataBound by a SqlDataReader.. .
            > The Reader returns say 3 records and as expected the datalist shows 3[/color]
            rows.[color=blue]
            > What i want to do is to add another extra Record (which is not neccesarily[/color]
            a[color=blue]
            > database record..) but it'll be shown up as an ordinary item in the[/color]
            datalist[color=blue]
            > rendered as an Item or AlternatingItem ...
            > Is there a way to do this?
            >
            > Thanks
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Programmaticall y Adding An Item To The DataList

              Umut,
              For a System.Web.UI.W ebControls.Drop DownList, I use something similar to:

              Dim Data As DataSet
              Dim lst As DropDownList
              lst.DataMember = "MyMember"
              lst.DataTextFie ld = "FieldName"
              lst.DataSrouce = Data

              DataBind()

              lst.Items.Inser t(0, New ListItem("<sele ct item>", ""))

              I use the above in the Page Load event, which adds the <select item> to the
              top of the drop down list. Notice that I call DataBind before I add the
              extra item.

              I would expect a DataList to work the same way.

              Hope this helps
              Jay

              "Umut K." <ukaraba@yahoo. com> wrote in message
              news:udKD%23QCd DHA.880@TK2MSFT NGP09.phx.gbl.. .[color=blue]
              > Hi all,
              > I've a DataList control and it's DataBound by a SqlDataReader.. .
              > The Reader returns say 3 records and as expected the datalist shows 3[/color]
              rows.[color=blue]
              > What i want to do is to add another extra Record (which is not neccesarily[/color]
              a[color=blue]
              > database record..) but it'll be shown up as an ordinary item in the[/color]
              datalist[color=blue]
              > rendered as an Item or AlternatingItem ...
              > Is there a way to do this?
              >
              > Thanks
              >
              >[/color]


              Comment

              • Umut K.

                #8
                Re: Programmaticall y Adding An Item To The DataList

                Yes, while this works great for the DropDownList,
                System.Web.UI.W ebControls.Data List doesn't allow sucha method like Insert...
                I mean ther's no method like Insert or Add (or am i missing something?)

                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
                news:%238eWmEId DHA.568@TK2MSFT NGP11.phx.gbl.. .[color=blue]
                > Umut,
                > For a System.Web.UI.W ebControls.Drop DownList, I use something similar to:
                >
                > Dim Data As DataSet
                > Dim lst As DropDownList
                > lst.DataMember = "MyMember"
                > lst.DataTextFie ld = "FieldName"
                > lst.DataSrouce = Data
                >
                > DataBind()
                >
                > lst.Items.Inser t(0, New ListItem("<sele ct item>", ""))
                >
                > I use the above in the Page Load event, which adds the <select item> to[/color]
                the[color=blue]
                > top of the drop down list. Notice that I call DataBind before I add the
                > extra item.
                >
                > I would expect a DataList to work the same way.
                >
                > Hope this helps
                > Jay
                >
                > "Umut K." <ukaraba@yahoo. com> wrote in message
                > news:udKD%23QCd DHA.880@TK2MSFT NGP09.phx.gbl.. .[color=green]
                > > Hi all,
                > > I've a DataList control and it's DataBound by a SqlDataReader.. .
                > > The Reader returns say 3 records and as expected the datalist shows 3[/color]
                > rows.[color=green]
                > > What i want to do is to add another extra Record (which is not[/color][/color]
                neccesarily[color=blue]
                > a[color=green]
                > > database record..) but it'll be shown up as an ordinary item in the[/color]
                > datalist[color=green]
                > > rendered as an Item or AlternatingItem ...
                > > Is there a way to do this?
                > >
                > > Thanks
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Programmaticall y Adding An Item To The DataList

                  Umut,
                  Doh! I don't see an Add or Insert on the DataListItemCol lection either.

                  Curious that they are different, must be some reason. (shrug)

                  Two options I would consider instead then.

                  1. Use the SqlDataReader to populate a DataTable, add your row to the
                  DataTable, then bind to the DataTable.
                  2. Create a 'Proxy' Data reader that returns the values out of a contained
                  SqlDataReader, plus the extra row. This class would implement the
                  IDataReader, IDataRecord, IDisposable, and IEnumerable interfaces.
                  Delegating most calls to the contained SqlDataReader, however it would
                  return one or two records first or last... Similar to how the
                  System.IO.Strea m classes work, where you can chain one stream to second
                  stream chained to a third stream...

                  I haven't done a lot with DataLists, maybe there is an easier way.

                  Hope this helps
                  Jay

                  "Umut K." <ukaraba@yahoo. com> wrote in message
                  news:%234$oazSd DHA.736@TK2MSFT NGP09.phx.gbl.. .[color=blue]
                  > Yes, while this works great for the DropDownList,
                  > System.Web.UI.W ebControls.Data List doesn't allow sucha method like[/color]
                  Insert...[color=blue]
                  > I mean ther's no method like Insert or Add (or am i missing something?)
                  >
                  > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
                  message[color=blue]
                  > news:%238eWmEId DHA.568@TK2MSFT NGP11.phx.gbl.. .[color=green]
                  > > Umut,
                  > > For a System.Web.UI.W ebControls.Drop DownList, I use something similar[/color][/color]
                  to:[color=blue][color=green]
                  > >
                  > > Dim Data As DataSet
                  > > Dim lst As DropDownList
                  > > lst.DataMember = "MyMember"
                  > > lst.DataTextFie ld = "FieldName"
                  > > lst.DataSrouce = Data
                  > >
                  > > DataBind()
                  > >
                  > > lst.Items.Inser t(0, New ListItem("<sele ct item>", ""))
                  > >
                  > > I use the above in the Page Load event, which adds the <select item> to[/color]
                  > the[color=green]
                  > > top of the drop down list. Notice that I call DataBind before I add the
                  > > extra item.
                  > >
                  > > I would expect a DataList to work the same way.
                  > >
                  > > Hope this helps
                  > > Jay
                  > >
                  > > "Umut K." <ukaraba@yahoo. com> wrote in message
                  > > news:udKD%23QCd DHA.880@TK2MSFT NGP09.phx.gbl.. .[color=darkred]
                  > > > Hi all,
                  > > > I've a DataList control and it's DataBound by a SqlDataReader.. .
                  > > > The Reader returns say 3 records and as expected the datalist shows 3[/color]
                  > > rows.[color=darkred]
                  > > > What i want to do is to add another extra Record (which is not[/color][/color]
                  > neccesarily[color=green]
                  > > a[color=darkred]
                  > > > database record..) but it'll be shown up as an ordinary item in the[/color]
                  > > datalist[color=darkred]
                  > > > rendered as an Item or AlternatingItem ...
                  > > > Is there a way to do this?
                  > > >
                  > > > Thanks
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • Umut K.

                    #10
                    Re: Programmaticall y Adding An Item To The DataList

                    Jay, thanks. i'm going to try your suggestions..


                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
                    news:uEHE3hUdDH A.3232@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > Umut,
                    > Doh! I don't see an Add or Insert on the DataListItemCol lection either.
                    >
                    > Curious that they are different, must be some reason. (shrug)
                    >
                    > Two options I would consider instead then.
                    >
                    > 1. Use the SqlDataReader to populate a DataTable, add your row to the
                    > DataTable, then bind to the DataTable.
                    > 2. Create a 'Proxy' Data reader that returns the values out of a contained
                    > SqlDataReader, plus the extra row. This class would implement the
                    > IDataReader, IDataRecord, IDisposable, and IEnumerable interfaces.
                    > Delegating most calls to the contained SqlDataReader, however it would
                    > return one or two records first or last... Similar to how the
                    > System.IO.Strea m classes work, where you can chain one stream to second
                    > stream chained to a third stream...
                    >
                    > I haven't done a lot with DataLists, maybe there is an easier way.
                    >
                    > Hope this helps
                    > Jay
                    >
                    > "Umut K." <ukaraba@yahoo. com> wrote in message
                    > news:%234$oazSd DHA.736@TK2MSFT NGP09.phx.gbl.. .[color=green]
                    > > Yes, while this works great for the DropDownList,
                    > > System.Web.UI.W ebControls.Data List doesn't allow sucha method like[/color]
                    > Insert...[color=green]
                    > > I mean ther's no method like Insert or Add (or am i missing something?)
                    > >
                    > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
                    > message[color=green]
                    > > news:%238eWmEId DHA.568@TK2MSFT NGP11.phx.gbl.. .[color=darkred]
                    > > > Umut,
                    > > > For a System.Web.UI.W ebControls.Drop DownList, I use something similar[/color][/color]
                    > to:[color=green][color=darkred]
                    > > >
                    > > > Dim Data As DataSet
                    > > > Dim lst As DropDownList
                    > > > lst.DataMember = "MyMember"
                    > > > lst.DataTextFie ld = "FieldName"
                    > > > lst.DataSrouce = Data
                    > > >
                    > > > DataBind()
                    > > >
                    > > > lst.Items.Inser t(0, New ListItem("<sele ct item>", ""))
                    > > >
                    > > > I use the above in the Page Load event, which adds the <select item>[/color][/color][/color]
                    to[color=blue][color=green]
                    > > the[color=darkred]
                    > > > top of the drop down list. Notice that I call DataBind before I add[/color][/color][/color]
                    the[color=blue][color=green][color=darkred]
                    > > > extra item.
                    > > >
                    > > > I would expect a DataList to work the same way.
                    > > >
                    > > > Hope this helps
                    > > > Jay
                    > > >
                    > > > "Umut K." <ukaraba@yahoo. com> wrote in message
                    > > > news:udKD%23QCd DHA.880@TK2MSFT NGP09.phx.gbl.. .
                    > > > > Hi all,
                    > > > > I've a DataList control and it's DataBound by a SqlDataReader.. .
                    > > > > The Reader returns say 3 records and as expected the datalist shows[/color][/color][/color]
                    3[color=blue][color=green][color=darkred]
                    > > > rows.
                    > > > > What i want to do is to add another extra Record (which is not[/color]
                    > > neccesarily[color=darkred]
                    > > > a
                    > > > > database record..) but it'll be shown up as an ordinary item in the
                    > > > datalist
                    > > > > rendered as an Item or AlternatingItem ...
                    > > > > Is there a way to do this?
                    > > > >
                    > > > > Thanks
                    > > > >
                    > > > >
                    > > >
                    > > >[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    Working...