Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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-------------

C#
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 code is-------------

ASP.NET
<%@ 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.
Posted
Updated 3-Sep-11 1:26am
v2
Comments
Abhinav S 3-Sep-11 7:25am    
What error? You have provided a lot of code but not mentioned where or what the error is.
Try debugging through your source code.
Abhijit Jana 3-Sep-11 15:23pm    
Hey Abhinav. Here is the comments . Moved in comments so that you can get a notification.
--------
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 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.
----------------------------

1 solution

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.
 
Share this answer
 
Comments
Abhijit Jana 3-Sep-11 15:22pm    
Please don't reply as a Solution. This does not help. Just Reply the comment of Abhinav's. This time I am doing it for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900