By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Urgent help with ItemTemplate (datagrid) - databound DropDownList

P: n/a
I have an itemtemplate in a datagrid. I'm trying to set it's value based on
data. Having no trouble with the textbox. But, how do I do the same thing
for a dropdownlist????

<ItemTemplate>
<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtComments" runat="server" Width="168px"
Text='<%# DataBinder.Eval(Container.DataItem, "Comments") %>'></asp:TextBox>
</ItemTemplate>
As you can see the TextBox works great! But, how do I set the
DropDownList's value to a similar data field?

Thanks!
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You can either go in the ItemDataBound event and do

dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
dropdownlist)
ddl.selectedIndex = 1

or you can do something like

<asp:listItem value="yes" selected='<%# DataBinder.Eval(Container.DataItem,
"yesNo") = "yes"%>'>
<asp:listItem value="yes" selected='<%# DataBinder.Eval(Container.DataItem,
"yesNo") = "no"%>'>

or something like that.

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have an itemtemplate in a datagrid. I'm trying to set it's value based on data. Having no trouble with the textbox. But, how do I do the same thing for a dropdownlist????

<ItemTemplate>
<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtComments" runat="server" Width="168px" Text='<%# DataBinder.Eval(Container.DataItem, "Comments") %>'></asp:TextBox> </ItemTemplate>
As you can see the TextBox works great! But, how do I set the
DropDownList's value to a similar data field?

Thanks!

Nov 18 '05 #2

P: n/a
I just tried doing one item. I tried this:

<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes" Selected='<%#
DataBinder.Eval(Container.DataItem, "Results") = "Yes"
%>'>Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>

But I got this error: Compiler Error Message: BC30676: 'DataBinding' is not
an event of 'System.Web.UI.WebControls.ListItem'.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:u5*************@TK2MSFTNGP09.phx.gbl...
You can either go in the ItemDataBound event and do

dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
dropdownlist)
ddl.selectedIndex = 1

or you can do something like

<asp:listItem value="yes" selected='<%# DataBinder.Eval(Container.DataItem, "yesNo") = "yes"%>'>
<asp:listItem value="yes" selected='<%# DataBinder.Eval(Container.DataItem, "yesNo") = "no"%>'>

or something like that.

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have an itemtemplate in a datagrid. I'm trying to set it's value
based on
data. Having no trouble with the textbox. But, how do I do the same

thing
for a dropdownlist????

<ItemTemplate>
<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtComments" runat="server"

Width="168px"
Text='<%# DataBinder.Eval(Container.DataItem, "Comments")

%>'></asp:TextBox>
</ItemTemplate>
As you can see the TextBox works great! But, how do I set the
DropDownList's value to a similar data field?

Thanks!


Nov 18 '05 #3

P: n/a
Oh well, I was wrong.

You'll need to do it the other way:

Private Sub x_ItenDataBound(ByVal source As Object, ByVal e As
RepeaterItemEventArgs) Handles x.ItemDataBound
'get a reference to the DLL here and via e.Item.FindControl("ddlYesNo")
and you can access the row via e.Item.DataIem which will allow you to apply
the logic you need to figure out which item to select
End Sub

Karl
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I just tried doing one item. I tried this:

<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes" Selected='<%#
DataBinder.Eval(Container.DataItem, "Results") = "Yes"
%>'>Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>

But I got this error: Compiler Error Message: BC30676: 'DataBinding' is not an event of 'System.Web.UI.WebControls.ListItem'.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:u5*************@TK2MSFTNGP09.phx.gbl...
You can either go in the ItemDataBound event and do

dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
dropdownlist)
ddl.selectedIndex = 1

or you can do something like

<asp:listItem value="yes" selected='<%#

DataBinder.Eval(Container.DataItem,
"yesNo") = "yes"%>'>
<asp:listItem value="yes" selected='<%#

DataBinder.Eval(Container.DataItem,
"yesNo") = "no"%>'>

or something like that.

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have an itemtemplate in a datagrid. I'm trying to set it's value

based
on
data. Having no trouble with the textbox. But, how do I do the same

thing
for a dropdownlist????

<ItemTemplate>
<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtComments" runat="server"

Width="168px"
Text='<%# DataBinder.Eval(Container.DataItem, "Comments")

%>'></asp:TextBox>
</ItemTemplate>
As you can see the TextBox works great! But, how do I set the
DropDownList's value to a similar data field?

