剛剛做了個2級無刷新連動..可是選擇第一個下拉菜單的話第二個菜單沒有數據
我把整個配置到代碼都發一下..希望高手幫忙解決web.config代碼
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>.aspx js代碼
<SCRIPT language="javascript">
//单位------------------------------
function danweiResult() 

var danwei=document.getElementById("DropDownList1");
AjaxMethod.GetDropDownList2(danwei.value,get_danwei_Result_CallBack);
}

function get_danwei_Result_CallBack(response)
{

if (response.value != null)
{
//debugger;
document.all("DropDownList2").length=0;    
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
{
var name=ds.Tables[0].Rows[i].pvdt;
var id=ds.Tables[0].Rows[i].pdpt;
document.all("DropDownList2").options.add(new Option(name,id));
}
}
}
return danweiResult();
}
//應為代碼是從3級連動考過來的..下面的js代碼是不是沒用的?我沒敢刪除!!
function getData()
{
var bumen=document.getElementById("DropDownList1");
var pindex = bumen.selectedIndex;
var pValue = bumen.options[pindex].value;
var pText  = bumen.options[pindex].text;
var danwei=document.getElementById("DropDownList2");
var cindex = danwei.selectedIndex;
var cValue = danwei.options[cindex].value;
var cText  = danwei.options[cindex].text;
}
</SCRIPT>cs代碼

private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if(!Page.IsPostBack)
{
this.DropDownList1.DataSource=AjaxMethod.GetDropDownList1();
this.DropDownList1.DataTextField="pvdt";
this.DropDownList1.DataValueField="pdpt";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onclick","danweiResult();");
}// 在此处放置用户代码以初始化页面
}AjaxMethod.cs代碼using System;
using System.Data;
using System.Data.OracleClient;namespace education.tongji
{
public class AjaxMethod
{
#region GetDropDownList1
public static DataSet GetDropDownList1()
{
string sql="select pdpt,pvdt from ha01 where pdpt like '%---- '";
return GetDataSet(sql);
}
#endregion#region GetDropDownList2
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public  DataSet GetDropDownList2(string pdpt)
{
//string tttt=pdpt.Replace("-","").Trim().ToString();
string sql="select pdpt,pvdt from ha01 where pdpt like '%"+pdpt+"%' order by pdpt asc";
return GetDataSet(sql);
}
#endregion#region GetDataSet
public static DataSet GetDataSet(string sql)
{
string ConnectionString= "Data Source=report;uid=hr;pwd=hr;";
OracleDataAdapter sda =new OracleDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion

}
}

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
    if(!Page.IsPostBack)
    {
    this.DropDownList1.DataSource=AjaxMethod.GetDropDownList1();
    this.DropDownList1.DataTextField="pvdt";
    this.DropDownList1.DataValueField="pdpt";
    this.DropDownList1.DataBind();
    this.DropDownList1.Attributes.Add("onChange","danweiResult();");
    }// 在此处放置用户代码以初始化页面
    }
      

  2.   

    onclick 事件是能夠觸發到的js中輸出變量怎麼寫的?
    var danwei=document.getElementById("DropDownList1");
    alert("+danwei+")//這個應該怎麼寫?
      

  3.   

    頂上來js中輸出變量怎麼寫的?
    var danwei=document.getElementById("DropDownList1");
    alert("+danwei+")//這個應該怎麼寫?
      

  4.   

    function get_danwei_Result_CallBack(response)//誰知道這個response是什麼意思阿??
    {if (response.value != null)
    {
    document.all("DropDownList2").length=0;    
    var ds = response.value;
    if(ds != null && typeof(ds) == "object" && ds.Tables != null)
    {
    for(var i=0; i<ds.Tables[0].Rows.length; i++)
    {
    var name=ds.Tables[0].Rows[i].pvdt;
    var id=ds.Tables[0].Rows[i].pdpt;
    document.all("DropDownList2").options.add(new Option(name,id));
    }
    }
    }
    return danweiResult();
    }
      

  5.   

    this.DropDownList1.Attributes.Add("onchange","danweiResult();");function danweiResult() 

        var ddl = document.getElementById("DropDownList1");
        var index = ddl.selectedIndex;
        var value = ddl.options[index].value;
    AjaxMethod.GetDropDownList2(value,get_danwei_Result_CallBack);
    }
      

  6.   

    alert("+danwei+")//這個應該怎麼寫?
    --------
    alert(danwei);//将会弹出[object],不知道你想alert什么东西?function get_danwei_Result_CallBack(response)//誰知道這個response是什麼意思阿??
    ---------
    把它理解为从.cs中返回来的东西就可以了
      

  7.   

    你现在danwei里面没数据
    你可以在.cs中设置断点
    看看选择bumen的时候是否执行了.cs中的方法
    然后再document.all("DropDownList2").options.add(new Option(name,id));
    这个地方加上
    alert(name);
    alert(id);
    看看是否执行了
      

  8.   

    http://www.i2key.com/TechDoc/Index.aspx?grads=0&Curpage=1&SearchKey=%ce%de%cb%a2%d0%c2&CatID=
      

  9.   

    謝謝蟑螂..
    蟑螂在嗎??
    我現在
    #region GetDropDownList2
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
    public  DataSet GetDropDownList2(string pdpt)
    {
    string sql1="select pdpt,pvdt from ha01 where pdpt = 'W------ ' order by pdpt asc";
    return GetDataSet(sql1);
    }
    #endregion
    在這個函數當中好像做了死循環了..我按F8一條條執行..執行完了又回到sql1重新執行了
    沒有把值傳回給.aspx里的函數中去
    是前面的函數錯了還是這裡錯了??
      

  10.   

    還差一點點就出來了
    if(ds != null && typeof(ds) == "object" && ds.Tables != null)

    for(var i=0; i<ds.Tables[0].Rows.length; i++)
    {
    alert (ds.Tables[0].Rows.length);//這裡我已經能夠取到表格得長度了
    var name = ds.Tables[0].Rows[i].pvdt;
    var id = ds.Tables[0].Rows[i].pdpt;
    alert(name);//這裡顯示的是undefined,是不是定義name的語法有錯誤阿
    document.all("DropDownList2").options.add(new Option(name,id));
    }
    }