级联下拉菜单中的问题 [英] Problem in Cascading dropdown

查看:63
本文介绍了级联下拉菜单中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是ajax的新手,我正尝试使用级联下拉列表,但遇到一些错误.我的代码如下.

------------ webservice.cs -------------

Hi all,

I am new to ajax and i was trying to use cascading dropdown but getting some error.my code is as below.

------------webservice.cs-------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Specialized;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    public string str = (@"server=PRINCE\MSSQLSERVER1;database=centuryDB;integrated security=true;");
    //public WebService()
    //{

    //    //Uncomment the following line if using designed components
    //    //InitializeComponent();
    //}

    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownCategories(string knownCategoryValues, string category)
    {
        SqlConnection sqlConn = new SqlConnection(str);
        sqlConn.Open();
        SqlCommand sqlSelect = new SqlCommand("SELECT country_id,Country FROM country_detail", sqlConn);
        sqlSelect.CommandType = System.Data.CommandType.Text;
        SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
        DataSet myDataset = new DataSet();
        sqlAdapter.Fill(myDataset);
        sqlConn.Close();

        List<AjaxControlToolkit.CascadingDropDownNameValue> cascadingValues = new List<AjaxControlToolkit.CascadingDropDownNameValue>();

        foreach (DataRow dRow in myDataset.Tables[0].Rows)
        {
            string categoryID = dRow["country_id"].ToString();
            string categoryName = dRow["Country"].ToString();
            cascadingValues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(categoryName, categoryID));
        }

        return cascadingValues.ToArray();
    }
    [WebMethod]
    public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownProducts(string knownCategoryValues, string category)
    {
        int categoryID;

        StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        categoryID = Convert.ToInt32(categoryValues["country"]);

        SqlConnection sqlConn = new SqlConnection(str);
        sqlConn.Open();
        SqlCommand sqlSelect = new SqlCommand("SELECT state_id,State FROM state_detail where country_id = @country_id", sqlConn);
        sqlSelect.CommandType = System.Data.CommandType.Text;
        sqlSelect.Parameters.Add("@country_id", SqlDbType.Int).Value = categoryID;
        SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
        DataSet myDataset = new DataSet();
        sqlAdapter.Fill(myDataset);
        sqlConn.Close();

        List<AjaxControlToolkit.CascadingDropDownNameValue> cascadingValues = new List<AjaxControlToolkit.CascadingDropDownNameValue>();

        foreach (DataRow dRow in myDataset.Tables[0].Rows)
        {
            string productID = dRow["state_id"].ToString();
            string productName = dRow["State"].ToString();
            cascadingValues.Add(new AjaxControlToolkit.CascadingDropDownNameValue(productName, productID));
        }

        return cascadingValues.ToArray();

    }

}



----------- default.aspx代码是-------------



-----------default.aspx code is-------------

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ToolkitScriptManager ID="ScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <table>
        <tr>
        <td>select country:
        <asp:DropDownList ID="ddlcountry" runat="server"></asp:DropDownList>
        </td>
        <td>
            <asp:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="country"  TargetControlID="ddlcountry"

            PromptText="[Select Category]"

            LoadingText="Loading categories..."

            ServicePath="~/WebService.asmx"

            ServiceMethod="GetDropDownCategories">
            </asp:CascadingDropDown>
        </td>
        <td>Select state:
        <asp:DropDownList ID="ddlstate" runat="server" ></asp:DropDownList>
        </td>
        <td>
            <asp:CascadingDropDown ID="CascadingDropDown2" runat="server" Category="state" TargetControlID="ddlstate"

            ParentControlID="ddlcountry"

            PromptText="Select ...."

            LoadingText="Loading....."

            ServicePath="~/WebService.asmx"

            ServiceMethod="GetDropDownProducts">
            </asp:CascadingDropDown>
        </td>
        <td><asp:Button ID="btnok" Text="ok" runat="server" onclick="btnok_Click" /></td>
        <td><asp:Label ID="lblresult" runat="server"></asp:Label></td>
        </tr>

        </table>

    </div>
    </form>
</body>
</html>







Can you please help me how to solve this.

推荐答案

hi Abhinav,

当我按下表单上的确定"按钮时,错误如下:

-------------------------------------------------
无效的回发或回调参数.使用< pages enableeventvalidation ="true">启用事件验证.在配置中或<%@页面EnableEventValidation ="true"%>在页面中.为了安全起见,此功能验证回发或回调事件的参数源自最初呈现它们的服务器控件.如果数据有效且预期,请使用ClientScriptManager.RegisterForEventValidation方法以注册回发或回调数据以进行验证.
-------------------------------------------------- -

请帮助我.
hi Abhinav,

The error is as follows when i press the ok button on my form

-------------------------------------------------
Invalid postback or callback argument. Event validation is enabled using <pages enableeventvalidation="true"> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
----------------------------------------------------

please help me in this.


这篇关于级联下拉菜单中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