Showing posts with label cascadingdropdown. Show all posts
Showing posts with label cascadingdropdown. Show all posts

Saturday, March 24, 2012

Question about CascadingDropDown

There have been a few questions on this forum about a very annoying aspect of the DropDownList control:that it does not allow the user to set the height of it's vertical scrollbar.

I have given up trying to change it but am I correct in saying that the CascadingDropDown control of the Ajax Control Toolkit does indeed (drumroll!) have a vertical scrollbar?

I have seen one working example online where it does seem to have a vertical scrollbar:

That is from this video tutorial:

http://aspalliance.com/videos/AtlasCascadingDropDownListControl/AtlasCascadingDropDownListControl_media/AtlasCascadingDropDownListControl.wmv

I d'loaded the code for the code for the above example but I wasn't able to get it working (don't ask).

In any case it's not really what I want as it requires an ASP DropDownList control to work. Most examples of the CascadingDropDown seem to be used in this way (ie it is populated when an item is selected first from an ASP DropDownList control).

Is there any working example of a CascadingDropDown where it connects directly to a table itself?

I would greatly appreciate any guidance, thank you.

set the height property of the Dropdown list, then you will get a scroll bar if the contents are more then the height.


Tried that already, doesn't work:

 <asp:DropDownList ID="DrCountries" runat="server" Width="124px" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Name" Height="20px">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CRS_SQLConnectionString%>"
SelectCommand="Load_Countries" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

I just basically want to use the CascadingDropDown control as it has a vertical scrollbar, that is the only reason. I don't need the control to be Ajax-enabled.

The above example in my first post is what I want except I don't want my CascadingDropDown to react to a DropDownList control, I want it to be bound directly to a stored procedure.

Edit:I am now thinking it would be easier to populate the CascadingDropDown from an xml file rather than a database table? Is this possible?


Hi Fonzi,

Based on your description, I think you should better use DropDownExtender instead of CascadingDropDown. CascadingDropDown is used to do cascading tasks. Here is the sample for DropDownExtender. You can do some modifications based on it.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <style> .DropDownList { border-bottom:dotted 1px #006699; cursor:pointer; color:Maroon } </style></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Label ID="TextLabel" runat="server" Text="Select your favorite exotic ice-cream flavor" Style="display: block; width: 300px; padding: 2px; padding-right: 50px; font-family: Tahoma; font-size: 11px;" /> <asp:Panel ID="DropPanel" runat="server" CssClass="ContextMenuPanel" Style="display: none; visibility: hidden; height:40px; width:120px;overflow:auto;"> <li class="DropDownList">11111</li> <li class="DropDownList">11111</li> <li class="DropDownList">11111</li> <li class="DropDownList">11111</li> <li class="DropDownList">11111</li> <li class="DropDownList">11111</li> </asp:Panel> <ajaxToolkit:DropDownExtender runat="server" ID="DDE" BehaviorID="DDEBID" TargetControlID="TextLabel" DropDownControlID="DropPanel" /> </form></body></html>

Best regards,

Jonathan


Jonathon, I actually fixed it by using a third party control from a company called Telerik. It has a combo box with a drop down menu that has a vertical scrollbar.

The height of the scrollbar can be adjusted.

I still can't believe this is not possible with the combo box control in ASP.NET


Hi Fonzi,

I have spent serval hours to find out a solution. But unfortunately I was failed to get a better one. Also I had a glance of the Telerik DropDownList, based on my experience, I think they use Div or similar. Generally, we can do most effect that you want by using DIV + CSS. You can find tons of samples online.

I hope this help.

Best regards,

Jonathan


Jonathan Shen – MSFT:

Hi Fonzi,

I have spent serval hours to find out a solution. But unfortunately I was failed to get a better one. Also I had a glance of the Telerik DropDownList, based on my experience, I think they use Div or similar. Generally, we can do most effect that you want by using DIV + CSS. You can find tons of samples online.

I hope this help.

Best regards,

Jonathan

Heh, tell me about it! The Telerik control works fine. I haven't put it up online yet but here's what it looks like:


Hi Fonzi,

Here is the working sample you that you can reference.

<script language="JavaScript" type="text/JavaScript"><!--function MM_reloadPage(init) { //reloads the window if Nav4 resized if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) { document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();}MM_reloadPage(true);//--></script><style type="text/css">.divMain{ border-style:solid; border-width:1px; border-color:#CCCCCC; position:absolute; left:72px; top:36px; width:157px; height:25px; z-index:1; overflow: visible;}.divText{ border-style:none; border-width:1px; border-color:#CCCCCC; position:absolute; left:76px; top:39px; width:125px; height:15px; z-index:2; font-size:x-small; font-family:Tahoma; color:#0000FF;}.content{ border-style:solid; border-width:1px; border-color:#CCCCCC; position:absolute; left:72px; top:60px; width:157px; height:35px; z-index:3; overflow:auto; visibility: hidden;}.spanCss{ width:100%; color:#666666; font-family:Tahoma; font-size:x-small; cursor:hand;}.button{ BORDER-RIGHT: #7b9ebd 1px solid; PADDING-RIGHT: 5px; padding-top:2px; BORDER-TOP: #7b9ebd 1px solid; PADDING-LEFT: 5px; FONT-SIZE: 10pt; FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#cccccc); BORDER-LEFT: #7b9ebd 1px solid; CURSOR: hand; BORDER-BOTTOM: #7b9ebd 1px solid}</style><body><div id="divMain" class="divMain"></div><div id="divText" class="divText"></div> <div id="content" class="content" onClick="onClickContent('content')" > </div><div id="button" onClick="onClickBtn('content')" style="border-left-style:solid;border-left-width:1px;border-left-color:#CCCCCC;cursor:hand;position:absolute; left:207px; top:36px; width:15; height:25px; z-index:4;"><img src="arrow1.jpg" width="15" height="15" hspace="3" vspace="5"></div><div id="Layer1" style="position:absolute; left:285px; top:37px; width:292px; height:103px; z-index:5"> <form name="form1" method="post" action=""> <input class="button" name="button" type="button" id="button" value="Add New Item" onClick="addItem()"> <input type="text" id="textfield" name="textfield" value=""> </form></div><script language="javascript">var flag;flag=false;function onSelected(obj){ document.getElementById('divText').innerText=obj.innerText;}function onClickBtn(obj){ if (flag==false) { document.getElementById(obj).style.visibility="visible"; flag=true; } else { document.getElementById(obj).style.visibility="hidden"; flag=false; }}function onClickContent(obj){ document.getElementById(obj).style.visibility="hidden"; flag=false;}function hiddenDiv(){ if (flag==true) document.getElementById('content').style.visibility="hidden";}function changeColor(obj){obj.style.backgroundColor="#ccc";}function changeColorBack(obj){obj.style.backgroundColor="";}function addItem(){ if (document.getElementById('textfield').value=="") { alert("input data first!"); return ; } var str; var value; value=document.getElementById('textfield').value; str="<span onClick='onSelected(this)' "; str+="class='spanCss'"; str+="onMouseMove='changeColor(this)' onMouseOut='changeColorBack(this)'>"+value+"</span>";// alert(str); document.getElementById('content').innerHTML+=str;}//document.onclick=hiddenDiv;</script></body> 

I hope this help.

Best regards,

Jonathan

question about CascadingDropDown

Hi!

I've started to learn AJAX technology recently and I have such questions about using CDDL:

1. Is it only possible to use it with web services? How could I adopt my present bunch "DDL-ObjectDatasource" ?

2.I've discovered that by simply putting set of my dropdownlists into UpdatePanel makes them to work without pagepostback. Than another question appears - if it works with default AJAX mechanism, why was CascadingDropDown extender invented? What is its particular destination or distinct functionality that it provides?

No ideas or no one faced the same?

1) You need to use WebServices. The interface is simple to use with an existing datasource though. Just query the data and create the value list. It's super-flextible for whatever data-access mechanism you're using. See the walkthrough in the SampleWebSite for an example. CCD isn't a straight databinding, it's a dynamic databinding based on a the values of the parent drop down.

2) The postback isn't the issue. You're still getting a post-back inside the UpdatePanel, it's just not affecting the whole page. The issue with the CCD is the linking of several dropdowns to navigate through a virtual tree of possible values.

Wednesday, March 21, 2012

Question: Using CascadingDropDown with OleDb

I'm curious. Do youhave to use a Web Service when trying to pull items for a cascading drop down from a database or is there another (perhaps simpler) way? I'd love to be more enlightened on the subject. Thanks in advance.

Hi Venom,

VenomZ302:

Using CascadingDropDown with OleDb

I have made a sample. Hope it helps.

ASPX:

<%@. Page Language="C#" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!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 id="Head1" runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> State:<asp:DropDownList runat="server" ID="dlState"> </asp:DropDownList> City: <asp:DropDownList runat="server" ID="dlCity"> </asp:DropDownList> <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="dlState" Category="State" PromptText="Select a state" ServicePath="../WebService/CityServiceOledb.asmx" ServiceMethod="GetStates" LoadingText="Load Text..."> </cc1:CascadingDropDown> <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="dlCity" ParentControlID="dlState" Category="City" PromptText="Select a city" ServicePath="../WebService/CityServiceOledb.asmx" ServiceMethod="GetCities" LoadingText="Load Text..."> </cc1:CascadingDropDown> </form></body></html>

WebService:

<%@. WebService Language="C#" Class="CityServiceOledb" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Specialized;
using System.Collections.Generic;
using AjaxControlToolkit;
using System.Data.OleDb;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class CityServiceOledb : System.Web.Services.WebService {

protected OleDbConnection cn;
protected OleDbCommand cm;
protected OleDbDataReader myReader;
private string strSql = "";
//private string conStr = "data source=TASKONE\\SQLEXPRESS;initial catalog=TestDB;user id=sa;pwd=password;Pooling=True; Min Pool Size=0; Max Pool Size=1000; Connection Lifetime=30;";

//private string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWork\\DB\\YourDBName.mdb";
//using Microsoft Office2007
private string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\MyWork\\DB\\Database1.accdb";
public CityServiceOledb() { }

[WebMethod]
public CascadingDropDownNameValue[] GetStates(string knownCategoryValues, string category)
{
strSql = "SELECT * FROM tb_State";
GetDr(strSql);
List<CascadingDropDownNameValue> myList = new List<CascadingDropDownNameValue>();
while (myReader.Read()) {
myList.Add(new CascadingDropDownNameValue(myReader["StateName"].ToString(), myReader["StateID"].ToString()));
}
myReader.Close();
return myList.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int stateID;
if (!kv.ContainsKey("State") || !Int32.TryParse(kv["State"], out stateID))
{
return null;
}
List<CascadingDropDownNameValue> myList = new List<CascadingDropDownNameValue>();
strSql = "SELECT * FROM tb_City WHERE StateID=" + stateID.ToString();

GetDr(strSql);
while (myReader.Read())
{
myList.Add(new CascadingDropDownNameValue(myReader["CityName"].ToString(), myReader["CityID"].ToString()));
}
myReader.Close();
return myList.ToArray();
}

#region DB Operate
public void Open()
{
if (cn == null)
{
cn = new OleDbConnection(conStr);
cn.Open();
}
}
public void Close() {
if (cn != null)
{
cn.Close();
cn = null;
}
}

public OleDbDataReader GetDr(string strSql) {
Open();
cm = new OleDbCommand(strSql,cn);
myReader = cm.ExecuteReader();
//myReader = cm.ExecuteReader();
return myReader;
}
#endregion DBOperate

}

Note: You should add two tables with the name "tb_State" and "tb_City" into your Database ,which is used in your project , before you using this demo.

Table "tb_State" has two fields with the name "StateID"(primarykey) and "StateName"(Text).

Table "tb_City" has three fields with the name "CityID"(primarykey)、"StateID"(Number) and "CityName"(Text).

VenomZ302:

I'm curious. Do youhave to use a Web Service when trying to pull items for a cascading drop down from a database or is there another (perhaps simpler) way? I'd love to be more enlightened on the subject. Thanks in advance.

We are not limited to use Web Service. Sometimes, we may add the implement code into our *.cs file directly. Here is the sample:

using System;using System.Web.Services;using AjaxControlToolkit;public partialclass CascadingDropDown_CascadingDropDown : CommonPage{protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e) {// Get selected values } [WebMethod] [System.Web.Script.Services.ScriptMethod]public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues,string category) {
 //obtain data from Database.
 return;
 }}
Note: We needn't to set the ServicePath property for the control in this case. Just like below.
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
Details please dip into the Ajax ControlToolkit source code.

If I misunderstood you, please let me know.


Thanks for the information. I really appreciate it. Now, however, I've progressed to the point where I need to understand more of the error codes that are given. Most specifically, error 12030, as found in my thread here: http://forums.asp.net/t/1138732.aspx

Thanks again.


Hi Venom,

Error code: 12030

ERROR_INTERNET_CONNECTION_ABORTED
The connection with the server has been terminated.

In your case, it seems like your webservice doesn't work properly.I suggest that you should add break points into your webservice and debug step-by-step.

Note: If you made some modifications in your webservice and before you debug , please reset your aplication by saving your web.config file.Good luck! Big Smile