p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   Cascading Drop Downs for a novice (http://p2p.wrox.com/showthread.php?t=80246)

mashype July 24th, 2010 01:33 AM

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......[:confused:]

mashype July 27th, 2010 11:41 AM

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! :)

Imar July 27th, 2010 12:09 PM

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

mashype July 27th, 2010 12:27 PM

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>



Imar July 28th, 2010 06:29 AM

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 July 28th, 2010 06:30 AM

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

Cheers,

Imar

mashype July 30th, 2010 01:35 AM

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();
}


Imar July 30th, 2010 02:13 AM

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


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

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