Dropdownlist SelectedIndex

Discussion in 'ASP .Net' started by Maziar Aflatoun, Sep 12, 2004.

  1. 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.
     
    Maziar Aflatoun, Sep 12, 2004
    #1
    1. Advertising

  2. Maziar Aflatoun <> 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("NewIsAdministator")).SelectedValue =
    ((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
    }


    --
    Davide Vernole
    MVP ASP/ASP.NET
    Microsoft Certified Solution Developer
     
    Davide Vernole [MVP], Sep 12, 2004
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > Maziar Aflatoun <> 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("NewIsAdministator")).SelectedValue =
    > ((DataRowView)e.Item.DataItem).Row["IsAdministrator"];
    > }
    >
    >
    > --
    > Davide Vernole
    > MVP ASP/ASP.NET
    > Microsoft Certified Solution Developer
    >
     
    Maziar Aflatoun, Sep 12, 2004
    #3
  4. Maziar Aflatoun <> 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("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
     
    Davide Vernole [MVP], Sep 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    1
    Views:
    1,501
    Matthew
    Nov 12, 2003
  2. Bryan Poteet
    Replies:
    0
    Views:
    407
    Bryan Poteet
    Dec 4, 2003
  3. jm
    Replies:
    1
    Views:
    446
    Craig Deelsnyder
    Feb 11, 2004
  4. keithb
    Replies:
    1
    Views:
    12,168
    shauns1
    Nov 1, 2006
  5. S_K
    Replies:
    5
    Views:
    802
    Eliyahu Goldin
    Nov 14, 2007
Loading...

Share This Page