本文介绍了Jquery json下拉列表poulate对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我点击我的按钮后出现以下错误

请检查代码并让我知道,

i已经花了很多时间来解决这个问题。 />
此代码的目的是获取基于pincode的国家/地区列表

我的下拉菜单是正确的

但是之后我点击按钮获取我在c#

中的下拉值会引发此错误。



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





单击我的按钮后出现此错误。



aspx代码



Hello all i am getting following error on click of my button
please check code and let me know,
i have already spent lot of time solving this.
Purpose of this code is to get list of country's based on pincode
my dropdown is coming correct
but after that when i click on button to get my dropdownvalue in c#
it throws this error.

Object reference not set to an instance of an object.


I am getting this error on click of my button.

aspx code

<%@ Page Language="C#" AutoEventWireup="false"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Bind Dropdownlist with JQuery in asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    //function copy() {
    //    document.getElementById("lblCountry").innerHTML = document.getElementById("ddlCountry").value;
    //}
    $(function () {
        $('[id$=txtPin]').change(function () {
            var v = $('[id$=txtPin]').val();
            $.ajax({
                type: "POST",
                contentType: "application/json;charset=utf-8",
                url: "Default.aspx/BindDatatoDropdown",
                data: JSON.stringify({ id: '1', pinCode: v }),
                dataType: "json",
                success: function (data) {
                            $.each(data.d, function (key, value) {
                                $("#ddlCountry").append($("<option></option>").val(value.ID).html(value.Description));
                            });
                },
                error: function (result) {
                    alert("error");
                }
            });
        });


        $('[id$=ddlCountry]').change(function () {

            alert("Handler for .change() called.");
        });
    });
    $(document).ready(function () {

    });
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>

    <asp:TextBox ID="txtPin" runat="server"></asp:TextBox>
    <br />
    <br />
<asp:DropDownList ID="ddlCountry" runat="server" />
     <%--onchange="copy();--%>
    <p>
        <%--<asp:Label ID="lblCountry" runat="server" Text="Label"></asp:Label>--%>
    </p>


            <asp:Button ID="btnGetData" runat="server" OnClick="btnGetData_Click" Text="Get Data" />

</form>
</body>
</html>







C#代码






C# Code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public class PODetails
    {
        public string ID { get; set; }
        public string Description { get; set; }
    }

    [WebMethod]
    public static PODetails[] BindDatatoDropdown(string id, string pinCode)
    {
        DataTable dt = new DataTable();
        List<podetails> details = new List<podetails>();

        using (SqlConnection con = new SqlConnection("Data Source=SAM-PC;Initial Catalog=BPOPortalProd;Persist Security Info=True;User ID=sa;Password=sudesi123"))
        {
            SqlCommand cmd = new SqlCommand("GetAddressDetails",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.VarChar, 100)).Value = id;
            cmd.Parameters.Add(new SqlParameter("@pincode", SqlDbType.VarChar, 100)).Value = pinCode;

            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dtrow in dt.Rows)
            {
                PODetails country = new PODetails();
                country.ID = dtrow["ID"].ToString();
                country.Description = dtrow["Description"].ToString();
                details.Add(country);
            }

        }
        return details.ToArray();
    }

    protected void btnGetData_Click(object sender, EventArgs e)
    {
        //This line is causing error
        string str = ddlCountry.SelectedItem.Text;
    }
}



先谢谢


Thanks in Advance

推荐答案




这篇关于Jquery json下拉列表poulate对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:06