Thanks!



Nov 18 '05 #4

P: n/a
How do I know the data value of the "current" field if I do it this way?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:uz**************@TK2MSFTNGP15.phx.gbl...
Oh well, I was wrong.

You'll need to do it the other way:

Private Sub x_ItenDataBound(ByVal source As Object, ByVal e As
RepeaterItemEventArgs) Handles x.ItemDataBound
'get a reference to the DLL here and via e.Item.FindControl("ddlYesNo")
and you can access the row via e.Item.DataIem which will allow you to apply the logic you need to figure out which item to select
End Sub

Karl
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I just tried doing one item. I tried this:

<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes" Selected='<%#
DataBinder.Eval(Container.DataItem, "Results") = "Yes"
%>'>Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>

But I got this error: Compiler Error Message: BC30676: 'DataBinding' is

not
an event of 'System.Web.UI.WebControls.ListItem'.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:u5*************@TK2MSFTNGP09.phx.gbl...
You can either go in the ItemDataBound event and do

dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
dropdownlist)
ddl.selectedIndex = 1

or you can do something like

<asp:listItem value="yes" selected='<%#

DataBinder.Eval(Container.DataItem,
"yesNo") = "yes"%>'>
<asp:listItem value="yes" selected='<%#

DataBinder.Eval(Container.DataItem,
"yesNo") = "no"%>'>

or something like that.

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> I have an itemtemplate in a datagrid. I'm trying to set it's value

based
on
> data. Having no trouble with the textbox. But, how do I do the same thing
> for a dropdownlist????
>
> <ItemTemplate>
> <asp:DropDownList id="ddlYesNo" runat="server" >
> <asp:ListItem Value="-">-</asp:ListItem>
> <asp:ListItem Value="Yes">Yes</asp:ListItem>
> <asp:ListItem Value="No">No</asp:ListItem>
> </asp:DropDownList>
> <asp:TextBox id="txtComments" runat="server"
Width="168px"
> Text='<%# DataBinder.Eval(Container.DataItem, "Comments")
%>'></asp:TextBox>
> </ItemTemplate>
>
>
> As you can see the TextBox works great! But, how do I set the
> DropDownList's value to a similar data field?
>
> Thanks!
>
>



Nov 18 '05 #5

P: n/a
As I said, you have access to e.Item.DataItem. which is an object, but can
be cast to a DataRowView (assuming you are binding form a
datatable/dataset/dataview)

dim dv as DataRowView = ctype(e.item.DataItem, DataRowView)
if cstr(dv("yesNo")) = "yes" then
...
I think it's a datarowview anyways...

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
How do I know the data value of the "current" field if I do it this way?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:uz**************@TK2MSFTNGP15.phx.gbl...
Oh well, I was wrong.

You'll need to do it the other way:

Private Sub x_ItenDataBound(ByVal source As Object, ByVal e As
RepeaterItemEventArgs) Handles x.ItemDataBound
'get a reference to the DLL here and via e.Item.FindControl("ddlYesNo")
and you can access the row via e.Item.DataIem which will allow you to

apply
the logic you need to figure out which item to select
End Sub

Karl
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I just tried doing one item. I tried this:

<asp:DropDownList id="ddlYesNo" runat="server" >
<asp:ListItem Value="-">-</asp:ListItem>
<asp:ListItem Value="Yes" Selected='<%#
DataBinder.Eval(Container.DataItem, "Results") = "Yes"
%>'>Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>

But I got this error: Compiler Error Message: BC30676: 'DataBinding' is
not
an event of 'System.Web.UI.WebControls.ListItem'.

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote

in message news:u5*************@TK2MSFTNGP09.phx.gbl...
> You can either go in the ItemDataBound event and do
>
> dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
> dropdownlist)
> ddl.selectedIndex = 1
>
> or you can do something like
>
> <asp:listItem value="yes" selected='<%#
DataBinder.Eval(Container.DataItem,
> "yesNo") = "yes"%>'>
> <asp:listItem value="yes" selected='<%#
DataBinder.Eval(Container.DataItem,
> "yesNo") = "no"%>'>
>
> or something like that.
>
> Karl
>
> "VB Programmer" <Do*****************@jEmail.com> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
> > I have an itemtemplate in a datagrid. I'm trying to set it's
value based
> on
> > data. Having no trouble with the textbox. But, how do I do the

