2014-04-16 2 views
0

ASP.Net에서 jQuery를 사용하여 AJAX 계단식 DropDownList를 사용하려고합니다. 하지만 내가 처음 드롭 다운 목록 (ddl_il)을 선택하면 jquery로 두 번째 Dropdownlist에 도달 할 수 없으며 jquery 코드에 대한 내 문제는 무엇인지 null로 오류가 발생합니까? 어떻게 해결할 수 있습니까?

enter image description hereJQuery AJAX 다중 계단식 드롭 다운 목록 select가 probl을로드하지 않습니다.




Default.aspx를

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

<!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></title> 
    <script src="JS/jquery-1.3.2.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#loaderGif1").hide(); 
     $("#loaderGif2").hide(); 

     $("#ddl_ilce").html("<option value=''>Önce İl Seçiniz</option>"); 
     $("#ddl_semt").html("<option value=''>Önce İlçe Seçiniz</option>"); 

     $("#ddl_il").change(function() { 
      ilChange(); 
     }) 

     $("#ddl_ilce").change(function() { 
      ilceChange(); 
     }) 
    }); 

    function ilChange() { 
     $("#loaderGif1").show(); 
     $("#ddl_ilce").attr("disabled", "true").html("<option value=''>Önce İl Seçiniz</option>"); 
     $("#ddl_semt").attr("disabled", "true").html("<option value=''>Önce İlçe Seçiniz</option>"); 

     var ilID = $("#ddl_il").val(); 

     var pagePath = window.location.pathname; 



     $.ajax({ 
      type: "POST", //GET veya POST 
      url: "Default.aspx/ilChange", 
      contentType: "application/json; charset=utf-8", 
      data: '{ilID:'+ilID+'}', 
      dataType: "json", 
      success: onSucceeded1, 
      error: onFailed 
     }); 
     return false; 
    } 

    //istek başarılı olduğunda çalışacak fonksiyon 
    function onSucceeded1(result) { 
     $("#loaderGif1").hide(); 
     $("#ddl_ilce").removeAttr("disabled").html(result.d); 

     $("#ddl_semt").removeAttr("disabled"); 
    } 

    //istek hatalı olduğunda çalışacak fonksiyon 
    function onFailed(result) { 
     alert(result.d); 
    } 

    function ilceChange() { 
     $("#loaderGif2").show(); 
     $("#ddl_semt").attr("disabled", "true").html("<option value=''>Önce İlçe Seçiniz</option>"); 
     var ilceID = $("#ddl_ilce").val(); 
     var pagePath = window.location.pathname; 

     $.ajax({ 
      type: "POST", 
      url: "Default.aspx/ilceChange", 
      contentType: "application/json; charset=utf-8", 
      data: '{ilceID:' + ilceID + '}', 
      dataType: "json", 
      success: onSucceeded2, 
      error: onFailed 
     }); 
     return false; 
    } 

    function onSucceeded2(result) { 
     $("#loaderGif2").hide(); 
     $("#ddl_semt").removeAttr("disabled").html(result.d); 
    } 
</script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <table> 
      <tr> 
       <td> 
        il</td> 
       <td> 
        <asp:DropDownList ID="ddl_il" runat="server" > 
        </asp:DropDownList> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        ilçe</td> 
       <td> 
        <asp:DropDownList ID="ddl_ilce" runat="server" > 
        </asp:DropDownList> 
        <img id="loaderGif1" alt="" src="Images/loader.gif" style="width: 16px; height: 16px" /></td> 
      </tr> 
      <tr> 
       <td> 
        semt</td> 
       <td> 
        <asp:DropDownList ID="ddl_semt" runat="server"> 
        </asp:DropDownList> 
        <img id="loaderGif2" alt="" src="Images/loader.gif" style="width: 16px; height: 16px" /></td> 
      </tr> 
     </table> 

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




하여 default.aspx.cs 코드 뒤에

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

public partial class _Default : System.Web.UI.Page 
{ 
    fonksiyon system = new fonksiyon(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     SqlConnection baglantim = new SqlConnection("Data Source=Hkadir;Initial Catalog=il-ilce-Semt;Integrated Security=True"); 
     DataTable dataTableIl = new DataTable(); 
     SqlCommand sqlCommand = new SqlCommand(); 
     SqlDataAdapter sqlAdapter = new SqlDataAdapter(); 

     baglantim.Open(); 
     try 
     { 
      sqlCommand = new SqlCommand("Select * from iller", baglantim); 

      sqlAdapter = new SqlDataAdapter(sqlCommand); 
      sqlAdapter.Fill(dataTableIl); 

      ddl_il.DataSource = dataTableIl; 
      ddl_il.DataTextField = "Ad"; 
      ddl_il.DataValueField = "IlID"; 
      ddl_il.DataBind(); 
      ListItem li = new ListItem("İl Seçiniz",""); 
      ddl_il.Items.Insert(0, li); 
     } 
     finally 
     { 
      baglantim.Close(); 
     } 
    } 


