Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 21st, 2015, 05:43 PM
Authorized User
 
Join Date: Nov 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Question Dropdownlists In DetailsView

I have 2 dropdownlists in a detailsview. One dropdownlist filters the others listitems.

My issue is that I can not figure out how to insert a -1 or 0 value for the top menu text.



Here is the html:

Code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DefaultMode="Insert">
        <Fields>
            <asp:TemplateField HeaderText="Operating System" SortExpression="OperatingSystem" HeaderStyle-Width="225">
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" DataSourceID="SqlDataSource2" DataTextField="OperatingSystem" DataValueField="Id" AppendDataBoundItems="true" Width="210px">
                        <asp:ListItem Value="-1">-- Select Operating System --</asp:ListItem>
                    </asp:DropDownList>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("OperatingSystem") %>'></asp:Label>
                </ItemTemplate>

            <asp:CommandField ShowInsertButton="False" />
        </Fields>
    </asp:DetailsView>

You can see here the reloading of the second dropdownlist:

Code:
protected void dvDropDownList2Model_DataBound(object sender, EventArgs e)
        {

            DropDownList DropDownList2Model = (DropDownList)sender;

            DetailsView DetailsView1 = (DetailsView)DropDownList2Model.NamingContainer;

            if (DetailsView1.DataItem != null)
            {

                string strModel = ((DataRowView)DetailsView1.DataItem)["Id2"] as string;

                ListItem lm = DropDownList2Model.Items.FindByValue(strModel);

                
                //DropDownList2Model.Items.Add(new ListItem("Select Model", "-1"));

                
                if (lm != null) lm.Selected = true;

            }



I have tried many approaches to insert/add the item (this is one of MANY):

Code:
DropDownList2Model.Items.Insert(0, "<-- Select -->");


How can I get this into the dropdownlist view?
 
Old June 25th, 2015, 05:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Something like this should work:

DropDownList2Model.Items.Add(new ListItem("Select Model", "-1"));

This adds it at the end though. Instead, Insert with a ListItem and pass 0 for the index parameter.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Dark-Star (June 27th, 2015)
 
Old June 27th, 2015, 12:45 AM
Authorized User
 
Join Date: Nov 2011
Posts: 15
Thanks: 4
Thanked 0 Times in 0 Posts
Thumbs up Thank You

Thanks Imar!


Your always there for me when I reach a wall I can't figure out





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Multiple DropDownLists in DetailsView dstreeter BOOK: Beginning ASP.NET 4 : in C# and VB 4 December 3rd, 2010 08:51 AM
Using Multiple DropDownLists in DetailsView dstreeter ASP.NET 4 General Discussion 0 December 1st, 2010 06:09 PM
Cascading dropdownlists not working in the edit template of detailsview thd_tq123 ASP.NET 3.5 Professionals 1 February 27th, 2009 07:55 AM
Dropdownlists in DetailsView irelandk BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 February 20th, 2007 10:13 AM
DropDownLists venterjo General .NET 1 January 8th, 2005 09:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.