Showing posts with label container. Show all posts
Showing posts with label container. Show all posts

Thursday, March 22, 2012

set ID of control dynamicaly

I tried to set the ID of a radiobuttonlist dynamicaly like this:
<asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
"optionnameFR") %>" runat="server" ></asp:RadioButtonList>
(fyi: the radiobuttonlist-control is placed in a datalist.)
But I get this error:
'<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a valid
identifier
Any help is appreciated,
Nic.We've gotten this question a lot the last couple ws. This is a timing
issue, controls are created before they are databound...in order to create
the control, it needs an id. In other words, you can't really do what you
want to. On the flip side, there's a much better way anyways.
Two things, if you HAVE to create the id dynamically (read on though, cuz
you don't) you can always create the radiobuttonlist dynamically on the
ItemDataBound event. Something like:
sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
if e.item.ItemType = ListItemType.Item then
dim rad as new RadioButtonList
rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
e.item.Controls.Add(rad)
end if
end sub
The above code might not work exactly as is, but should give you an
idea...
The real question you should be asking is if this is necessary? Chances are
that you can assign the radiobuttonlist a static id, say "optionNameFr" and
then access it, in the ITemDataBound or when a button is saved via
e.item.FindControl("optionNameFr")
check out my databinding tutorial for some more ideas:
http://openmymind.net/databinding/index.html
Karl
MY ASP.Net tutorials
http://www.openmymind.net/
"nicholas" <murmurait1@.hotmail.com> wrote in message
news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> I tried to set the ID of a radiobuttonlist dynamicaly like this:
> <asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
> "optionnameFR") %>" runat="server" ></asp:RadioButtonList>
> (fyi: the radiobuttonlist-control is placed in a datalist.)
> But I get this error:
> '<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a
valid
> identifier
> Any help is appreciated,
> Nic.
>
thx a lot for your reply
You're indeed right.
had a look a your link: it's great - real good explanations
THX again
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
> We've gotten this question a lot the last couple ws. This is a timing
> issue, controls are created before they are databound...in order to create
> the control, it needs an id. In other words, you can't really do what you
> want to. On the flip side, there's a much better way anyways.
> Two things, if you HAVE to create the id dynamically (read on though, cuz
> you don't) you can always create the radiobuttonlist dynamically on the
> ItemDataBound event. Something like:
> sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
> if e.item.ItemType = ListItemType.Item then
> dim rad as new RadioButtonList
> rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
> e.item.Controls.Add(rad)
> end if
> end sub
>
> The above code might not work exactly as is, but should give you an
> idea...
> The real question you should be asking is if this is necessary? Chances
are
> that you can assign the radiobuttonlist a static id, say "optionNameFr"
and
> then access it, in the ITemDataBound or when a button is saved via
> e.item.FindControl("optionNameFr")
> check out my databinding tutorial for some more ideas:
> http://openmymind.net/databinding/index.html
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
> "nicholas" <murmurait1@.hotmail.com> wrote in message
> news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> valid
>
Maybe I'll explain what I want to do, as I'm a bit on how to do
it...
It's quite a common problem:
I got a product with product options, such as color, size, etc
All are defined a table.
1 table for the products
1 for the options (optionID, parentoptionID, optionname)
ex:
1, 0, color
2, 1, yellow
3, 1, green
4, 1, blue
5, 0, size
6, 5, small
7, 5, medium
8, 5, large
I want an webpage that generates the product info (that's no problem)
and that generates a way of selecting the options.
So, a dynamically created dropdown would be great, but it could also be done
with a dynamically created radiobuttonlist.
I tried to put a radiobuttonlist in a repeater, but the first problem I meet
is that a control in another control can not be found on the page...
Well, anyway. I think I can work it out, but if you have any hints or links
where it allready is explained, it would be even better.
That's it,
Thanks a lot,
Nic
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
> We've gotten this question a lot the last couple ws. This is a timing
> issue, controls are created before they are databound...in order to create
> the control, it needs an id. In other words, you can't really do what you
> want to. On the flip side, there's a much better way anyways.
> Two things, if you HAVE to create the id dynamically (read on though, cuz
> you don't) you can always create the radiobuttonlist dynamically on the
> ItemDataBound event. Something like:
> sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
> if e.item.ItemType = ListItemType.Item then
> dim rad as new RadioButtonList
> rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
> e.item.Controls.Add(rad)
> end if
> end sub
>
> The above code might not work exactly as is, but should give you an
> idea...
> The real question you should be asking is if this is necessary? Chances
are
> that you can assign the radiobuttonlist a static id, say "optionNameFr"
and
> then access it, in the ITemDataBound or when a button is saved via
> e.item.FindControl("optionNameFr")
> check out my databinding tutorial for some more ideas:
> http://openmymind.net/databinding/index.html
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
> "nicholas" <murmurait1@.hotmail.com> wrote in message
> news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> valid
>
I created a new toppic for this.
THX
"nicholas" <murmurait1@.hotmail.com> wrote in message
news:OiRYdaw0EHA.2016@.TK2MSFTNGP15.phx.gbl...
> Maybe I'll explain what I want to do, as I'm a bit on how to do
> it...
> It's quite a common problem:
> I got a product with product options, such as color, size, etc
> All are defined a table.
> 1 table for the products
> 1 for the options (optionID, parentoptionID, optionname)
> ex:
> 1, 0, color
> 2, 1, yellow
> 3, 1, green
> 4, 1, blue
> 5, 0, size
> 6, 5, small
> 7, 5, medium
> 8, 5, large
> I want an webpage that generates the product info (that's no problem)
> and that generates a way of selecting the options.
> So, a dynamically created dropdown would be great, but it could also be
done
> with a dynamically created radiobuttonlist.
> I tried to put a radiobuttonlist in a repeater, but the first problem I
meet
> is that a control in another control can not be found on the page...
> Well, anyway. I think I can work it out, but if you have any hints or
links
> where it allready is explained, it would be even better.
> That's it,
> Thanks a lot,
> Nic
> "Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
timing
create
you
cuz
> are
> and
>

set ID of control dynamicaly

I tried to set the ID of a radiobuttonlist dynamicaly like this:

<asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
"optionnameFR") %>" runat="server" ></asp:RadioButtonList
(fyi: the radiobuttonlist-control is placed in a datalist.)

But I get this error:
'<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a valid
identifier

Any help is appreciated,
Nic.We've gotten this question a lot the last couple weeks. This is a timing
issue, controls are created before they are databound...in order to create
the control, it needs an id. In other words, you can't really do what you
want to. On the flip side, there's a much better way anyways.

Two things, if you HAVE to create the id dynamically (read on though, cuz
you don't) you can always create the radiobuttonlist dynamically on the
ItemDataBound event. Something like:

sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
if e.item.ItemType = ListItemType.Item then
dim rad as new RadioButtonList
rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
e.item.Controls.Add(rad)
end if
end sub

The above code might not work exactly as is, but should give you an
idea...

The real question you should be asking is if this is necessary? Chances are
that you can assign the radiobuttonlist a static id, say "optionNameFr" and
then access it, in the ITemDataBound or when a button is saved via
e.item.FindControl("optionNameFr")

check out my databinding tutorial for some more ideas:
http://openmymind.net/databinding/index.html

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"nicholas" <murmurait1@.hotmail.com> wrote in message
news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> I tried to set the ID of a radiobuttonlist dynamicaly like this:
> <asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
> "optionnameFR") %>" runat="server" ></asp:RadioButtonList>
> (fyi: the radiobuttonlist-control is placed in a datalist.)
> But I get this error:
> '<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a
valid
> identifier
> Any help is appreciated,
> Nic.
thx a lot for your reply
You're indeed right.

had a look a your link: it's great - real good explanations

THX again

"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
> We've gotten this question a lot the last couple weeks. This is a timing
> issue, controls are created before they are databound...in order to create
> the control, it needs an id. In other words, you can't really do what you
> want to. On the flip side, there's a much better way anyways.
> Two things, if you HAVE to create the id dynamically (read on though, cuz
> you don't) you can always create the radiobuttonlist dynamically on the
> ItemDataBound event. Something like:
> sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
> if e.item.ItemType = ListItemType.Item then
> dim rad as new RadioButtonList
> rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
> e.item.Controls.Add(rad)
> end if
> end sub
>
> The above code might not work exactly as is, but should give you an
> idea...
> The real question you should be asking is if this is necessary? Chances
are
> that you can assign the radiobuttonlist a static id, say "optionNameFr"
and
> then access it, in the ITemDataBound or when a button is saved via
> e.item.FindControl("optionNameFr")
> check out my databinding tutorial for some more ideas:
> http://openmymind.net/databinding/index.html
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
> "nicholas" <murmurait1@.hotmail.com> wrote in message
> news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> > I tried to set the ID of a radiobuttonlist dynamicaly like this:
> > <asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
> > "optionnameFR") %>" runat="server" ></asp:RadioButtonList>
> > (fyi: the radiobuttonlist-control is placed in a datalist.)
> > But I get this error:
> > '<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a
> valid
> > identifier
> > Any help is appreciated,
> > Nic.
Maybe I'll explain what I want to do, as I'm a bit confused on how to do
it...

It's quite a common problem:

I got a product with product options, such as color, size, etc
All are defined a table.
1 table for the products
1 for the options (optionID, parentoptionID, optionname)
ex:
1, 0, color
2, 1, yellow
3, 1, green
4, 1, blue
5, 0, size
6, 5, small
7, 5, medium
8, 5, large

I want an webpage that generates the product info (that's no problem)
and that generates a way of selecting the options.

So, a dynamically created dropdown would be great, but it could also be done
with a dynamically created radiobuttonlist.

I tried to put a radiobuttonlist in a repeater, but the first problem I meet
is that a control in another control can not be found on the page...

Well, anyway. I think I can work it out, but if you have any hints or links
where it allready is explained, it would be even better.

That's it,

Thanks a lot,
Nic

"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
> We've gotten this question a lot the last couple weeks. This is a timing
> issue, controls are created before they are databound...in order to create
> the control, it needs an id. In other words, you can't really do what you
> want to. On the flip side, there's a much better way anyways.
> Two things, if you HAVE to create the id dynamically (read on though, cuz
> you don't) you can always create the radiobuttonlist dynamically on the
> ItemDataBound event. Something like:
> sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
> if e.item.ItemType = ListItemType.Item then
> dim rad as new RadioButtonList
> rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
> e.item.Controls.Add(rad)
> end if
> end sub
>
> The above code might not work exactly as is, but should give you an
> idea...
> The real question you should be asking is if this is necessary? Chances
are
> that you can assign the radiobuttonlist a static id, say "optionNameFr"
and
> then access it, in the ITemDataBound or when a button is saved via
> e.item.FindControl("optionNameFr")
> check out my databinding tutorial for some more ideas:
> http://openmymind.net/databinding/index.html
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
> "nicholas" <murmurait1@.hotmail.com> wrote in message
> news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> > I tried to set the ID of a radiobuttonlist dynamicaly like this:
> > <asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
> > "optionnameFR") %>" runat="server" ></asp:RadioButtonList>
> > (fyi: the radiobuttonlist-control is placed in a datalist.)
> > But I get this error:
> > '<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a
> valid
> > identifier
> > Any help is appreciated,
> > Nic.
I created a new toppic for this.
THX

"nicholas" <murmurait1@.hotmail.com> wrote in message
news:OiRYdaw0EHA.2016@.TK2MSFTNGP15.phx.gbl...
> Maybe I'll explain what I want to do, as I'm a bit confused on how to do
> it...
> It's quite a common problem:
> I got a product with product options, such as color, size, etc
> All are defined a table.
> 1 table for the products
> 1 for the options (optionID, parentoptionID, optionname)
> ex:
> 1, 0, color
> 2, 1, yellow
> 3, 1, green
> 4, 1, blue
> 5, 0, size
> 6, 5, small
> 7, 5, medium
> 8, 5, large
> I want an webpage that generates the product info (that's no problem)
> and that generates a way of selecting the options.
> So, a dynamically created dropdown would be great, but it could also be
done
> with a dynamically created radiobuttonlist.
> I tried to put a radiobuttonlist in a repeater, but the first problem I
meet
> is that a control in another control can not be found on the page...
> Well, anyway. I think I can work it out, but if you have any hints or
links
> where it allready is explained, it would be even better.
> That's it,
> Thanks a lot,
> Nic
> "Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:%23ThLx%23v0EHA.2012@.TK2MSFTNGP15.phx.gbl...
> > We've gotten this question a lot the last couple weeks. This is a
timing
> > issue, controls are created before they are databound...in order to
create
> > the control, it needs an id. In other words, you can't really do what
you
> > want to. On the flip side, there's a much better way anyways.
> > Two things, if you HAVE to create the id dynamically (read on though,
cuz
> > you don't) you can always create the radiobuttonlist dynamically on the
> > ItemDataBound event. Something like:
> > sub repeater_ItemDataBound(s as object, e as RepeaterItemEventArgs)
> > if e.item.ItemType = ListItemType.Item then
> > dim rad as new RadioButtonList
> > rad.id = ctype(e.item.DataItem, DataRowView)("optionnameFR")
> > e.item.Controls.Add(rad)
> > end if
> > end sub
> > The above code might not work exactly as is, but should give you an
> > idea...
> > The real question you should be asking is if this is necessary? Chances
> are
> > that you can assign the radiobuttonlist a static id, say "optionNameFr"
> and
> > then access it, in the ITemDataBound or when a button is saved via
> > e.item.FindControl("optionNameFr")
> > check out my databinding tutorial for some more ideas:
> > http://openmymind.net/databinding/index.html
> > Karl
> > --
> > MY ASP.Net tutorials
> > http://www.openmymind.net/
> > "nicholas" <murmurait1@.hotmail.com> wrote in message
> > news:%23p0Whvv0EHA.2040@.tk2msftngp13.phx.gbl...
> > > I tried to set the ID of a radiobuttonlist dynamicaly like this:
> > > > <asp:RadioButtonList id="<%# DataBinder.Eval(Container.DataItem,
> > > "optionnameFR") %>" runat="server" ></asp:RadioButtonList>
> > > > (fyi: the radiobuttonlist-control is placed in a datalist.)
> > > > But I get this error:
> > > '<%# DataBinder.Eval(Container.DataItem, "optionnameFR") %>' is not a
> > valid
> > > identifier
> > > > Any help is appreciated,
> > > Nic.
> >

Set length of container

Hi to anyone out there that is taking the time to read this. I'm new to
..net and any help given is appreciated.

I am displaying a datalist on a page and I want to limit the length of
the returned container.dataitem that is displayed to 39 characters. How
do i do this in asp.net? I can do this in classic asp by using the
len() function, but i'm a bit stuck on how to do it in asp.net.

Thanks for any help you can give, my code is below.

========================================
'code behind creating list

Dim SQLdata As String
Dim dDS As New DataSet()
SQLdata = "SELECT id, pagedata FROM tblContent WHERE level6 =
'" & sLev6 & "'"
sConn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" &
dbPath & ";"

Dim myConnd As New OleDbConnection(sConn)
Dim myAdapterd As New OleDbDataAdapter(SQLdata, myConnd)
myConnd.Open()

Try
myAdapterd.Fill(dDS)
DLdata.DataSource = dDS.Tables(0).DefaultView
DLdata.DataBind()
Finally
myConnd.Close()
End Try

========================================
'code on aspx page displaying list

<asp:DataList ID="DLdata" runat="server" cssclass="DLdatagrid">
<ItemStyle CssClass="DLdata" />
<ItemTemplate>
<img src="http://pics.10026.com/?src=images/arrow.jpg" alt=""/> <a
href='pagedata.aspx?id=<%#DataBinder.Eval(Container.DataItem,
"id")%>'><%#DataBinder.Eval(Container.DataItem, "pagedata")%></a
</ItemTemplate>
</asp:DataListthe easiest way to do it is:

<%# left(DataBinder.Eval(Container.DataItem, "pagedata"), 39)%
Karl

--
http://www.openmymind.net/

<marcus.lecount@.googlemail.com> wrote in message
news:1138056537.361430.326800@.z14g2000cwz.googlegr oups.com...
> Hi to anyone out there that is taking the time to read this. I'm new to
> .net and any help given is appreciated.
> I am displaying a datalist on a page and I want to limit the length of
> the returned container.dataitem that is displayed to 39 characters. How
> do i do this in asp.net? I can do this in classic asp by using the
> len() function, but i'm a bit stuck on how to do it in asp.net.
> Thanks for any help you can give, my code is below.
> ========================================
> 'code behind creating list
> Dim SQLdata As String
> Dim dDS As New DataSet()
> SQLdata = "SELECT id, pagedata FROM tblContent WHERE level6 =
> '" & sLev6 & "'"
> sConn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" &
> dbPath & ";"
> Dim myConnd As New OleDbConnection(sConn)
> Dim myAdapterd As New OleDbDataAdapter(SQLdata, myConnd)
> myConnd.Open()
> Try
> myAdapterd.Fill(dDS)
> DLdata.DataSource = dDS.Tables(0).DefaultView
> DLdata.DataBind()
> Finally
> myConnd.Close()
> End Try
> ========================================
> 'code on aspx page displaying list
> <asp:DataList ID="DLdata" runat="server" cssclass="DLdatagrid">
> <ItemStyle CssClass="DLdata" />
> <ItemTemplate>
> <img src="http://pics.10026.com/?src=images/arrow.jpg" alt=""/> <a
> href='pagedata.aspx?id=<%#DataBinder.Eval(Container.DataItem,
> "id")%>'><%#DataBinder.Eval(Container.DataItem, "pagedata")%></a>
> </ItemTemplate>
> </asp:DataList
Thanks Karl, I can't believe i missed that. Why do i always miss the
obvious?
Thanks for taking the time to answer, I appreciate your help.

Karl Seguin [MVP] wrote:
> the easiest way to do it is:
> <%# left(DataBinder.Eval(Container.DataItem, "pagedata"), 39)%>
> Karl
> --
> http://www.openmymind.net/
>
> <marcus.lecount@.googlemail.com> wrote in message
> news:1138056537.361430.326800@.z14g2000cwz.googlegr oups.com...
> > Hi to anyone out there that is taking the time to read this. I'm new to
> > .net and any help given is appreciated.
> > I am displaying a datalist on a page and I want to limit the length of
> > the returned container.dataitem that is displayed to 39 characters. How
> > do i do this in asp.net? I can do this in classic asp by using the
> > len() function, but i'm a bit stuck on how to do it in asp.net.
> > Thanks for any help you can give, my code is below.
> > ========================================
> > 'code behind creating list
> > Dim SQLdata As String
> > Dim dDS As New DataSet()
> > SQLdata = "SELECT id, pagedata FROM tblContent WHERE level6 =
> > '" & sLev6 & "'"
> > sConn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" &
> > dbPath & ";"
> > Dim myConnd As New OleDbConnection(sConn)
> > Dim myAdapterd As New OleDbDataAdapter(SQLdata, myConnd)
> > myConnd.Open()
> > Try
> > myAdapterd.Fill(dDS)
> > DLdata.DataSource = dDS.Tables(0).DefaultView
> > DLdata.DataBind()
> > Finally
> > myConnd.Close()
> > End Try
> > ========================================
> > 'code on aspx page displaying list
> > <asp:DataList ID="DLdata" runat="server" cssclass="DLdatagrid">
> > <ItemStyle CssClass="DLdata" />
> > <ItemTemplate>
> > <img src="http://pics.10026.com/?src=images/arrow.jpg" alt=""/> <a
> > href='pagedata.aspx?id=<%#DataBinder.Eval(Container.DataItem,
> > "id")%>'><%#DataBinder.Eval(Container.DataItem, "pagedata")%></a>
> > </ItemTemplate>
> > </asp:DataList

Set length of container

Hi to anyone out there that is taking the time to read this. I'm new to
.net and any help given is appreciated.
I am displaying a datalist on a page and I want to limit the length of
the returned container.dataitem that is displayed to 39 characters. How
do i do this in asp.net? I can do this in classic asp by using the
len() function, but i'm a bit stuck on how to do it in asp.net.
Thanks for any help you can give, my code is below.
========================================
'code behind creating list
Dim SQLdata As String
Dim dDS As New DataSet()
SQLdata = "SELECT id, pagedata FROM tblContent WHERE level6 =
'" & sLev6 & "'"
sConn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" &
dbPath & ";"
Dim myConnd As New OleDbConnection(sConn)
Dim myAdapterd As New OleDbDataAdapter(SQLdata, myConnd)
myConnd.Open()
Try
myAdapterd.Fill(dDS)
DLdata.DataSource = dDS.Tables(0).DefaultView
DLdata.DataBind()
Finally
myConnd.Close()
End Try
========================================
'code on aspx page displaying list
<asp:DataList ID="DLdata" runat="server" cssclass="DLdatagrid">
<ItemStyle CssClass="DLdata" />
<ItemTemplate>
<img src="http://pics.10026.com/?src=/library/images/arrow.jpg" alt=""/> <a
href='pagedata.aspx?id=<%#DataBinder.Eval(Container.DataItem,
"id")%>'><%#DataBinder.Eval(Container.DataItem, "pagedata")%></a>
</ItemTemplate>
</asp:DataList>the easiest way to do it is:
<%# left(DataBinder.Eval(Container.DataItem, "pagedata"), 39)%>
Karl
http://www.openmymind.net/
<marcus.lecount@.googlemail.com> wrote in message
news:1138056537.361430.326800@.z14g2000cwz.googlegroups.com...
> Hi to anyone out there that is taking the time to read this. I'm new to
> .net and any help given is appreciated.
> I am displaying a datalist on a page and I want to limit the length of
> the returned container.dataitem that is displayed to 39 characters. How
> do i do this in asp.net? I can do this in classic asp by using the
> len() function, but i'm a bit stuck on how to do it in asp.net.
> Thanks for any help you can give, my code is below.
> ========================================
> 'code behind creating list
> Dim SQLdata As String
> Dim dDS As New DataSet()
> SQLdata = "SELECT id, pagedata FROM tblContent WHERE level6 =
> '" & sLev6 & "'"
> sConn = "Provider=Microsoft.jet.OLEDB.4.0; Data Source=" &
> dbPath & ";"
> Dim myConnd As New OleDbConnection(sConn)
> Dim myAdapterd As New OleDbDataAdapter(SQLdata, myConnd)
> myConnd.Open()
> Try
> myAdapterd.Fill(dDS)
> DLdata.DataSource = dDS.Tables(0).DefaultView
> DLdata.DataBind()
> Finally
> myConnd.Close()
> End Try
> ========================================
> 'code on aspx page displaying list
> <asp:DataList ID="DLdata" runat="server" cssclass="DLdatagrid">
> <ItemStyle CssClass="DLdata" />
> <ItemTemplate>
> <img src="http://pics.10026.com/?src=/library/images/arrow.jpg" alt=""/> <a
> href='pagedata.aspx?id=<%#DataBinder.Eval(Container.DataItem,
> "id")%>'><%#DataBinder.Eval(Container.DataItem, "pagedata")%></a>
> </ItemTemplate>
> </asp:DataList>
>
Thanks Karl, I can't believe i missed that. Why do i always miss the
obvious?
Thanks for taking the time to answer, I appreciate your help.
Karl Seguin [MVP] wrote:
> the easiest way to do it is:
> <%# left(DataBinder.Eval(Container.DataItem, "pagedata"), 39)%>
> Karl
> --
> http://www.openmymind.net/
>
> <marcus.lecount@.googlemail.com> wrote in message
> news:1138056537.361430.326800@.z14g2000cwz.googlegroups.com...

