473,386 Members | 1,790 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,386 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 9078
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.