if evaluation in template column

C

Crash

I want to set the Selected attribute depending on the data in the data item, this was what I tried, but didn't work.

<asp:TemplateColumn HeaderText="Schedule Type">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Schedule Type")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat=server Id="SelScheduleType">
<asp:ListItem Value=100 <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>>Daily/Weekly</asp:ListItem>
<asp:ListItem Value=200>Monthly</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

of course it doesnt like the
<%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>

Can anyone point me in the right direction? Thanks...
 
S

Scott G.

This kind of thing is tricky because you need a data binding (i.e. <%# ) but you are working with a nested control.

I would override the DataGrid OnItemDataBound event and look for the item that is being edited and set the DropDownList by hand; something like:

void Item_Bound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddl = e.Item.FindControl("SelScheduleType") as DropDownList;
if (ddl != null)
{
ddl.SelectedIndex = -1;
DataRowView r = e.Item.DataItem as DataRowView;
if (r != null)
{
ListItem it = ddl.Items.FindByValue(r["Schedule Type"].ToString());
if (it != null)
{
it.Selected = true;
}
}
}
}
}

Scott
I want to set the Selected attribute depending on the data in the data item, this was what I tried, but didn't work.

<asp:TemplateColumn HeaderText="Schedule Type">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Schedule Type")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList Runat=server Id="SelScheduleType">
<asp:ListItem Value=100 <%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>>Daily/Weekly</asp:ListItem>
<asp:ListItem Value=200>Monthly</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

of course it doesnt like the
<%if(DataBinder.Eval(Container.DataItem, "Schedule Type")==100){%> Selected="True" <%}%>

Can anyone point me in the right direction? Thanks...
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top