使用AJAX和PageMethod通过另一个下拉列表填充下拉列表 [英] Populate a dropdownlist via another dropdownlist using AJAX and PageMethod

查看:49
本文介绍了使用AJAX和PageMethod通过另一个下拉列表填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



遇到一个奇怪的问题,希望这里的专家能够提供帮助。

我想尝试当下拉列表中的值ddlCountry发生变化时,填充下拉列表即ddlStates。我不想使用下拉列表和AutoPostBack属性的OnSelectedIndexChanged事件。



因此,我将ScriptManager实例从工具箱的AJAX扩展部分删除到Default.aspx页面,并将EnablePageMethods属性设置为true。

接下来,我为ddlCountry编写了一个名为onchange的事件,调用名为ddlCountry_Changed()的Javascript函数。



在ddlCountry_Changed()中,我试图将名为populateStatesinDefault()的服务器端方法调用为PageMethods.populateStatesinDefault ()。



控件进入服务器端功能。问题是,为了使PageMethods正常工作,我们需要在方法上添加[System.Web.Services.WebMethod()],我们需要将这个方法声明为静态。



在静态方法中,我们不能直接调用非静态成员,我们需要进行对象引用。由于我的所有控件都在部分类_Default中,我正在创建此部分类的对象,并尝试通过此对象访问下拉列表控件。



但令我惊讶的是,我得到一个NullReference异常。通过部分类obj访问时控件ddlCountry显示为null。

以下是我的代码:



Default.aspx:



< head> 
< script language = javascript type = text / javascript >
debugger
function ddlCountry_changed(){
PageMethods.populateStatesinDefault(onsuccess);
// alert('hii');
}
function onsuccess(){
alert(' success');
}
< / script >
< / head >
< body>
< form id = form1 runat = server >
< div>
< h2>让' 使用AJAX填充DropDowns而不使用AutoPostBack< / h2>

< asp:ScriptManager ID =ScriptManager1runat =serverEnablePageMethods =true>
< / asp:ScriptManager>
< asp:DropDownList ID =ddlCountryrunat =serverCssClass =DropDownonchange =ddlCountry_changed()>
< / asp:DropDownList>
< asp:DropDownList ID =ddlStatesCssClass =DropDownrunat =server>
< / asp:DropDownList>
< / body>





Default.aspx.cs:



 使用系统; 
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
DAL obj = new DAL();
DataTable dt = obj.populateCountry();
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = dt.Columns [ 1 ]。ToString();
ddlCountry.DataValueField = dt.Columns [ 0 ]。ToString();
ddlCountry.DataBind();
}
[System.Web.Services.WebMethod()]
public static void populateStatesinDefault()
{
_Default defObj = new _Default ();
int country = int .Parse(defObj.ddlCountry.SelectedItem.Value) ; // 此处出现NullReference错误
DAL obj2 = new DAL();
DataTable dt = obj2.populateStates(country);
defObj.ddlStates.DataSource = dt;
defObj.ddlStates.DataTextField = dt.Columns [ 1 ]。ToString();
defObj.ddlStates.DataValueField = dt.Columns [ 0 ]。ToString();
defObj.ddlStates.DataBind();
}
}





数据访问层:



 使用系统; 
使用 System.Data;
使用 System.Configuration;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Data.SqlClient;
public class DAL
{
SqlConnection xconn = new SqlConnection(ConfigurationManager.ConnectionStrings [ ConnectionStrings]的ToString());
public DataTable populateCountry()
{
SqlDataAdapter da = new SqlDataAdapter( select * from Development.dbo.Country,xconn);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public DataTable populateStates( int selectedCountry)
{
SqlDataAdapter da = new SqlDataAdapter( select *来自Development.dbo.States,其中CountryID =' + selectedCountry + ',xconn );
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}





我错过了什么吗?我认为页面中的控件位于_Default类下,因此如果我们创建_Default类的对象并尝试访问静态方法中的控件,它应该可以工作,但奇怪的是它不是。

任何帮助或指示将不胜感激。



-Regards

Anurag

解决方案

使用ajax CascadingDropDown控件


Scroll-to-Load-Data-webservice-javascript

在此使用此引用,看看如何使用html标签在页面上加载数据而不回发希望它能帮到你:)






你实例化_Default类的对象,当你实例化一个类时,新的内存是在堆栈用于新创建的对象,而无需在新对象中输入任何内容,以便您可以使用它。我认为它应该只提供null异常



 _Default defObj =  new  _默认(); 
int country = int .Parse(defObj.ddlCountry.SelectedItem.Value);


Hello people,

Got into a weird problem, hope experts out here would be able to help.
I am trying to populate a dropdownlist namely ddlStates when the values in the dropdownlist ddlCountry changes.I don''t want to use the OnSelectedIndexChanged event of the dropdownlist and AutoPostBack property too.

Hence, I dropped a ScriptManager instance from the AJAX extensions part of the toolbox on to the Default.aspx page and set the EnablePageMethods property as true.
Next, I wrote an event called as onchange for the ddlCountry and called a Javascript function named as ddlCountry_Changed().

In ddlCountry_Changed(), I am trying to call a server-side method named as populateStatesinDefault() as PageMethods.populateStatesinDefault().

The control is going inside that server-side function. The problem is that for PageMethods to be working we need to add [System.Web.Services.WebMethod()] over the method and we need to declare that very method as static.

In static methods, we can''t call non static members directly, we would need to make an object reference. Since all my controls are in partial class _Default, I am creating an object of this partial class and trying to access the dropdownlist controls via this object.

But to my surprise, I am getting a NullReference exception. The control ddlCountry when accessed via partial class obj shows as null.
Below are my codes:

Default.aspx:

<head>
    <script language="javascript" type="text/javascript">
    debugger
        function ddlCountry_changed() {
            PageMethods.populateStatesinDefault(onsuccess);
            //alert('hii');
        }
        function onsuccess() {
            alert('success');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2> Let's populate DropDowns using AJAX without AutoPostBack</h2>

        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
            <asp:DropDownList ID="ddlCountry" runat="server" CssClass="DropDown" onchange="ddlCountry_changed()">
            </asp:DropDownList>
            <asp:DropDownList ID="ddlStates" CssClass="DropDown" runat="server">
            </asp:DropDownList>
</body>



Default.aspx.cs:

using System;
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;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DAL obj = new DAL();
        DataTable dt = obj.populateCountry();
        ddlCountry.DataSource = dt;
        ddlCountry.DataTextField = dt.Columns[1].ToString();
        ddlCountry.DataValueField = dt.Columns[0].ToString();
        ddlCountry.DataBind();
      }
    [System.Web.Services.WebMethod()]
    public static void populateStatesinDefault()
    {
            _Default defObj = new _Default();
            int country = int.Parse(defObj.ddlCountry.SelectedItem.Value);//NullReference error over here
            DAL obj2 = new DAL();
            DataTable dt = obj2.populateStates(country);
            defObj.ddlStates.DataSource = dt;
            defObj.ddlStates.DataTextField = dt.Columns[1].ToString();
            defObj.ddlStates.DataValueField = dt.Columns[0].ToString();
            defObj.ddlStates.DataBind();
    }
}



Data Access Layer:

using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
public class DAL
{
    SqlConnection xconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStrings"].ToString());
    public DataTable populateCountry()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Development.dbo.Country", xconn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
    public DataTable populateStates(int selectedCountry)
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Development.dbo.States where CountryID='"+selectedCountry+"'", xconn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
}



Am I missing something? I think that the controls in the page are under the _Default class and hence if we create an object of _Default class and try to access the controls inside a static method, it should work, but strangely it ain''t.
Any help or pointers would be greatly appreciated.

-Regards
Anurag

解决方案

use ajax CascadingDropDown control


Scroll-to-Load-Data-webservice-javascript
use this reference in this see how html tags are use to load data on page without postback hope it will help you :)


Hi,

you are instantiating object for _Default class, when you are instantiating a class new memory is created in heap for newly created object, without entering anything in new object for that drop down how you can use it. i think it should provide null exception only

 _Default defObj = new _Default();
int country = int.Parse(defObj.ddlCountry.SelectedItem.Value);


这篇关于使用AJAX和PageMethod通过另一个下拉列表填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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