473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 19 '05 #1
6 9072
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.
Jul 19 '05 #2
Thank you,
That's what i'm avoiding to do... (but seems like the only way i have .. )
Why isn't it allowed myDataList.Items.Add(new DataListItem()...... ) or
smtg. like this..
??
Any other ideas?

"Bench Wang" <be*******@sohu.com> wrote in message
news:Oy**************@tk2msftngp13.phx.gbl...
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.

Jul 19 '05 #3
Umut,
For a System.Web.UI.WebControls.DropDownList, I use something similar to:

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

DataBind()

lst.Items.Insert(0, New ListItem("<select 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." <uk*****@yahoo.com> wrote in message
news:ud***************@TK2MSFTNGP09.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 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

Jul 19 '05 #4
Yes, while this works great for the DropDownList,
System.Web.UI.WebControls.DataList 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]" <Ja********@email.msn.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Umut,
For a System.Web.UI.WebControls.DropDownList, I use something similar to:

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

DataBind()

lst.Items.Insert(0, New ListItem("<select 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." <uk*****@yahoo.com> wrote in message
news:ud***************@TK2MSFTNGP09.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 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


Jul 21 '05 #5
Umut,
Doh! I don't see an Add or Insert on the DataListItemCollection 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.Stream 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." <uk*****@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Yes, while this works great for the DropDownList,
System.Web.UI.WebControls.DataList 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]" <Ja********@email.msn.com> wrote in message news:%2***************@TK2MSFTNGP11.phx.gbl...
Umut,
For a System.Web.UI.WebControls.DropDownList, I use something similar to:
Dim Data As DataSet
Dim lst As DropDownList
lst.DataMember = "MyMember"
lst.DataTextField = "FieldName"
lst.DataSrouce = Data

DataBind()

lst.Items.Insert(0, New ListItem("<select 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." <uk*****@yahoo.com> wrote in message
news:ud***************@TK2MSFTNGP09.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 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



Jul 21 '05 #6
Jay, thanks. i'm going to try your suggestions..
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uE**************@TK2MSFTNGP10.phx.gbl...
Umut,
Doh! I don't see an Add or Insert on the DataListItemCollection 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.Stream 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." <uk*****@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
Yes, while this works great for the DropDownList,
System.Web.UI.WebControls.DataList 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]" <Ja********@email.msn.com> wrote in

message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Umut,
For a System.Web.UI.WebControls.DropDownList, I use something similar to:
Dim Data As DataSet
Dim lst As DropDownList
lst.DataMember = "MyMember"
lst.DataTextField = "FieldName"
lst.DataSrouce = Data

DataBind()

lst.Items.Insert(0, New ListItem("<select 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." <uk*****@yahoo.com> wrote in message
news:ud***************@TK2MSFTNGP09.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 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
>
>



Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Umut K. | last post by:
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...
3
by: Barbara Alderton | last post by:
I recently wrote an ASP.NET app that used user controls. I produced a printer-friendly output form. One requirement was a pagebreak between one part of the form and another. I had no problem...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
3
by: J'son | last post by:
Guys, I have created a custom class that derives from DataList so that I can add some custom client side functionality into each new item row (<td>). Heres the class in its simplest form: ...
1
by: | last post by:
I have a label control that I've embedded in a datalist template. I will be binding data to that label. I want to run a string formatting function on the database text before it is injected into...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.