Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 3rd, 2008, 09:01 AM
Authorized User
 
Join Date: Apr 2008
Location: Rockville, MD, USA.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two Level Dynamic DropDown Question

I am trying to dynamicly load selections in a DropDown List based on the section that was selected in the first drop down and display a message when the button is clicked. I get an error message concerning the void on the button click. I will include all of the code for your review.

Thank you,

Brian

Mark up code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TwoLevelDropDown.aspx.cs" Inherits="Demos_TwoLevelDropDown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

    <form id="form1" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
        onselectedindexchanged="DropDownList1_SelectedInde xChanged">
        <asp:ListItem>Airplaine</asp:ListItem>
        <asp:ListItem>Car</asp:ListItem>
        <asp:ListItem>Train</asp:ListItem>
    </asp:DropDownList>

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="false">
    </asp:DropDownList>

    <asp:Button ID="Button1" runat="server" Font-Bold="True"
        onclick="Button1_Click" Text="Selections Button" />
 <div>

    </div>
</body>
</html>
</form>

C# Code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Demos_TwoLevelDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] CarArray = new string[4] { "Ford", "Honda", "BMW", "Dodge" };
        string[] AirplaneArray = new string[3] { "Boeing 777", "Boeing 747", "Boeing 777" };
        string[] TrainArray = new string[3] { "Bullet Train", "Amtrack", "Tram" };

        if (DropDownList1.SelectedValue == "Car")
        {
            DropDownList2.DataSource = CarArray;
        }
        else if(DropDownList1.SelectedValue == "Airplane")
        {
            DropDownList2.DataSource = AirplaneArray;
        }
        else
        {
            DropDownList2.DataSource = TrainArray;
        }

    }
}
protected void Button1_Click(object sender, EventArgs e)
{
  Response.Write("You selected <b>" +
        DropDownList1.SelectedValue.ToStrng() + ": " +
        DropDownList2.SelectedValue.ToString() + "</b>");
}
  #2 (permalink)  
Old May 3rd, 2008, 10:31 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

It would help if you posted the actual error message. 9 out of 10 times, that leads to the solution directly.

Have you seen this?

DropDownList1.SelectedValue.ToStrng() + ": " +


Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #3 (permalink)  
Old May 3rd, 2008, 09:41 PM
Authorized User
 
Join Date: Apr 2008
Location: Rockville, MD, USA.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have attached the code and the error message I am getting. Thank you for your help.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Demos_TwoLevelDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] CarArray = new string[4] { "Ford", "Honda", "BMW", "Dodge" };
        string[] AirplaneArray = new string[3] { "Boeing 777", "Boeing 747", "Boeing 777" };
        string[] TrainArray = new string[3] { "Bullet Train", "Amtrack", "Tram" };

        if (DropDownList1.SelectedValue == "Car")
        {
            DropDownList2.DataSource = CarArray;
        }
        else if(DropDownList1.SelectedValue == "Airplane")
        {
            DropDownList2.DataSource = AirplaneArray;
        }
        else
        {
            DropDownList2.DataSource = TrainArray;
        }

    }
}
protected void Button1_Click(object sender, EventArgs e)
{
  Response.Write("You selected <b>" +
        DropDownList1.SelectedValue.ToStrng() + ": " +
        DropDownList2.SelectedValue.ToString() + "</b>");
}

Here is the error message I am getting. The void above is underlined in red.
Error 1 Expected class, delegate, enum, interface, or struct C:\BegASPNET\Site\PlanetWrox\Demos\TwoLevelDropDow n.aspx.cs 42 11 C:\...\DemosThank you,

Brian
  #4 (permalink)  
Old May 4th, 2008, 04:58 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,050
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

Apparently my previous reply didn't help? There's clearly a mistake in there. Some feedback on replies would be nice.

Anyway, looks like you have a brace too many:

    }
}
protected void Button1_Click(object sender, EventArgs e)
{

Looks like you're closing the class as well there.....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #5 (permalink)  
Old May 7th, 2008, 08:18 AM
Registered User
 
Join Date: Apr 2008
Location: Birkeroed, 3460, Denmark.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,

I have checked your code. You are short of one line code when you bind data to the second dropdownlist.

  if (DropDownList1.SelectedValue == "Car")
        {
            DropDownList2.DataSource = CarArray;
            DropDownList2.DataBind();
        }
If you can't understand what I mean, you can send email to may.cao@magasin.dk

May


  #6 (permalink)  
Old May 7th, 2008, 09:04 AM
Registered User
 
Join Date: Mar 2008
Location: madurai, tamilnadu, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ilayaraja
Default

You have missed the following dropdownlist DropDownList2.DataBind();
and wrong brases closed... this coding will work properly

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default15 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] CarArray = new string[4] { "Ford", "Honda", "BMW", "Dodge" };
        string[] AirplaneArray = new string[3] { "Boeing 777", "Boeing 747", "Boeing 777" };
        string[] TrainArray = new string[3] { "Bullet Train", "Amtrack", "Tram" };

        if (DropDownList1.SelectedValue == "Car")
        {
            DropDownList2.DataSource = CarArray;
            DropDownList2.DataBind();
        }
        else if (DropDownList1.SelectedValue == "Airplaine")
        {
            DropDownList2.DataSource = AirplaneArray;
            DropDownList2.DataBind();
        }
        else
        {
            DropDownList2.DataSource = TrainArray;
            DropDownList2.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You selected <b>" +
        DropDownList1.SelectedValue.ToString() + ": " +
        DropDownList2.SelectedValue.ToString() + "</b>");
    }
}

  #7 (permalink)  
Old May 14th, 2008, 10:18 PM
Authorized User
 
Join Date: Apr 2008
Location: Rockville, MD, USA.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thank you for your help with this issue.

Brian
 


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two Level Dynamic DropDown List Error Question workib BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 May 3rd, 2008 03:37 AM
.level and .data files question H2O BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 0 September 17th, 2007 07:03 AM
question about login at server level hertendreef SQL Server 2005 0 August 27th, 2007 05:40 PM
Multiple Self Join w/Dynamic Level Hierarchy LiamLiamLiam SQL Language 0 August 17th, 2007 11:21 AM



All times are GMT -4. The time now is 03:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.