xmlhttp无刷新二级联动支持firefox

javascript代码:
<script language="javascript" type="text/javascript"> 
//jb函数会根据不同的浏览器初始化个xmlhttp对象 
function jb() 

    
var A=null
    
try 
    

        A
=new ActiveXObject("Msxml2.XMLHTTP"); 
    }
 
    
catch(e) 
    

        
try 
        

            A
=new ActiveXObject("Microsoft.XMLHTTP"); 
        }
 
        
catch(oc) 
        

            A
=null 
        }
 
    }
 
    
if ( !&& typeof XMLHttpRequest != "undefined" ) 
    

        A
=new XMLHttpRequest() 
    }
 
    
return A 
}
 
              
//下面Go函数是父列表框改变的时候调用,参数是选择的条目 
function XmlPostT(obj) 

    
//得到选择框的下拉列表的value 
    var svalue = obj; 
    
//定义要处理数据的页面 
    var weburl = "?yearid="+svalue; 
    
//初始化个xmlhttp对象 
    var xmlhttp = jb(); 
    
//提交数据,第一个参数最好为get,第三个参数最好为true 
    xmlhttp.open("get",weburl,true); 
    
// alert(xmlhttp.responseText); 
    //如果已经成功的返回了数据 
    xmlhttp.onreadystatechange=function() 
    

        
if(xmlhttp.readyState==4)//4代表成功返回数据 
        
            
var result = xmlhttp.responseText;//得到服务器返回的数据 
            //先清空dListChild的所有下拉项 
            document.getElementById("ddlnos").length = 0
            
//给dListChild加个全部型号的,注意是Option不是option 
            document.getElementById("ddlnos").options.add(new Option("请选择刊号","")); 
           
if(result!="")//如果返回的数据不是空 
           
                
//把收到的字符串按照,分割成数组 
                var allArray = result.split(","); 
                
//循环这个数组,注意是从1开始,因为收到的字符串第一个字符是,号,所以分割后第一个数组为空 
                for(var i=0;i<allArray.length;i++
                

                    
//在把这个字符串按照|分割成数组 
                    var thisArray = allArray[i].split("|"); 
                    
//为dListChild添加条目 
                    document.getElementById("ddlnos").options.add(new Option(thisArray[1].toString(),thisArray[0].toString())); 
                }
 
           }
 
    }
 
 }
 
 
//发送数据,请注意顺序和参数,参数一定为null或者"" 
 xmlhttp.send(null); 
}
 
function option_gourl(str)
{
    window.location.href
=str;
}

</script> 

aspx代码:
        <tr>
          
<td height="30" align="center">
              
<asp:DropDownList ID="ddlyears" runat="server" AppendDataBoundItems="True">
                  
<asp:ListItem Value="">请选择年份</asp:ListItem>
              
</asp:DropDownList>                  
              
</td>
          
<td align="center"><asp:DropDownList ID="ddlnos" runat="server" AppendDataBoundItems="True">
                
<asp:ListItem Value="">请选择刊号</asp:ListItem>
              
</asp:DropDownList></td>
        
</tr>

c#代码:
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.ddlnos.Attributes.Add("onchange""option_gourl(this.value)");
            
this.ddlyears.Attributes.Add("onchange""XmlPostT(this.value)");
            BinData();
            
if (yearid != "")
            
{
                
this.NoBind(yearid);
            }

        }

    }

    
private string yearid
    
{
        
get
        
{
            
if (ViewState["yearid"!= null && ViewState["yearid"].ToString() != "")
            
{
                
return ViewState["yearid"].ToString();
            }

            
else
            
{
                
if (Request["yearid"!= null && Request["yearid"].ToString() != "")
                
{
                    
return Request["yearid"];
                }

                
else
                
{
                    
return "";
                }

            }

        }

        
set
        
{
            ViewState[
"yearid"= value;
        }

    }

    
private void NoBind(string strid)
    
{
        WebClass.ArticleList tmp 
= new WebClass.ArticleList();
        DataTable dt 
= tmp.ListIndexYear(strid);

        
string mystr = "";
        
if (dt.Rows.Count != 0)
        
{
            
for (int i = 0; i < dt.Rows.Count; i++)
            
{
                mystr 
+= "," + dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString();
            }

            mystr 
= mystr.Substring(1);
        }


        
//把从数据库得到的信息输出到客户端
        Response.Write(mystr);
        
//输出完成关闭Response,以免造成不必要的输出
        Response.Flush();
        Response.Close();
        
//this.Response.Write(mystr);
        
//this.Response.End();
    }

posted on 2008-06-06 20:39  ★金★  阅读(359)  评论(0编辑  收藏  举报

导航