asp.net javascript对象引用未设置为对象的实例

时间:2011-10-05 14:09:55

标签: javascript asp.net ajax

我说英语不太好。讲述问题。 。 net page,从数据库中获取数据的javascript代码。我将它们添加到DropDownList中。当我运行它时,我想在按下按钮时从选定的DropDownList选项中检索数据。 asp.net,但我看到它作为DropDownList的一部分是空的。我能做什么。等待帮助。感谢。

aspx.cs ----------------

using Ajax;

public partial class Ajax_CSharp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Ajax_CSharp));
    }


    [Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
    public string GetDataCity()
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
            SqlCommand cmd = new SqlCommand("SELECT * FROM Sehir", con);
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet("DataSet");
            adapter.Fill(ds, "Table");

            if ((ds.Tables[0].Rows.Count <= 0))
            {
                return "Empty";
            }
            else
            {
                string cityID = "";
                string cityName = "";
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    cityID += ds.Tables[0].Rows[i]["SehirID"].ToString() + ",";
                    cityName += ds.Tables[0].Rows[i]["SehirAdi"].ToString() + ",";
                }
                cityID = cityID.Substring(0, cityID.Length - 1);
                cityName = cityName.Substring(0, cityName.Length - 1);

                return cityID + "~" + cityName;
            }
        }
        catch
        {
            return "Error";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string c;
        c = ddlistCity.SelectedItem.Value; **(error here. DropDownList is null.)**
    }

aspx页面----------------------

<script language="javascript" type="text/javascript">

    function GetDataCity() {
        var response;
        Ajax_CSharp.GetDataCity(GetData_CallBackCity);
    }

    function GetData_CallBackCity(response) {
        var response = response.value;

        if (response == "Empty") {
            alert("no record");
        }
        else if (response == 'Error') {
            alert("database not connection");
        }
        else {
            var arr = response.split("~");
            var cityID = arr[0].split(",");
            var cityName = arr[1].split(",");

            document.getElementById('ddlistCity').length = 0;
            var o = document.createElement("option");
            o.value = "select";
            o.text = "select";
            document.getElementById('ddlistCity').add(o);
            for (var i = 0; i < cityID.length; i++) {
                var o = document.createElement("option");
                o.value = cityID[i];
                o.text = cityName[i];
                document.getElementById('ddlistCity').add(o);
            }
        }
    }




    </script>
</head>
<body onload="GetDataCity();">
    <form id="form1" runat="server"  >
    <div>
        <asp:DropDownList ID="ddlistCity" runat="server" >
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="ddlistCity" ErrorMessage="no select.." 
            InitialValue="seciniz" SetFocusOnError="True" ValidationGroup="genel">*</asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="genel"/>
    </div>
    </form>
</body>

2 个答案:

答案 0 :(得分:1)

改变这个:

protected void Button1_Click(object sender, EventArgs e)
{
     string c;
     c = ddlistCity.SelectedItem.Value; **(error here. DropDownList is null.)**
 }

要:

protected void Button1_Click(object sender, EventArgs e)
{
     string c;
     if (ddllistCity.SelectedItem != null)
        c = ddlistCity.SelectedItem.Value; **(error here. DropDownList is null.)**
 }

防止错误。

但是,错误是在客户端上创建值时,值不会持久保存回服务器。这意味着您必须将下拉列表的选定值存储在隐藏字段中,以便在服务器上处理它。您还必须在每个页面加载时绑定下拉列表,因为服务器不再保留在客户端上创建的项目。

HTH。

答案 1 :(得分:0)

正如Brian所说,使用SelectedItem需要一些逻辑来验证该项不为空。您可以更轻松地使用SelectedValue代替:

string selectedValue = DropDownList1.SelectedValue;