Tuesday, March 13, 2012

Set object properties via a loop?

I have a data container object that holds user info, with one public property mapping to each field in my database table.

When retrieving a user's info from my db I'm using a DataReader, and writing code like this:

myDBObj.name = reader["name"];
myDBObj.id = reader["id"];

Etc, etc. This works ok, but I was hoping to use the DataReader's GetName and FieldCount methods to automate setting my property values.

Something like:

for (int i = 0; i <= reader.FieldCount; i++)
{
string fieldName = reader.GetName(i);
usr.[fieldName] = (reader.GetDataTypeName(i))reader[i];
}

The part in bold is throwing me--is there a way to use a variable in determining which property I'm assigning? Is there another way to do what I'm trying?

Thanks!

Aaron

You could try reader[i].ToString()

usr.[fieldName] = reader[i].ToString();

Sample Code:

SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand commond = new SqlCommand("SELECT * FROM TEST");
DataSet fillDataSet = new DataSet();
commond.Connection = connection;
commond.CommandType = CommandType.Text;
connection.Open();
SqlDataReader dr;
dr = commond.ExecuteReader();
while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ i.ToString() +":" + dr[i].ToString());
Response.Write("<br>");
}
}
connection.Close();


Obtaining the info via the datareader isn't the problem, the issue is that the object I'm creating (objUserDetail) has properties of varying types (strings, ints, datetimes, etc).

