20101229 学习记录:VS2005调试时int型变量值显示为16进制 & 无刷新DropdownList联动(二级联动)


 

在调试工具栏,或监视窗口的右键菜单里或工具栏上有一个”按十六进制显示”(Hexadecimal Display),去掉选中就可以.

 

 


无刷新DropdownList联动(二级联动)

<script type="text/javascript">
function load(DepID){           //ClassID为接收传递的大类编号
    var drp2 = document.getElementById("DropDownList2");
    function RemoveAll(oElem) {             //清除DropDownList2的所有项
    var i = 0;
    for (i = oElem.length; i >= 0; i--){
        oElem.options.remove(i);
        }
    }
    RemoveAll(drp2)
    //新建一请求与XML文档
    var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
    var oDoc = new ActiveXObject("MSXML2.DOMDocument");
    oHttpReq.open("POST", "DdlChild.aspx?DepID="+DepID, false);   //调用读取小类数据的页面,将大类编号值传递过去
    oHttpReq.send("");
    result = oHttpReq.responseText;
    oDoc.loadXML(result);
    items1 = oDoc.selectNodes("//DEPNAME/Table/CenName");   //读取所有请求大类所属小类的类名
    items2 = oDoc.selectNodes("//DEPNAME/Table/CenID"); //读取所有请求大类所属小类的编号        
    var itemsLength=items1.length;
    for(i=0;i<itemsLength;i++)            //将小类的类名和编号赋予DropDownList2                                                   
    {
        var newOption = document.createElement("OPTION");
        newOption.text=items1[i].text;
        newOption.value=items2[i].text;
        drp2.options.add(newOption);
    }
}
    </script>

<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="Red"></asp:Label>
                <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                <asp:TextBox ID="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
                <asp:Label ID="Label2" runat="server"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Button"></asp:Button>

DdlChild.aspx.cs的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["DepID"] != null)
        {
            string state = Request.QueryString["DepID"].ToString();
            SqlConnection con = new SqlConnection("server=localhost;database=IMS;uid=sa;pwd=sa;");
            SqlDataAdapter da = new SqlDataAdapter("select CenName,CenID from Centers where DepID = '" + state + "'", con);
            DataSet ds = new DataSet("DEPNAME");
            da.Fill(ds);
            //XhtmlTextWriter
            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();
        }
    }

上述代码我试过了可用。就是有写内容自己还不是很明白什么意思呢。

以下是在网上搜的资料,我是根据以下资料来整理上述代码的。可供大家参考!

所谓DropdownList联动,也就是在选一个DropdownList的时候使另外一个DropdownList的内容更新(如选省份时显示 所属城市),按常规的方法那就是在第一个DropdownList的SelectedIndexChanged事件中改变第二个DropdownList 的数据源及重新绑定,但是如果这样的话在每一次的重新选择将带来一次页面的刷新,除了屏幕闪动以外,如果同页有密码框的话,内容也会清除掉.这时我们就需 要无刷新实现,基本原理在选择改变时用JS向另外一个隐藏页发送请求并得到一个XML流,解析后放入相应的DropdownList中.例子如下:

  一、数据库设计:

字段名 数据类型 说明
ClassID 自动编号 类编号
ClassName     varchar(8) 类名
UpClassID int(4) 上级类编号
ClassLevel int(4) 类级别,1为大类,2为小类

二、设计步骤:

1、首先,我们新建一个页面DropTest.aspx,在其中放入两个DropDownList控件:

DropDownList1和DropDownList2,其完整代码如下:
<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script>
function load(ClassID){           //ClassID为接收传递的大类编号
var drp2 = document.getElementById("DropDownList2");
function RemoveAll(oElem) {             //清除DropDownList2的所有项
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
//新建一请求与XML文档
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
      oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false);

        //调用读取小类数据的页面,将大类
// 编号值传递过去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("//CLASSNAME/Table/ClassName");  

             //读取所有请求大类所属小类的类名
items2 = oDoc.selectNodes("//CLASSNAME/Table/ClassID");

                  //读取所有请求大类所属小类的编号
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++)                                                                

