How to set the default value for a dropdownlist

M

Maziar Aflatoun

Hi everyone,

I have the following code
<asp:TemplateColumn HeaderText="Administator" Visible="True"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="IsAdministator" Runat="server" text='<%#
FormatIsAdministator(DataBinder.Eval(Container.DataItem, "IsAdministator"))
%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="NewIsAdministator" Runat="server"
CssClass="textbox1">
<asp:ListItem Value="0" Text="No"/>
<asp:ListItem Value="1" Text="Yes"/>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Does anyone know how in EditItemTemplate I can preselect the ListItem in my
DropDownList based on what's currently in the database (0 or 1 for
NewIsAdministator dropdownlist)?

I tried: SelectedIndex = '<%# DataBinder.Eval(Container.DataItem,
"IsAdministator") %>' but that failed

Thank you
Maz.
 
J

John Wadie

You need to handle the ItemDataBound event of the datagrid, and do
something like the following

if(e.Item.ItemType != ListItemType.EditItem)
return;
DropDownList ddl = (DropDownList)e.Item.Cells[2].Controls[1]; // You need
to change the cell and control index here
DataRowView drv = (DataRowView)e.Item.DataItem;
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByText(FormatIsAdministator(drv["IsAdminista
tor"].ToString())));

Cheers,
John Wadie
 

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