 |
| 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 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
|
|
|
|

May 3rd, 2008, 08:01 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>");
}
|
|

May 3rd, 2008, 08:41 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 4th, 2008, 03:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

May 7th, 2008, 07:18 AM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 [email protected]
May
|
|

May 7th, 2008, 08:04 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>");
}
}
|
|

May 14th, 2008, 09:18 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
Thank you for your help with this issue.
Brian
|
|
 |