//将小类的类名和编号赋予DropDownList2
{
var newOption = document.createElement("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
</script>
</HEAD>
<body MS_POSITIONING="flowLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
<asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox>
<asp:Label id="Label1" runat="server"></asp:Label>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
</body>
</HTML>

 

 

上面的页面中有两个DropDownList和一段js脚本,该脚本能直接写在页面也能写在后台在Regeist到页面上(后者更灵活一些)

 

该页面的后台文件(DropDownList1.aspx.cs)中Page_Load内的代码如下:

if(!this.IsPostBack)
{
SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "ClassName";
this.DropDownList1.DataValueField = "ClassID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");  

 /*

 DataSet ds = new DataSet("CITY");
 da.Fill(ds);
 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();

 

//该方法得到用户选择的state通过查询以后得到一个DataSet对象,使用该对象的WriteXML方法直接将内容写到 Response.OutputStream里面然后传递到客户端,客户端的load方法通过result =oHttpReq.responseText;句话得到一个XML字符串,最后解析此串。
 

 

 

 */

//将ClassID作为参数传递给脚本函数load(ClassID),

如果要传递的是ClassName,应将value改为innerText,但如果大类为中文,

则调用小类时出现无法显示的问题
// this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;");  

//读取DropDownList2的值,将其赋给一个TextBox控件TH,以获取DropDownList2的值,为获取DropDownList2的值.

 

 

在上面的代码中我们做了两件事情:1、帮定其中一个DropDownList(你也可以同时绑定两个)。2、指定该 控件 的客户端脚本。下面我们详细介绍一下上面的js代码,首先得到页面上要联动的DorpDownList对象,将他的Options清空,再创建两个客户端 对象oHttpReq和oDoc对象,其中一个负责发送请求另一个负责得到响应结果,我们将用户选择的State发送到名为WebForm6.aspx的 页面,该页面将处理这个请求并返回一个响应,该响应的结果是一个XML文件,稍候介绍WebForm6.aspx里面的代码。我们将返回的结果使用 loadXML方法Load到oDoc对象里面,然后就可以使用selectNodes方法得到所有的city节点,接着循环这些节点在客户端创建 Option对象,最后将这些Option对象Add到DropDwonList2里面去。

 

 

 

20101229 学习记录:VS2005调试时int型变量值显示为16进制 & 无刷新DropdownList联动(二级联动) Eg2
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
<% @ Page Language = " C# "  AutoEventWireup = " true "    CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>
< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
     
< title > .net2.0回调实现二级无刷新联动 </ title >
     
< script type = " text/javascript " >
     
<!--
     function test(arg)
     {
         
if (arg != " button " )
         {
             var thisArray
= arg.split( ' , ' );
             document.getElementById(
" DropDownList2 " ).length = 0 ;
             
for (var i  =   0 ; i < thisArray.length ;i ++ )
             {           
             document.getElementById(
" DropDownList2 " ).options.add( new  Option(thisArray[i].toString(),thisArray[i].toString())); 
             }
         }
         
else
         {
             document.getElementById(
" text " ).innerHTML  =   " 你选择的大类是: " + document.getElementById( " DropDownList1 " ).value + "  小类是: " + document.getElementById( " DropDownList2 " ).value;
         }
     }
     
// -->
      </ script >
< style >
</ style >
</ head >
< body >
     
< form id = " form1 "  runat = " server " >
         
< asp:DropDownList ID = " DropDownList1 "  runat = " server " >
             
< asp:ListItem Text = ' 请选择 ' ></ asp:ListItem >
             
< asp:ListItem Value = 1  Text = 1 ></ asp:ListItem >
             
< asp:ListItem Value = 2  Text = 2 ></ asp:ListItem >
         
</ asp:DropDownList >
         
< asp:DropDownList ID = " DropDownList2 "  runat = " server " >
             
< asp:ListItem Text = ' 请选择 ' ></ asp:ListItem >
         
</ asp:DropDownList >
         
< input type = button onclick = " callserver('3') "  value = " 提交 "   />
         
< span id = " text " ></ span >
     
</ form >
</ body >
</ html >


using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.IO;
using  System.Text;

public   partial   class  _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{

     
protected   void  Page_Load( object  sender, EventArgs e)
     {
         
string  str  =  Page.ClientScript.GetCallbackEventReference( this , " arg " , " test " , null , false ); /// 获取一个对客户端函数的引用
          string  script  =   " function callserver(arg){ " + str + " } " ; /// 组织一个callserver函数
         Page.ClientScript.RegisterClientScriptBlock( this .GetType(),  " key " , script,  true ); /// 注册客户端脚本
         DropDownList1.Attributes.Add( " onchange " " callserver(document.getElementById('DropDownList1').value) " ); /// 为DropDownList1添加客户端的onchange事件,触发callserver函数
     }
     
#region  ICallbackEventHandler 成员
     
string  selecttext  =   null ;
     
public   string  GetCallbackResult()
     {
         StringBuilder sb 
=   new  StringBuilder();

         
if  (selecttext  ==   " 1 " )
         {
             sb.Append(
" 1-1,1-2,1-3 " );
         }
         
else   if  (selecttext  ==   " 2 " )
         {
             sb.Append(
" 2-1,2-2,2-3 " );
         }
         
else
         {
             sb.Append(
" button " );
         }

         
return  sb.ToString(); /// 给客户端函数test返回一个参数sb
     }

     
public   void  RaiseCallbackEvent( string  eventArgument)
     {
         selecttext 
=  eventArgument; /// 获得客户端提交的参数赋给变量selecttext
     }
     
#endregion
}


 

 

http://blog.163.com/tianshenglongchang@126/blog/static/16462850320106311111217/ 

 

 

显示中文: http://www.west263.com/www/info/33996-1.htm

 

http://www.sudu.cn/info/html/edu/20071109/98473.html

http://www.360doc.com/content/09/0518/11/109051_3549391.shtml

 

 

你可能感兴趣的:(list)