级联下拉列表中的问题 [英] problem in Cascading drop down list
本文介绍了级联下拉列表中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从三天开始就遇到问题了.请解决此问题.我在第一个下拉列表中获取值,但是当我选择第二个下拉列表时却出现"Method Error500".在调试时,我发现问题出在stringdictionary变量中.但是我无法解决这个问题.这个变量中没有CountryId值.请看看我的问题.当我到达第二种方法时也会抛出一些反汇编消息.这是什么?我的代码是:
i am facing problem from three days.please solve this.i am getting values in my first dropdown list but when i select second dropdown list i get "Method Error500".while debugging i see problem is in stringdictionary variable.but iam not able to solve this.CountryId value is not coming in this variable.please see my problem.Also some Disassembly message is thrown when i reach to second method.what is this?my code is:
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDown.aspx.cs" Inherits="CascadingDropDown" EnableEventValidation="false" %>]]>
<![CDATA[<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>]]>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:toolkitscriptmanager id="ToolkitScriptManager1" runat="server" xmlns:asp="#unknown">
</asp:toolkitscriptmanager>
Country:
<asp:dropdownlist id="ddlcountry" runat="server" xmlns:asp="#unknown">
AutoPostBack="True">
</asp:dropdownlist>
<br />
<br />
State:
<asp:dropdownlist id="ddlstate" runat="server" xmlns:asp="#unknown">
onselectedindexchanged="ddlstate_SelectedIndexChanged">
</asp:dropdownlist>
<br />
<asp:cascadingdropdown id="CascadingDropDown1" runat="server" xmlns:asp="#unknown">
TargetControlID="ddlcountry" Category="country"
PromptText="select country" ServiceMethod="GetDropDownContents"
UseContextKey="True">
</asp:cascadingdropdown>
<br />
<asp:cascadingdropdown id="CascadingDropDown2" runat="server" category="state" xmlns:asp="#unknown">
ParentControlID="ddlcountry" PromptText="select state"
ServiceMethod="GetDropDownContents2" TargetControlID="ddlstate"
UseContextKey="True">
</asp:cascadingdropdown>
<asp:updatepanel id="UpdatePanel1" runat="server" updatemode="Conditional" xmlns:asp="#unknown">
<contenttemplate>
<asp:label id="Label1" runat="server" text="Label"></asp:label>
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="ddlstate" eventname="SelectedIndexChanged" />
</triggers>
</asp:updatepanel>
</div><asp:label id="Label2" runat="server" text="Label" xmlns:asp="#unknown"></asp:label>
</form>
</body>
</html>
CODEBEHIND:
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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Services;
public partial class CascadingDropDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public static AjaxControlToolkit.CascadingDropDownNameValue[]
GetDropDownContents(string knownCategoryValues, string category)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
sqlcon.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Country", sqlcon);
da.Fill(dt);
List<ajaxcontroltoolkit.cascadingdropdownnamevalue> values = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>();
for (int i = 0; i < dt.Rows.Count; i++)
{
int CountryId = (int)dt.Rows[i]["countryid"];
string Countryname = (string)dt.Rows[i]["country"];
values.Add(new AjaxControlToolkit.CascadingDropDownNameValue(Countryname, CountryId.ToString()));
}
return values.ToArray();
}
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public static AjaxControlToolkit.CascadingDropDownNameValue[]
GetDropDownContents2(string knownCategoryValues, string category)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
int CountryId;
StringDictionary country = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
foreach (string key in country.Keys)
{
string value = country[key];
CountryId = int.Parse(value.ToString());
}
CountryId = Convert.ToInt32(country.ToString());
sqlcon.Open();
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(" select * from State where countryid = @CountryId", sqlcon);
da1.Fill(dt1);
List<ajaxcontroltoolkit.cascadingdropdownnamevalue> statevalues = new List<ajaxcontroltoolkit.cascadingdropdownnamevalue>();
for (int i = 0; i < dt1.Rows.Count; i++)
{
int stateId = (int)dt1.Rows[i]["stateid"];
string statename = (string)dt1.Rows[i]["state"];
statevalues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(statename.ToString(), stateId.ToString()));
}
return statevalues.ToArray();
}
//return default(AjaxControlToolkit.CascadingDropDownNameValue[]);
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = string.Format("you selected{0} from {1} category.", ddlstate.SelectedItem.Text, ddlcountry.SelectedItem.Text);
}
</ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue></ajaxcontroltoolkit.cascadingdropdownnamevalue>
推荐答案
您能否在此声明之前检查StringDictionary国家/地区是否具有价值.
CountryId = Convert.ToInt32(country.ToString());
为了获得用户选择的值,您必须使用
在下面的语句中使用country ["state"]代替country.ToString().
CountryId = Convert.ToInt32(country.ToString());
Could you please check if the StringDictionary country, has value before this statement.
CountryId = Convert.ToInt32(country.ToString());
Inorder to get the value selected by the user you have to use
country["state"] instead of country.ToString() in the below statement.
CountryId = Convert.ToInt32(country.ToString());
正如你所说,我也尝试过这种方法,但是却得到了同样的错误.我在页面代码后面的代码如下:
as u said,i tried that way also,but getting the same error.my code behind page code is like:
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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Services;
public partial class CascadingDropDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public static AjaxControlToolkit.CascadingDropDownNameValue[]
GetDropDownContents(string knownCategoryValues, string category)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
sqlcon.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select * from Country", sqlcon);
da.Fill(dt);
List values = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
int CountryId = (int)dt.Rows[i]["countryid"];
string Countryname = (string)dt.Rows[i]["country"];
values.Add(new AjaxControlToolkit.CascadingDropDownNameValue(Countryname, CountryId.ToString()));
}
return values.ToArray();
}
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public static AjaxControlToolkit.CascadingDropDownNameValue[]
GetDropDownContents2(string knownCategoryValues, string category)
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ajax"].ConnectionString);
int CountryId;
StringDictionary categoryvalues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
CountryId = Convert.ToInt32(categoryvalues["state"]); //CountryId = Convert.ToInt32(categoryvalues["country"]);
sqlcon.Open();
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(" select * from State where countryid = @ CountryId", sqlcon);
da1.Fill(dt1);
sqlcon.Close();
List statevalues = new List();
for (int i = 0; i < dt1.Rows.Count; i++)
{
int stateid = (int)dt1.Rows[i]["stateid"];
string statename = (string)dt1.Rows[i]["state"];
statevalues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(stateid.ToString(), statename.ToString()));
}
return statevalues.ToArray();
这篇关于级联下拉列表中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文