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 Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2010, 01:33 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default Cascading Drop Downs for a novice

I have trying to get this to work right forever and have had absolutely no luck. I am trying to setup a two tier dropdown menu for long terms goals to narrow selections of a second list of short term goals. I am able to get the dropdowns to work based on two tables from my MSSQL database, but when I try to insert the record I get that awful error....

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I have researched and researched, but every solution I find is too complex to figure out for this novice programmer. Can anyone show me a simple solution or walk me through how I setup the whole webservice stuff???

Here is my page file:

Code:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Index.aspx.cs"Inherits="Anthem_Admin_Index" %>
<%@Registerassembly="AjaxControlToolkit"namespace="AjaxControlToolkit"tagprefix="asp" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<asp:FormViewID="FormView1"runat="server"DataKeyNames="ProductionID"DataSourceID="Tasks"EnableModelValidation="True"DefaultMode="Insert">
<InsertItemTemplate>
LTGoal:
<asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"DataSourceID="LT"DataTextField="LTGoal"DataValueField="LTGoalID">
</asp:DropDownList>
<br/>
STGoal:
<asp:DropDownListID="DropDownList2"runat="server"DataSourceID="ST"DataTextField="SpecificTarget"DataValueField="SpecTargetID"SelectedValue='<%# Bind("STGoal") %>'>
</asp:DropDownList>
<asp:CascadingDropDownID="DropDownList2_CascadingDropDown"runat="server"Enabled="True"ServiceMethod="GetDropDownContents"TargetControlID="DropDownList2"UseContextKey="True"Category="Tasks"EmptyText="Select LT Goal">
</asp:CascadingDropDown>
<br/>
Comments:
<asp:TextBoxID="CommentsTextBox"runat="server"Text='<%# Bind("Comments") %>'/>
<br/>
<asp:LinkButtonID="InsertButton"runat="server"CausesValidation="True"CommandName="Insert"Text="Insert"/>
&nbsp;<asp:LinkButtonID="InsertCancelButton"runat="server"CausesValidation="False"CommandName="Cancel"Text="Cancel"/>
<asp:SqlDataSourceID="LT"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"SelectCommand="SELECT [LTGoalID], [LTGoal] FROM [TblGoalsLT]">
</asp:SqlDataSource>
<asp:SqlDataSourceID="ST"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"SelectCommand="SELECT [SpecTargetID], [SpecificTarget] FROM [TblGoalsST] WHERE ([MeasureID] = @MeasureID)">
<SelectParameters>
<asp:ControlParameterControlID="DropDownList1"Name="MeasureID"PropertyName="SelectedValue"Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSourceID="Tasks"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"DeleteCommand="DELETE FROM [TblProduction] WHERE [ProductionID] = @ProductionID"InsertCommand="INSERT INTO [TblProduction] ([LTGoal], [STGoal], [Comments]) VALUES (@LTGoal, @STGoal, @Comments)"SelectCommand="SELECT [ProductionID], [LTGoal], [STGoal], [Comments] FROM [TblProduction]"UpdateCommand="UPDATE [TblProduction] SET [LTGoal] = @LTGoal, [STGoal] = @STGoal, [Comments] = @Comments WHERE [ProductionID] = @ProductionID">
<DeleteParameters>
<asp:ParameterName="ProductionID"Type="Int32"/>
</DeleteParameters>
<InsertParameters>
<asp:ParameterName="LTGoal"Type="Int32"/>
<asp:ParameterName="STGoal"Type="Int32"/>
<asp:ParameterName="Comments"Type="String"/>
</InsertParameters>
<UpdateParameters>
<asp:ParameterName="LTGoal"Type="Int32"/>
<asp:ParameterName="STGoal"Type="Int32"/>
<asp:ParameterName="Comments"Type="String"/>
<asp:ParameterName="ProductionID"Type="Int32"/>
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
And my code....
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
publicpartialclassAnthem_Admin_Index : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
publicstatic AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
{
returndefault(AjaxControlToolkit.CascadingDropDownNameValue[]);
}
}



