Urgent help with ItemTemplate (datagrid) - databound DropDownList

V

VB Programmer

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!
 
K

Karl

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
 
V

VB Programmer

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

Karl

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
 
K

Karl

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top