Showing posts with label cascadingdropdownlists. Show all posts
Showing posts with label cascadingdropdownlists. Show all posts

Saturday, March 24, 2012

CascadingDropDownList and ModalPopupExtender

I have created a ModalPopupExtender that contains 3 CascadingDropDownLists. The dropdowns work and are populated normally if I don't try and set a value. If I try and set a value for the first dropdown I get an ArgumentOutOfRangeException error. It seems like the value is trying to be set before the dropdown is populated. Does anyone know how I can fix this?

Thanks.

Nevermind. I got it.

CascadingDropDownList and a MySQL database

How to use CascadingDropDownLists bound to a MySQL-database? I want a parent DDList with items selected from one table and a second DDlist with items selected from another table based on the value selected in the first one. But how to write the asmx-file?

Hi,

First, you can refer to this documentation for how to use it with a database. http://ajax.asp.net/ajaxtoolkit/Walkthrough/CCDWithDB.aspx

Then, as far as I know, MySql provides db driver for .net which can be used in a similar way as the build-in ADO.NET does. You can download it from their website and try to implement it according to the above documentation.

Hope this helps.

CascadingDropDownList - Am i being a complete tool?

Hey,

Trying to get my head round these CascadingDropDownLists but i am continually returned with a [Method Error 500] error.

This is my code for 'Default.aspx'


<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
<%@dotnet.itags.org. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"
Category="Author"
TargetControlID="DropDownList1"
ServiceMethod="FillMe"
ServicePath="WebService.asmx"
PromptText="Please select a category"
>
</ajaxToolkit:CascadingDropDown>
</form>
</body>
</html>

And my webservice, WebService.asmx, is as follows:

using System;using System.Configuration;using System.Web;using System.Collections;using System.Collections.Generic;using System.Collections.Specialized;using System.Web.Services;using System.Web.Services.Protocols;using AjaxControlToolkit;using System.Data;using System.Data.SqlClient;/// <summary>/// Summary description for WebService/// </summary>[WebService(Namespace ="http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WebService : System.Web.Services.WebService { [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public static CascadingDropDownNameValue[] FillMe(string knownCategoryValues, string category) { string connString = ConfigurationManager.ConnectionStrings["tenderlistDB"].ConnectionString; SqlConnection conn = new SqlConnection(connString); DataTable dt = new DataTable(); string Sql = "SELECT [ID], [Name] FROM tblCategories WHERE ParentID=@dotnet.itags.org.ParentID ORDER BY [Name] ASC"; try { conn.Open(); SqlCommand comm = new SqlCommand(Sql, conn); comm.Parameters.AddWithValue("@dotnet.itags.org.ParentID", "0"); SqlDataAdapter adapt = new SqlDataAdapter(comm); adapt.Fill(dt); } finally { conn.Close(); } List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); for (int i = 0; i < dt.Rows.Count; i++) { CascadingDropDownNameValue CCD = new CascadingDropDownNameValue(); CCD.name = dt.Rows[i]["Name"].ToString(); CCD.value = dt.Rows[i]["ID"].ToString(); values.Add(CCD); }return values.ToArray(); }}

Anyone have an idea as to why it's just not working?

Cheers,

Sean


Most likely you have an exception being thrown in your WebMethod. Set a breakpoint in it and use the site in Debug mode. Step through the WebMethod and see if you are getting an exception. This is usually the case for me. The other thing I have seen is too much information being returned, but I think that is another error number. The Execption will not be returned by the Web Service, so AJAX throws a 500 error up at you.


Thanks for getting back to me.

I've never really used the debug function, but i stuck some breakpoints in the code and hit F5, and it all ran ok.

If i run it in debug mode, instead of Error method 500, i get an Error Method 12031...Could that be anything?

In terms of the SQL, it returns 12 rows when run through Sql Query Analyzer.

Appreciate your help!


Been thinking about this a little over lunch. Have you marked your Web Service and Web methods with AJAX attributes?

The Class should have this attribute:

<System.Web.Script.Services.ScriptService()> _

The Web Method this one:

<System.Web.Script.Services.ScriptMethod()> _

So a method would resemble the following:

<WebMethod()> _

<System.Web.Script.Services.ScriptMethod()> _

PublicFunction GetMarkets(ByVal knownCategoryValuesAsString, _

ByVal categoryAsString)As AjaxControlToolkit.CascadingDropDownNameValue()

Dim kvAs StringDictionary = _

AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

Dim RegionIdAsInteger

IfNot kv.ContainsKey("Region")OrElseNot Int32.TryParse(kv("Region"), RegionId)Then

ReturnNothing

EndIf

Dim lcAsNew LocMarketController

Dim itemListAs LocMarketList = lc.GetLocMarketByRegion(RegionId)

Dim valueAsNew List(Of AjaxControlToolkit.CascadingDropDownNameValue)

ForEach itemAs LocMarketInfoIn itemList

value.Add(New AjaxControlToolkit.CascadingDropDownNameValue(item.Market, item.MarketId))

Next

Return value.ToArray

EndFunction


Hey,

Tried that but still no joy. Starting to get frustrating now hehe, anyone else got any ideas?

Cheers for the help!


Hey,


Just to let you all know i got it working...i rewrote it this morning when i got into work and its all working fine...not sure why though, i have a feeling I wasn't passing through the correct parameters, so when the


'StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);'

line was hit, the method would break!

Thanks for all the help.