Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dropdownlist SelectedIndex

Reply
Thread Tools

Dropdownlist SelectedIndex

 
 
Maziar Aflatoun
Guest
Posts: n/a
 
      09-12-2004
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.Dat aItem, "IsAdministator"))
%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="NewIsAdministator" Runat="server"
CssClass="textbox1">
<asp:ListItem Value="0" Text="No"/>
<asp:ListItem Value="1" Text="Yes"/>
</aspropDownList>
</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 With Quote
 
 
 
 
Davide Vernole [MVP]
Guest
Posts: n/a
 
      09-12-2004
Maziar Aflatoun <(E-Mail Removed)> typed:
> Hi everyone,
>
> I have the following code
> .....
> [CUT]
> .....
> 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.


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

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


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


 
Reply With Quote
 
 
 
 
Maziar Aflatoun
Guest
Posts: n/a
 
      09-12-2004
I tried it

((DropDownList)e.Item.FindControl("NewIsAdministat or")).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("NewIsAdministat or")).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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Maziar Aflatoun <(E-Mail Removed)> typed:
>> Hi everyone,
>>
>> I have the following code
>> .....
>> [CUT]
>> .....
>> 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.

>
> Did you try in the method delegate to the ItemDataBound event ? Try this:
>
> if(e.Item.ItemType == ListItemType.EdiItem)
> {
> ((DropDownList)e.Item.FindControl("NewIsAdministat or")).SelectedValue =
> ((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
> }
>
>
> --
> Davide Vernole
> MVP ASP/ASP.NET
> Microsoft Certified Solution Developer
>



 
Reply With Quote
 
Davide Vernole [MVP]
Guest
Posts: n/a
 
      09-12-2004
Maziar Aflatoun <(E-Mail Removed)> typed:
> [CUT]
> 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. [CUT]
> Any idea why it's saying that?


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("NewIsAdministat or")).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 With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When I set the DropDownList.SelectedIndex of ONE DropDownList they ALL change! S_K ASP .Net 5 11-14-2007 04:51 PM
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
How do I move a dropdownlist to a selectedindex value? jm ASP .Net 1 02-11-2004 05:44 PM
DropDownList SelectedIndex value is not working for the last item. Bryan Poteet ASP .Net 0 12-04-2003 06:06 PM
DropDownList SelectedIndex stuck at -1 after IsPostBack Matthew ASP .Net 1 11-12-2003 08:12 PM



Advertisments