“对象引用未设置为对象的实例” - 使用jquery绑定下拉列表时出现此错误 [英] "Object reference not set to an instance of an object" - getting this error while binding dropdown using jquery

查看:94
本文介绍了“对象引用未设置为对象的实例” - 使用jquery绑定下拉列表时出现此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



我正在使用jquery ajax在选择项目时填充整个表单。该表单包含4-5个下拉列表和3个复选框列表以及少量文本框。因此,我计划使用c#从代码后面绑定值而不是在jquery中为每个下拉列表编写函数/



该场景已在下面的帮助中解释如下样本:



testForm.aspx页面

  function  shw(id){
alert(id);

$ .ajax({
type: post
url: testForm.aspx / popu
data:< span class =code-string>' {uid:' + id + ' }'
// dataType :json,
contentType: application / json; charset = utf-8
成功:功能(resp){
alert(resp.d);
},
错误: function (xhr,errorType,exception){
alert(errorType + <小号pan class =code-string> | +例外);
}
});
}





< pre lang =HTML> < asp:ListBox ID = ListBox1 runat = server onchange = shw(this.value); >
< span class =code-keyword>< asp:ListItem 文字 = = 1 > < / asp:ListItem >
< asp:ListItem 文本 = B = 2 > < / asp:ListItem >
< asp:ListItem 文本 = C = 3 > < / asp:ListItem >
< ; asp:ListItem 文字 = D = 4 > < / asp:ListItem >
< asp:ListItem 文本 = E = 5 > < ; / asp:ListItem >
< / asp:ListBox >





testForm.aspx.cs page

 [WebMethod] 
public static void popu(字符串 uid)
{
// JavaScriptSerializer js = new JavaScriptSerializer();
// return js.Serialize(Value = + ID);

// returnValue =+ uid;

使用(testForm obj = new testForm())
{
obj.bindDrop1();
}
}



 私人  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();
}





bindDrop1()方法中的上述粗线会引发错误:

对象引用未设置为对象的实例。

当我查找ddl1对象时,它显示为NULL。

嗯,我可以理解对象(ddl1)是NULL,所以这个错误是。但是,我无法理解为什么以及如何?

因为,第一次调用page_load事件,那时只会创建所有控件,如ddl1,ddl2等。那么,为什么它显示为NULL?

解决方案

.ajax({
type: < span class =code-string> post,
url: testForm.aspx / popu
data:' {uid:' + id + ' }'
// dataType:json,
contentType: application / json; charset = utf-8
成功: function (resp){
alert (resp.d);
},
错误: function (xhr,errorType,exception){
a lert(errorType + | +例外);
}
});
}





< pre lang =HTML> < asp:ListBox ID = ListBox1 runat = server onchange = shw(this.value); >
< span class =code-keyword>< asp:ListItem 文字 = = 1 > < / asp:ListItem >
< asp:ListItem 文本 = B = 2 > < / asp:ListItem >
< asp:ListItem 文本 = C = 3 > < / asp:ListItem >
< ; asp:ListItem 文字 = D = 4 > < / asp:ListItem >
< asp:ListItem 文本 = E = 5 > < ; / asp:ListItem >
< / asp:ListBox >





testForm.aspx.cs page

 [WebMethod] 
public static void popu(字符串 uid)
{
// JavaScriptSerializer js = new JavaScriptSerializer();
// return js.Serialize(Value = + ID);

// returnValue =+ uid;

使用(testForm obj = new testForm())
{
obj.bindDrop1();
}
}



 私人  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();
}





bindDrop1()方法中的上述粗线会引发错误:

对象引用未设置为对象的实例。

当我查找ddl1对象时,它显示为NULL。

嗯,我可以理解对象(ddl1)是NULL,所以这个错误是。但是,我无法理解为什么以及如何?

因为,第一次调用page_load事件,那时只会创建所有控件,如ddl1,ddl2等。那么,为什么它显示为NULL?


静态方法无法访问非静态类级别成员,并且静态方法也无权访问UI控件等成员,因为它不是那个实例。

您可以从您的Web服务获取数据,并使用您的jquery方法使用该数据填充网格。查看以下文章了解更多信息

使用jQuery AJAX将数据集绑定到ASP.Net GridView [ ^ ]

在asp.net C#中使用jQuery JSON Ajax调用绑定Gridview [ ^ ]

在ASP.NET中使用jQuery绑定GridView [ ^ ]


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 + '}',
                //dataType: "json",
                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)
        {
            //JavaScriptSerializer js = new JavaScriptSerializer();
            //return js.Serialize("Value = " + ID);

            //return "Value = " + 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?

解决方案

.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: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)
        {
            //JavaScriptSerializer js = new JavaScriptSerializer();
            //return js.Serialize("Value = " + ID);

            //return "Value = " + 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?


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[^]


这篇关于“对象引用未设置为对象的实例” - 使用jquery绑定下拉列表时出现此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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