While I could certainly simply write out a long list of property assignments and cast the datareader's info in the correct type each time, I'd much rather figure out a way to do it via a loop.

I figured out how to use a PropertyInfo object to get the names of the properties dynamically at runtime, but now the problem is the datareader is sending all the data back as a string, and I can't find a way to convert/cast it into the format the various properties are expecting.

Normally I would do this:

objUserDetail.UserID = (int)reader["UserID"];

But I need a way to convert the reader's field value dynamically.

Anyone know how?


Can you try this..

(dr[i].GetType()) dr[i].ToString()

while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ dr[i].GetType() +":" + dr[i].ToString());
Response.Write("<br>");
}
}

Why don't you use TypeDataSet?


like this:

Dim myDBobjAs New usrDim fieldNameAs String Dim flagsAs BindingFlags = BindingFlags.InstanceOr BindingFlags.Public Or BindingFlags.IgnoreCaseOr BindingFlags.Static Or BindingFlags.FlattenHierarchyDim pAs PropertyInfoDim readerAs System.Data.SqlClient.SqlDataReader = GetTheReader()'populate one entityFor iAs Integer = 0To (reader.FieldCount - 1) fieldname = reader.GetName(i) p =GetType(usr).GetProperty(fieldName, flags) p.SetValue(myDBobj, reader(i),Nothing)Next

Thanks for your help, everyone!

I was able to get it working, and now I'm reconsidering the whole data table class concept, since that's what DataSets are for, right?

aaron


DataSets are basically just collections of DataTables.

NC...