Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
JavaScript
function shw(id) {
            alert(id);

            $.ajax({
                type: "post",
                url: "testForm.aspx/popu",
                data: '{"uid" : ' + id + '}',
                //dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (resp) {
                    alert(resp.d);
                },
                error: function (xhr, errorType, exception) {
                    alert(errorType + " | " + exception);
                }
            });
        }


ASP.NET
<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
C#
[WebMethod]
        public static void popu(string uid)
        {
            //JavaScriptSerializer js = new JavaScriptSerializer();
            //return js.Serialize("Value = " + ID);

            //return "Value = " + uid;

            using (testForm obj = new testForm())
            {
                obj.bindDrop1();
            }
        }

C#
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?
Posted
Updated 11-Aug-15 21:29pm
v2

1 solution

A static method cannot access non-static class level members and also static method doesn't have access to members like UI controls because it is not a part of that instance.
you can get the data from your web service and populate grid using that data by your jquery method. check below articles for more information
Bind Dataset to ASP.Net GridView using jQuery AJAX[^]
Bind Gridview using jQuery JSON Ajax call in asp.net C#[^]
Bind GridView using jQuery in ASP.NET[^]
 
Share this answer
 
Comments
Member 11072126 12-Aug-15 7:26am    
Thanks Damith. I understood.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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