使用ASP.NET中的Web Service在网格视图中填充级联DropdownList时出错 [英] Error Populating Cascading DropdownList inside a gridview using Web Service in ASP.NET

查看:62
本文介绍了使用ASP.NET中的Web Service在网格视图中填充级联DropdownList时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在gridview中实现级联下拉列表。如下所示



我的.aspx页面





 <%@     Page    语言  =   C#    AutoEventWireup  < span class =code-keyword> =  true    CodeBehind   =  available_services.aspx.cs    MasterPageFile   = 〜/ Forms / Home.Master     继承  =  HospitalManagementSystem.Forms.available_services   %>  
<%@ 注册 汇编 = AjaxControlToolkit 命名空间 = AjaxControlToolkit TagPrefix = cc1 %>

< asp:内容 ID = Content1 ContentPlaceHolderID = ContentPlaceHolder1 runat = server >
< script type = text / javascript src = scripts / jquery-1.10.1.min。 js > < / script >

< div >

< ajaxToolkit:ToolkitScriptManager ID = ToolkitScriptManager1 runat = server >
< / ajaxToolkit:ToolkitScriptManager >

< asp:GridView ID = GridView1 runat = server AutoGenerateColumns = false >
< >
< asp:BoundField HeaderText = 名称 DataField = ContactName / >
< asp:TemplateField HeaderText = 国家/地区 >
< ItemTemplate >
< asp:DropDownList ID = ddlCountries runat = server 宽度 = 150 >
< / asp:DropDownList > ;
< cc1:CascadingDropDown ID = cdlCountries TargetControlID = ddlCountries <跨度class =code-attribute>
PromptText = 选择国家/地区 PromptValue =

ServicePath = ServiceCS.asmx ServiceMethod = GetCountries

runat = 服务器 类别 = 国家/地区 / >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 城市 >
< ItemTemplate >
< asp:DropDownList ID = < span class =code-key单词> ddlCities runat = server 宽度 < span class =code-keyword> = 150 >
< / asp:DropDownList >
< cc1:CascadingDropDown ID = cdlCities < span class =code-attribute> TargetControlID = ddlCities PromptText = 选择城市 PromptValue =

< span class =code-attribute> ServicePath = ServiceCS.asmx ServiceMethod = GetCities runat = server

< span class =code-attribute> 类别 = 城市 ParentControlID = d dlCountries / >
< / ItemTemplate >
< / asp:TemplateField >
< / Columns >
< / asp:GridView >
< / div < span class =code-keyword>>
< / asp:Content >







C#代码背后



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

使用 System.Data;
使用 System.Configuration;
使用 System.Data.SqlClient;
使用 System.Web.Services;
使用 AjaxControlToolkit;


namespace Testing.Forms
{

public partial class available_services:System.Web.UI.Page
{

受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack) // 仅在第一次加载时执行此操作
{
BindMajorHeadDropdown();
GridView1.DataSource = GetData( SELECT TOP 10 ContactName,Country FROM Customers);
GridView1.DataBind();
}
}