I know it cant be all that complicated, but I can not seem to figure it out on my own.

Please help......
  #2 (permalink)  
Old July 27th, 2010, 11:41 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default

I am sure that I am missing some simple little thing in here. Is there anyone that can help me get this page working properly???? Please please please......HELP! :)
  #3 (permalink)  
Old July 27th, 2010, 12:09 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

It's a bit hard to help you, as it's not really clear what you're trying to accomplish, how and what the problem is. The fact your code is so garbled doesn't make things any easier.

Can you post a more detailed description of the problem and what it is you;re trying to do? Also, can you repost your relevant code and then use the Remove Text Formatting button on the toolbar before you wrap the code in code tags (also on the toolbar). Because of a bug in this form, code pasted from Visual Studio gets messed up real bad.

It also seems you're missing code that returns second level content in the GetDropDownContents method. Right now, it just returns an empty default instance.
Finally have you looked the documentation for the CascadingDropDown control? It has a sample that uses a database: http://www.asp.net/ajax/ajaxcontrolt...CCDWithDB.aspx

Cheers,

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!
  #4 (permalink)  
Old July 27th, 2010, 12:27 PM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default

Sorry, but I am just trying to get a cascading dropdown item (basically what is on the site http://www.asp.net/ajax/ajaxcontrolt...CCDWithDB.aspx) to bind back to a MSSQL database. I have three relevanttables that I am pulling from

tblLTGoals - Which is the first level of the drop down
tblSTGoals - Which is the second level of the drop down
tblProduction - Which is where I want to store what ends up in the second dropdown.

You say that I am missing code that returns the second level content in the GetDropDownContents method. I have a feeling this is exactly what I am missing that is causing my errors. Is there any help you can provide in how I complete that code?

Here is the cleaned up code.
Code:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Index.aspx.cs"Inherits="Anthem_Admin_Index" %>
<%@Registerassembly="AjaxControlToolkit"namespace="AjaxControlToolkit"tagprefix="asp" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<asp:FormViewID="FormView1"runat="server"DataKeyNames="ProductionID"DataSourceID="Tasks"EnableModelValidation="True"DefaultMode="Insert">
<InsertItemTemplate>
LTGoal:
<asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"DataSourceID="LT"DataTextField="LTGoal"DataValueField="LTGoalID">
</asp:DropDownList>
<br/>
STGoal:
<asp:DropDownListID="DropDownList2"runat="server"DataSourceID="ST"DataTextField="SpecificTarget"DataValueField="SpecTargetID"SelectedValue='<%# Bind("STGoal") %>'>
</asp:DropDownList>
<asp:CascadingDropDownID="DropDownList2_CascadingDropDown"runat="server"Enabled="True"ServiceMethod="GetDropDownContents"TargetControlID="DropDownList2"UseContextKey="True"Category="Tasks"EmptyText="Select LT Goal">
</asp:CascadingDropDown>
<br/>
Comments:
<asp:TextBoxID="CommentsTextBox"runat="server"Text='<%# Bind("Comments") %>'/>
<br/>
<asp:LinkButtonID="InsertButton"runat="server"CausesValidation="True"CommandName="Insert"Text="Insert"/>
&nbsp;<asp:LinkButtonID="InsertCancelButton"runat="server"CausesValidation="False"CommandName="Cancel"Text="Cancel"/>
<asp:SqlDataSourceID="LT"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"SelectCommand="SELECT [LTGoalID], [LTGoal] FROM [TblGoalsLT]">
</asp:SqlDataSource>
<asp:SqlDataSourceID="ST"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"SelectCommand="SELECT [SpecTargetID], [SpecificTarget] FROM [TblGoalsST] WHERE ([MeasureID] = @MeasureID)">
<SelectParameters>
<asp:ControlParameterControlID="DropDownList1"Name="MeasureID"PropertyName="SelectedValue"Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSourceID="Tasks"runat="server"ConnectionString="<%$ ConnectionStrings:anthemprodConnectionString %>"InsertCommand="INSERT INTO [TblProduction] ([LTGoal], [STGoal], [Comments]) VALUES (@LTGoal, @STGoal, @Comments)">
<InsertParameters>
<asp:ParameterName="LTGoal"Type="Int32"/>
<asp:ParameterName="STGoal"Type="Int32"/>
<asp:ParameterName="Comments"Type="String"/>
</InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

  #5 (permalink)  
Old July 28th, 2010, 06:29 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
Is there any help you can provide in how I complete that code?
Isn't that explained exactly in the article I linked to? That example uses a typed dataset to query the database in the GetColorsForModel method.

You can't use the SqlDataSource controls to return data in a web service method.

Cheers,

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!
  #6 (permalink)  
Old July 28th, 2010, 06:30 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

BTW: the code is still messed up; notice how all spaces are gone.

Cheers,

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!
  #7 (permalink)  
Old July 30th, 2010, 01:35 AM
Authorized User
 
Join Date: Jan 2009
Posts: 25
Thanks: 3
Thanked 1 Time in 1 Post
Default

Well I have tried to use the template for make model and color and just work it backward, but I am still getting an error. I basicallly copied to code and the asp pages but I can post if necessage. I am getting the following error:

Compiler Error Message: CS0246: The type or namespace name 'CarsTableAdapters' could not be found (are you missing a using directive or an assembly reference?)

Can anyone tell me where this needs to be loaded?

Code:
[WebMethod]
publicCascadingDropDownNameValue[] GetMakes(
string knownCategoryValues,
string category)
{
CarsTableAdapters.MakeTableAdapter makeAdapter =
new CarsTableAdapters.MakeTableAdapter();
Cars.MakeDataTable makes = makeAdapter.GetMakes();
List<CascadingDropDownNameValue> values =
newList<CascadingDropDownNameValue>();
foreach (DataRow dr in makes)
{
string make = (string)dr["Make"];
int makeId = (int)dr["MakeID"];
values.Add(newCascadingDropDownNameValue(
make, makeId.ToString()));
}
return values.ToArray();
}
  #8 (permalink)  
Old July 30th, 2010, 02:13 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
Also, can you repost your relevant code and then use the Remove Text Formatting button on the toolbar before you wrap the code in code tags (also on the toolbar). Because of a bug in this form, code pasted from Visual Studio gets messed up real bad.
Please use the Remove Text Formatting button when you post code, or else it's almost impossible to read it. Notice how many spaces are gone.....

In the article, they are using a typed DataSet: http://www.google.com/#hl=en&source=...+typed+dataset which is used to access the database. Instead of a typed dataset, you could also use other technologies such as Linq to SQL or the Entity Framework. Are you already using one of those, or did you use controls such as the SqlDataSource control only? Either way, in the service method you need to some way to access a database and get the "second level" data based on what was passed to your GetMakes method.

You may want to get a copy of my book Beginning ASP.NET 3.5 in C# and VB or the more recent Beginning ASP.NET 4 in C# and VB (see my signature for links). Besides explaining how to work with data and databases in ASP.NET in general, there's a whole chapter on LINQ to SQL (3.5 version) and Entity Framework (4.0) that shows you how you could access the database using these technologies from code which makes it easy to accomplish the task you're working on right now.

Cheers,

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!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
dealing with drop downs lisabb ASP.NET 2.0 Basics 5 May 28th, 2007 12:29 AM
dependent drop downs p2pMember ASP.NET 1.0 and 1.1 Professional 0 July 19th, 2006 05:25 AM
Drop downs for choosing # of pages gilgalbiblewheel Classic ASP Databases 3 October 21st, 2004 03:12 PM
Day of week Drop Downs jeffbarclay Javascript How-To 5 November 17th, 2003 08:28 PM



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


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