same > thing
> > for a dropdownlist????
> >
> > <ItemTemplate>
> > <asp:DropDownList id="ddlYesNo" runat="server" >
> > <asp:ListItem Value="-">-</asp:ListItem>
> > <asp:ListItem Value="Yes">Yes</asp:ListItem>
> > <asp:ListItem Value="No">No</asp:ListItem>
> > </asp:DropDownList>
> > <asp:TextBox id="txtComments" runat="server"
> Width="168px"
> > Text='<%# DataBinder.Eval(Container.DataItem, "Comments")
> %>'></asp:TextBox>
> > </ItemTemplate>
> >
> >
> > As you can see the TextBox works great! But, how do I set the
> > DropDownList's value to a similar data field?
> >
> > Thanks!
> >
> >
>
>



Nov 18 '05 #6

P: n/a
It works!!!! Karl for President!!!!!

THANKS!!!!!

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:OL*************@TK2MSFTNGP10.phx.gbl...
As I said, you have access to e.Item.DataItem. which is an object, but can be cast to a DataRowView (assuming you are binding form a
datatable/dataset/dataview)

dim dv as DataRowView = ctype(e.item.DataItem, DataRowView)
if cstr(dv("yesNo")) = "yes" then
...
I think it's a datarowview anyways...

Karl

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
How do I know the data value of the "current" field if I do it this way?

"Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in
message news:uz**************@TK2MSFTNGP15.phx.gbl...
Oh well, I was wrong.

You'll need to do it the other way:

Private Sub x_ItenDataBound(ByVal source As Object, ByVal e As
RepeaterItemEventArgs) Handles x.ItemDataBound
'get a reference to the DLL here and via e.Item.FindControl("ddlYesNo") and you can access the row via e.Item.DataIem which will allow you to apply
the logic you need to figure out which item to select
End Sub

Karl
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
> I just tried doing one item. I tried this:
>
> <asp:DropDownList id="ddlYesNo" runat="server" >
> <asp:ListItem Value="-">-</asp:ListItem>
> <asp:ListItem Value="Yes" Selected='<%#
> DataBinder.Eval(Container.DataItem, "Results") = "Yes"
> %>'>Yes</asp:ListItem>
> <asp:ListItem Value="No">No</asp:ListItem>
> </asp:DropDownList>
>
> But I got this error: Compiler Error Message: BC30676:
'DataBinding' is not
> an event of 'System.Web.UI.WebControls.ListItem'.
>
> "Karl" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in
> message news:u5*************@TK2MSFTNGP09.phx.gbl...
> > You can either go in the ItemDataBound event and do
> >
> > dim ddl as dropdownlist = ctype(e.item.findControl("ddlYesNo"),
> > dropdownlist)
> > ddl.selectedIndex = 1
> >
> > or you can do something like
> >
> > <asp:listItem value="yes" selected='<%#
> DataBinder.Eval(Container.DataItem,
> > "yesNo") = "yes"%>'>
> > <asp:listItem value="yes" selected='<%#
> DataBinder.Eval(Container.DataItem,
> > "yesNo") = "no"%>'>
> >
> > or something like that.
> >
> > Karl
> >
> > "VB Programmer" <Do*****************@jEmail.com> wrote in message
> > news:%2****************@tk2msftngp13.phx.gbl...
> > > I have an itemtemplate in a datagrid. I'm trying to set it's

value > based
> > on
> > > data. Having no trouble with the textbox. But, how do I do the

same
> > thing
> > > for a dropdownlist????
> > >
> > > <ItemTemplate>
> > > <asp:DropDownList id="ddlYesNo" runat="server" > > <asp:ListItem Value="-">-</asp:ListItem>
> > > <asp:ListItem Value="Yes">Yes</asp:ListItem>
> > > <asp:ListItem Value="No">No</asp:ListItem>
> > > </asp:DropDownList>
> > > <asp:TextBox id="txtComments" runat="server"
> > Width="168px"
> > > Text='<%# DataBinder.Eval(Container.DataItem, "Comments")
> > %>'></asp:TextBox>
> > > </ItemTemplate>
> > >
> > >
> > > As you can see the TextBox works great! But, how do I set the
> > > DropDownList's value to a similar data field?
> > >
> > > Thanks!
> > >
> > >
> >
> >
>
>



Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.