473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9092
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
4287
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
6250
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
3035
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
3244
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
3869
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
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.