HELP with preselecting dropdownlist item

Last post 06-21-2007 12:23 PM by keyboardcowboy. 4 replies.

Sort Posts:

  • HELP with preselecting dropdownlist item

    06-21-2007, 11:27 AM
    • Loading...
    • pspacek
    • Joined on 06-14-2007, 8:07 PM
    • Posts 4

    Another simple question for the masses I am sure, I have a master content page setup where I have a detailsview in a content placeholder.  When you go into the edit mode of the detailsview on this page I want to preselect a value in a dropdownlist in the editItemTemplate that corrisponds to the value in itemtemplate label. Esentially I want to limit their options to what is in the dropdownlist but preselect the value that is already there.

    Any help or suggestions would be great.

    Thanks

     

    My content page code: 

     

    <asp:TemplateField>

    <HeaderTemplate>

    <asp:Label ID="lblTtpe" Text="Type" runat="Server" Width="50px"></asp:Label>

    </HeaderTemplate>

     

    <ItemTemplate>

    <asp:Label ID="lblType" Text='<%#Eval("Type")%>' runat="Server"/>

    </ItemTemplate>

     

    <EditItemTemplate>

    <asp:DropDownList ID="ddlType" Text='<%# Bind("Type") %>' runat="Server">

    <asp:ListItem Value="Blade">Blade</asp:ListItem>

    <asp:ListItem Value="Rack Mount">Rack Mount</asp:ListItem>

    <asp:ListItem Value="Tower">Tower</asp:ListItem>

    <asp:ListItem Value="Virtual">Virtual</asp:ListItem>

    <asp:ListItem Value="PC">PC</asp:ListItem>

    </asp:DropDownList>

    </EditItemTemplate>

     

    <ItemStyle CssClass="DetailItem"/>

    <HeaderStyle CssClass="DetailHead"/>

    </asp:TemplateField>

  • Re: HELP with preselecting dropdownlist item

    06-21-2007, 11:41 AM
    • Loading...
    • myost2921
    • Joined on 04-27-2006, 8:56 PM
    • Huntsville
    • Posts 58

    I believe you can set the SelectedIndex right in the DropDownList item.  If you don't want to start it at a particular index but a certain item in the list, then I would use the follow bit of code...maybe it will help you:

     

        protected void SetDropDownList(DropDownList ddl, string s)

        {

            ListItem li = ddl.Items.FindByText(s);

            ddl.SelectedIndex = ddl.Items.IndexOf(li);

        }

    Don't forget to mark the response as answered if it answers your question!

    Matthew Yost
    Software Engineer
    MCP, MCAD
  • Re: HELP with preselecting dropdownlist item

    06-21-2007, 11:49 AM

    dropDownLists have a parameter you can set that is selectedValue. so just set that are your golden

    Please remember to click "Mark as Answer" on this post if it helped you.
  • Re: HELP with preselecting dropdownlist item

    06-21-2007, 12:21 PM
    • Loading...
    • pspacek
    • Joined on 06-14-2007, 8:07 PM
    • Posts 4

    let me elaborate on what I am trying to do.

    Scenario:

    Step one: User does a search for an item

    Step two: User picks item from a Gridview

    Step three: User selects edit from details view (item template)

    Step four: User can modify record (EditItemTemplate) (here is where I want to preselect the item in a dropdownlist that matces what was in the details view item template) Basically just carry the value from one template to another but convert from label to dropdown list.

     I hope this clarifies what I am looking for.

     Thanks

  • Re: HELP with preselecting dropdownlist item

    06-21-2007, 12:23 PM
    Answer

    well if you want to convert from label to drop down list item it should be very easy.

    DropDownList1.SelectedValue = Label1.Text;

    Please remember to click "Mark as Answer" on this post if it helped you.
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter