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 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.
> >

0 comments:

Post a Comment