Hello All,
I am using jquery ajax to populate a whole form on selection of an item. The form contains 4-5 dropdown lists and 3 checkbox list and few textboxes. So, I planned to bind the values using c# from code behind instead of writing fucntions in jquery itself for each dropdowns /
The scenario has been explained as below with the help of a sample:
testForm.aspx page
function shw(id) {
alert(id);
$.ajax({
type: "post",
url: "testForm.aspx/popu",
data: '{"uid" : ' + id + '}',
contentType: "application/json; charset=utf-8",
success: function (resp) {
alert(resp.d);
},
error: function (xhr, errorType, exception) {
alert(errorType + " | " + exception);
}
});
}
<asp:ListBox ID="ListBox1" runat="server" onchange="shw(this.value);">
<asp:ListItem Text="A" Value="1"></asp:ListItem>
<asp:ListItem Text="B" Value="2"></asp:ListItem>
<asp:ListItem Text="C" Value="3"></asp:ListItem>
<asp:ListItem Text="D" Value="4"></asp:ListItem>
<asp:ListItem Text="E" Value="5"></asp:ListItem>
</asp:ListBox>
testForm.aspx.cs page
[WebMethod]
public static void popu(string uid)
{
using (testForm obj = new testForm())
{
obj.bindDrop1();
}
}
private void bindDrop1()
{
DataTable tbl = new DataTable();
tbl.Columns.Add("qid", typeof(Int32));
tbl.Columns.Add("desc");
DataRow dr;
for (int i = 1; i <= 5; i++)
{
dr = tbl.NewRow();
dr["qid"] = i;
dr["desc"] = "Q" + i;
tbl.Rows.Add(dr);
tbl.AcceptChanges();
}
ddl1.DataTextField = "desc";
ddl1.DataValueField = "qid";
ddl1.DataSource = tbl;
ddl1.DataBind();
}
The above bold line in bindDrop1() method throws the error:
"
Object reference not set to an instance of an object.
"
When I looked for "ddl1" object, its showing NULL.
Well, I can understand that the object (ddl1) is NULL and so this error is. But, I cannot be able to understand why and how?
Because, the "page_load" event is called for first time and that time only all the controls like ddl1, ddl2 etc. are getting created. So, why its showing NULL?