    public static string ilChange(string ilID) 
    { 
     System.Threading.Thread.Sleep(2000); 
     SqlConnection baglantim = new SqlConnection("Data Source=Hkadir;Initial Catalog=il-ilce-Semt;Integrated Security=True"); 
     DataTable dataTableIlce = new DataTable(); 
     SqlCommand sqlCommand = new SqlCommand(); 
     SqlDataAdapter sqlAdapter = new SqlDataAdapter(); 
     string result=""; 

     baglantim.Open(); 
     try 
     { 
      sqlCommand = new SqlCommand("Select * from ilceler where [email protected]", baglantim); 

      sqlCommand.Parameters.Add("IlID", SqlDbType.NVarChar); 
      sqlCommand.Parameters["IlID"].Value = ilID; 

      sqlAdapter = new SqlDataAdapter(sqlCommand); 
      sqlAdapter.Fill(dataTableIlce); 

      result += "<option value=''>İlçe Seçiniz</option>"; 
      foreach (DataRow dataRow in dataTableIlce.Rows) 
      { 
       result += "<option value='" + dataRow["IlceId"].ToString() + "'>" + dataRow["Ad"].ToString() + "</option>"; 
      } 
      return result; 
     } 
     catch(Exception e) 
     { 
      return (e.ToString()); 
     } 
     finally 
     { 
      baglantim.Close(); 
     } 
    } 


    public static string ilceChange(string ilceID) 
    { 
     System.Threading.Thread.Sleep(2000); 
     SqlConnection baglantim = new SqlConnection("Data Source=Hkadir;Initial Catalog=il-ilce-Semt;Integrated Security=True"); 
     DataTable dataTableSemt = new DataTable(); 
     SqlCommand sqlCommand = new SqlCommand(); 
     SqlDataAdapter sqlAdapter = new SqlDataAdapter(); 
     string result = ""; 

     baglantim.Open(); 
     try 
     { 
      sqlCommand = new SqlCommand("Select * from semtler where [email protected]", baglantim); 

      sqlCommand.Parameters.Add("IlceID", SqlDbType.NVarChar); 
      sqlCommand.Parameters["IlceID"].Value = ilceID; 

      sqlAdapter = new SqlDataAdapter(sqlCommand); 
      sqlAdapter.Fill(dataTableSemt); 

      result += "<option value=''>Semt Seçiniz</option>"; 
      foreach (DataRow dataRow in dataTableSemt.Rows) 
      { 
       result += "<option value='" + dataRow["SemtId"].ToString() + "'>" + dataRow["Ad"].ToString() + "</option>"; 
      } 
      return result; 
     } 
     catch (Exception e) 
     { 
      return (e.ToString()); 
     } 
     finally 
     { 
      baglantim.Close(); 
     } 
    } 
} 

데이터베이스 :

enter image description here

답변

1

는 WebMethod 특성을 추가 할 필요가 직접 ASP.NET AJAX 페이지 메소드를 호출 할 jQuery를 사용합니다.
+0

ı ilChange (문자열 ilID) 그냥 [webmethod] 넣어,하지만 두 번째 드롭 다운 목록에서 null로 오는 항목을 표시 할 수 없습니다 – neon

+0

괜찮아요 내가 고맙다고 해결 :) – neon

0

모든 코드에보기를 가진 후 : 당신의 기능 onsucceeded & onsucceded2$("#ddl_semt").removeAttr("disabled").html(result); 모두 $("#ddl_ilce").removeAttr("disabled").Append(result);$("#ddl_semt").removeAttr("disabled").Append(result);이 줄 $("#ddl_ilce").removeAttr("disabled").html(result.d);을 변경하려고합니다. 나는 그것이 오류 일 수 있다고 추정한다.

[WebMethod] 
public static string ilChange(string ilID) 
{ 
    /// 
} 
+0

나는이 코드를 변경하지만 결과는 내가 대답을 업데이트 한 – neon

+0

작동하지 않습니다 동일합니다 –

+0

객체가 있는지'경고 (결과)'를 시도하십시오. –

관련 문제