实现无刷新(script访问)

private void Page_Load(object sender, System.EventArgs e)
{
    
if(this.Request["state"]!=null
)
    
{
        
string state = this.Request["state"
].ToString();
        SqlConnection con 
= new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=sa;"
);
        SqlDataAdapter da 
= new SqlDataAdapter("select city from authors where state = '"+state+"'"
,con);
        DataSet ds 
= new DataSet("authors"
);
        da.Fill(ds);

        
//以下是创建XML格式输出

        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
        writer.Formatting 
=
 Formatting.Indented;
        writer.Indentation 
= 4
;
        writer.IndentChar 
= ' '
;
        ds.WriteXml(writer);
        writer.Flush();
        Response.End();
        writer.Close();
    }

}

例如输出的XML如下:

<NewDataSet>
    
<authors>
        
<city>wuhan</city>
    
</authors>
</NewDataSet>

 

<script>
    
function load()
    {
        
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP"
);
        
var oDoc = new ActiveXObject("MSXML2.DOMDocument"
);

        
//打开某文件

        oHttpReq.open("POST""webform6.aspx?state="+state, false);
        oHttpReq.send(
""
);
        result 
=
 oHttpReq.responseText;
        
//读取XML

        oDoc.loadXML(result);
        items 
= oDoc.selectNodes("//authors"
);
        
for (var item = items.nextNode(); item; item =
 items.nextNode())
        {
            
var city = item.selectSingleNode("//city"
).nodeTypedValue;
               xx.value = city;
        }
    }
</script>
posted @ 2004-10-13 13:16  小Dan  阅读(781)  评论(0编辑  收藏  举报