私有 DataSet GetData( string 查询)
{
string conString = ConfigurationManager.ConnectionStrings [ conString]的ConnectionString。
SqlCommand cmd = new SqlCommand(query);
使用(SqlConnection con = new SqlConnection(conString))
{
使用(SqlDataAdapter sda = new SqlDataAdapter())
{
cmd .Connection = con;

sda.SelectCommand = cmd;
使用(DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
}







ServiceCS.asmx(网络服务)



 使用系统; 
使用 System.Collections.Generic;
使用 System.Web;
使用 System.Web.Services;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 AjaxControlToolkit;



命名空间测试
{

/// < summary >
/// ServiceCS的摘要描述
/// < / summary >
[ WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem( false )]

// 到使用ASP.NET AJAX从脚本调用此Web服务,取消注释以下行。
[System.Web.Script.Services.ScriptService]

public class ServiceCS:System.Web.Services.WebService
{

[WebMethod]
public CascadingDropDownNameValue [] GetCountries( string knownCategoryValues)
{
string query = < span class =code-string> SELECT DISTINCT Country FROM Customers;
List< CascadingDropDownNameValue> countries = GetData(查询);
return countries.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue [] GetCities( string knownCategoryValues)
{
string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)[ 国家];
string query = string .Format( SELECT DISTINCT City FROM Customers WHERE Country ='{0}',country);
List< CascadingDropDownNameValue> cities = GetData(查询);
return cities.ToArray();
}

private 列表< CascadingDropDownNameValue> GetData( string query)
{
string conString = ConfigurationManager.ConnectionStrings [< span class =code-string> conString]。ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List< CascadingDropDownNameValue> values = new 列表< CascadingDropDownNameValue>();
使用(SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
使用(SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add( new CascadingDropDownNameValue
{
name = reader [ 0 ]。ToString(),
value = reader [ 0 ]。ToString()
});
}
reader.Close();
con.Close();
返回值;
}
}
}
}
}







当我尝试运行程序时,我在下拉列表中没有得到任何内容,而是得到[方法错误500]。请帮助我该怎么做。

解决方案

你可以试试这个链接...... http: //stackoverflow.com/questions/13984127/validation-for-checked-items-in-gridview [ ^ ]


这可能是多种原因之一。

首先尝试用于级联下拉菜单的服务调用URL,例如



ServicePath =ServiceCS.asmx修改为完全限定路径。



检查网络服务,确保它正常工作

请尝试浏览它,以便你可以获得像http:// localhost / path
这样的完整网址


接下来,它可能是由太多数据引起的在Web服务调用后面的SQL查询中返回。为此,增加web.config中返回的数据的大小限制。



希望这有助于...

I am going to implement a cascading dropdownlist inside a gridview. Like given below

My .aspx Page


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="available_services.aspx.cs" MasterPageFile="~/Forms/Home.Master"  Inherits="HospitalManagementSystem.Forms.available_services" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 <script type="text/javascript" src="scripts/jquery-1.10.1.min.js"></script>

<div>

    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1"  runat="server">
    </ajaxToolkit:ToolkitScriptManager>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="Name" DataField="ContactName" />
            <asp:TemplateField HeaderText="Country">
                <ItemTemplate>
 <asp:DropDownList ID="ddlCountries" runat="server" Width="150">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cdlCountries" TargetControlID="ddlCountries" PromptText="Select Country" PromptValue=""

                        ServicePath="ServiceCS.asmx" ServiceMethod="GetCountries"

                         runat="server" Category="Country" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlCities" runat="server" Width="150">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cdlCities" TargetControlID="ddlCities" PromptText="Select City" PromptValue=""

                        ServicePath="ServiceCS.asmx" ServiceMethod="GetCities"  runat="server"

                        Category="City" ParentControlID="ddlCountries" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 </div>
</asp:Content>




C# Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
using AjaxControlToolkit;


namespace Testing.Forms
{

public partial class available_services : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) //Only do this first time it's loaded
        {
            BindMajorHeadDropdown();
            GridView1.DataSource = GetData("SELECT TOP 10 ContactName, Country FROM Customers");
            GridView1.DataBind();                              
        }
    }

private DataSet GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }
}




ServiceCS.asmx (Web Service)

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;



namespace Testing
{

/// <summary>
/// Summary description for ServiceCS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

// 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 ServiceCS : System.Web.Services.WebService
{

    [WebMethod]
    public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues)
    {
        string query = "SELECT DISTINCT Country FROM Customers";
        List<CascadingDropDownNameValue> countries = GetData(query);
        return countries.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetCities(string knownCategoryValues)
    {
        string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["Country"];
        string query = string.Format("SELECT DISTINCT City FROM Customers WHERE Country = '{0}'", country);
        List<CascadingDropDownNameValue> cities = GetData(query);
        return cities.ToArray();
    }

    private List<CascadingDropDownNameValue> GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        using (SqlConnection con = new SqlConnection(conString))
        {
            con.Open();
            cmd.Connection = con;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    values.Add(new CascadingDropDownNameValue
                    {
                        name = reader[0].ToString(),
                        value = reader[0].ToString()
                    });
                }
                reader.Close();
                con.Close();
                return values;
            }
        }
    }
}
}




When i try to run the program i get nothing in the dropdownlist instead i get [Method error 500]. Please help me what should i do.

解决方案

you can try with this link...http://stackoverflow.com/questions/13984127/validation-for-checked-items-in-gridview[^]


It can be one of multiple reasons.
First try the service call url used for cascading dropdowns like

ServicePath="ServiceCS.asmx" modify to a full qualified path.

check the web service, make sure it working properly
Please try to browser it so that you can get the full url like http://localhost/path

Then next, it can be caused by too much data being returned in the SQL query behind the web service call.For that,increase the size limit of data returned in web.config .

Hope this helps...


这篇关于使用ASP.NET中的Web Service在网格视图中填充级联DropdownList时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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