.NETGURU
Dropdownlist SelectedIndex
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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.

Reply to this message...
 
    
Davide Vernole [MVP] (VIP)
Maziar Aflatoun <Click here to reveal e-mail address> typed:
[Original message clipped]

Did you try in the method delegate to the ItemDataBound event ? Try this:

if(e.Item.ItemType == ListItemType.EdiItem)
{
((DropDownList)e.Item.FindControl("NewIsAdministator")).SelectedValue =
((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
}

--
Davide Vernole
MVP ASP/ASP.NET
Microsoft Certified Solution Developer

Reply to this message...
 
    
Maziar Aflatoun
I tried it

((DropDownList)e.Item.FindControl("NewIsAdministator")).SelectedIndex =
(int)((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
and it still fails.

I can't figure out why it's complaining

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 137: ((WebControl)e.Item.Cells[8].Controls[0]).CssClass = "button1";
Line 138:
Line 139:
((DropDownList)e.Item.FindControl("NewIsAdministator")).SelectedIndex =
(int)((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
Line 140:
Line 141:

Any idea why it's saying that?

Thank you
Maz.

"Davide Vernole [MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Davide Vernole [MVP] (VIP)
Maziar Aflatoun <Click here to reveal e-mail address> typed:
[Original message clipped]

Ok, I forgot a little thing. This is the correct piece of code (I hope so):

if(e.Item.ItemType == ListItemType.EdiItem)
{
((DropDownList)e.Item.FindControl("NewIsAdministator")).SelectedValue =
((DataRowView)e.Item.DataItem).Row["IsAdministrator"].ToString();
}

As you know, in a DropDownList, the SelectedValue property is a string, so
we have to convert the value received from the DataItem to a string value.

--
Davide Vernole
MVP ASP/ASP.NET
Microsoft Certified Solution Developer

Reply to this message...
 
 
System.ComponentModel.Container
System.Data.DataRowView
System.InvalidCastException
System.Web.UI.DataBinder
System.Web.UI.WebControls.DropDownList
System.Web.UI.WebControls.HorizontalAlign
System.Web.UI.WebControls.ListItem
System.Web.UI.WebControls.ListItemType
System.Web.UI.WebControls.TemplateColumn
System.Web.UI.WebControls.WebControl




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification