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);
}
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);
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