DataGrid中的级联菜单

要点:
1> 通过一个隐藏的页面根据传递的参数从数据源获取数据,并且写到网络流中。
2> 在主页面通过javascript+xmlhttp根据一级菜单提供的参数(省的代码)向隐藏页面发送请求,并且解析返回的响应,并且动态添加到二级菜单中。
3> 由于DropDownList是在DataGrid模板中,因此DropDownList的ID是不确定的,因此需要用js根据父菜单的ID来动态生成子菜单的ID并且用document.getElementByID查找。

隐藏页面代码(default2.aspx.cs):
 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Collections;
 5 None.gif using  System.Web;
 6 None.gif using  System.Web.Security;
 7 None.gif using  System.Web.UI;
 8 None.gif using  System.Web.UI.WebControls;
 9 None.gif using  System.Web.UI.WebControls.WebParts;
10 None.gif using  System.Web.UI.HtmlControls;
11 None.gif using  System.Data.OleDb;
12 None.gif
13 None.gif
14 None.gif public  partial  class  Default2 : System.Web.UI.Page
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
17InBlock.gif    /// Retrive data from db based on querystring.
18InBlock.gif    /// </summary>
19InBlock.gif    /// <param name="sender"></param>
20ExpandedSubBlockEnd.gif    /// <param name="e"></param>

21InBlock.gif    protected void Page_Load(object sender, EventArgs e)
22ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
23InBlock.gif        string id = Request.QueryString["id"];
24InBlock.gif        OleDbConnection cnn=new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source="+Server.MapPath(@"Data\Data.mdb"));
25InBlock.gif        OleDbDataAdapter adapter=new OleDbDataAdapter("select * from child where provincecode="+id,cnn);
26InBlock.gif        DataSet dst=new DataSet();
27InBlock.gif        try
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            adapter.Fill(dst);
30InBlock.gif            dst.WriteXml(Response.OutputStream);
31ExpandedSubBlockEnd.gif        }

32InBlock.gif        catch(Exception ex)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            Response.Write(ex.Message);
35ExpandedSubBlockEnd.gif        }

36ExpandedSubBlockEnd.gif    }

37ExpandedBlockEnd.gif}

38 None.gif


主页面服务器端代码:
 1 None.gif using  System;
 2 None.gif using  System.Data;
 3 None.gif using  System.Configuration;
 4 None.gif using  System.Web;
 5 None.gif using  System.Web.Security;
 6 None.gif using  System.Web.UI;
 7 None.gif using  System.Web.UI.WebControls;
 8 None.gif using  System.Web.UI.WebControls.WebParts;
 9 None.gif using  System.Web.UI.HtmlControls;
10 None.gif using  System.Data.OleDb;
11 None.gif
12 None.gif public  partial  class  _Default : System.Web.UI.Page
13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
14InBlock.gif    protected void Page_Load(object sender, EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        if (!IsPostBack)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            //Bind test data to grid view for display purpose.
19InBlock.gif            GridView1.DataSource = getProvince();
20InBlock.gif            GridView1.DataBind();
21InBlock.gif         
22InBlock.gif            for (int i = 0; i < GridView1.Rows.Count; i++)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                DropDownList list = (DropDownList)(GridView1.Rows[i].FindControl("Province"));
25InBlock.gif                //Register client event
26InBlock.gif                list.Attributes.Add("onchange""RequestByGet(this)");
27ExpandedSubBlockEnd.gif            }

28InBlock.gif           
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31InBlock.gif
32ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
33InBlock.gif    /// Retrieve provinces for upcoming dropdownlist province binding.
34InBlock.gif    /// </summary>
35ExpandedSubBlockEnd.gif    /// <returns></returns>

36InBlock.gif    public DataTable getProvince()
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        OleDbConnection cnn = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=" + Server.MapPath(@"Data\Data.mdb"));
39InBlock.gif        OleDbDataAdapter adapter = new OleDbDataAdapter("select * from parent ", cnn);
40InBlock.gif        DataSet dst = new DataSet();
41InBlock.gif        try
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            adapter.Fill(dst);
44InBlock.gif            
45ExpandedSubBlockEnd.gif        }

46InBlock.gif        catch (Exception ex)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            Response.Write(ex.Message);
49ExpandedSubBlockEnd.gif        }

50InBlock.gif        return dst.Tables[0];
51ExpandedSubBlockEnd.gif    }

52InBlock.gif  
53ExpandedBlockEnd.gif}

54 None.gif

主页面客户端代码:
 1 None.gif <% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>
 2 None.gif
 3 None.gif <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
 4 None.gif
 5 None.gif < html xmlns = " http://www.w3.org/1999/xhtml "   >
 6 None.gif < head runat = " server " >
 7 None.gif     < title > Untitled Page </ title >
 8 None.gif     < script language = javascript >
 9 ExpandedBlockStart.gifContractedBlock.gif      function  RequestByGet(parentSelect) dot.gif
10InBlock.gif  
11InBlock.gif       var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
12InBlock.gif       var doc=new ActiveXObject("MSXML2.DOMDocument");
13InBlock.gif       var URL;
14InBlock.gif       var CityNamelist;
15InBlock.gif       var CityCodeList;
16InBlock.gif       var body;
17InBlock.gif       var postion;
18InBlock.gif       var parentSelectID;
19InBlock.gif       var childPostion; 
20InBlock.gif       var childID;
21InBlock.gif       var child;
22InBlock.gif       var id;
23InBlock.gif       var status;
24InBlock.gif       id=parentSelect.value;
25InBlock.gif       
26InBlock.gif       URL="http://localhost/WebSite4/Default2.aspx?id="+id;
27InBlock.gif       xmlhttp.Open("GET",URL, false); 
28InBlock.gif       xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); 
29InBlock.gif       xmlhttp.SetRequestHeader ("SOAPAction","http://localhost/WebSite4/"); 
30InBlock.gif       xmlhttp.Send(null); 
31InBlock.gif       status = xmlhttp.status; 
32InBlock.gif      
33InBlock.gif      if(status==200)
34ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif
35InBlock.gif            body=xmlhttp.responseText;
36InBlock.gif            //Parse xml string from response text.
37InBlock.gif            postion=body.lastIndexOf("NewDataSet");
38InBlock.gif            body=body.substring(0,postion);
39InBlock.gif            body=body+"NewDataSet>";
40InBlock.gif            doc.loadXML(body);
41InBlock.gif
42InBlock.gif            //Get cityname nodes and provincecode nodes.
43InBlock.gif            CityNamelist=doc.selectNodes("//NewDataSet/Table/CityName");
44InBlock.gif            CityCodeList=doc.selectNodes("//NewDataSet/Table/ProvinceCode");
45InBlock.gif            parentSelectID=parentSelect.id;
46InBlock.gif            childPostion=parentSelectID.lastIndexOf("_");
47InBlock.gif            childID=parentSelectID.substring(0,childPostion);
48InBlock.gif            childID=childID+"_City";
49InBlock.gif            child=document.getElementById(childID);
50InBlock.gif            child.length=1;
51InBlock.gif
52InBlock.gif             for(var i=0;i<CityNamelist.length;i++)
53ExpandedSubBlockStart.gifContractedSubBlock.gif                   dot.gif{
54InBlock.gif                     var op=new Option(CityNamelist.item(i).text,CityCodeList.item(i).text);
55InBlock.gif                     child.options[i+1]=op;
56ExpandedSubBlockEnd.gif                   }

57ExpandedSubBlockEnd.gif       }
 
58InBlock.gif     xmlhttp = null
59ExpandedBlockEnd.gif }
 
60 None.gif </ script >
61 None.gif </ head >
62 None.gif < body  >
63 None.gif     < form id = " form1 "  runat = " server " >
64 None.gif    
65 None.gif     < div >
66 None.gif         < asp:GridView ID = " GridView1 "  runat = " server "  OnSelectedIndexChanged = " GridView1_SelectedIndexChanged "   AutoGenerateColumns = false >
67 None.gif         < Columns >
68 None.gif          < asp:TemplateField HeaderText = " Province " >
69 None.gif         < ItemTemplate >
70 None.gif         < asp:DropDownList ID = " Province "  runat = server   DataSource = " <%#getProvince()%> "  DataTextField = " ProvinceName "  DataValueField = " ProvinceCode "   >
71 None.gif         </ asp:DropDownList >
72 None.gif         </ ItemTemplate >
73 None.gif         </ asp:TemplateField >
74 None.gif        
75 None.gif         < asp:TemplateField HeaderText = " City " >
76 None.gif         < ItemTemplate >
77 None.gif         < asp:DropDownList ID = " City "  runat = server  >
78 None.gif         < asp:ListItem >------</ asp:ListItem >
79 None.gif         </ asp:DropDownList >
80 None.gif         </ ItemTemplate >
81 None.gif         </ asp:TemplateField >
82 None.gif         < asp:BoundField  HeaderText = " Message "  DataField = " ProvinceName "  DataFormatString = " Just a static message inlcuding the word {0} " />
83 None.gif         </ Columns >
84 None.gif         </ asp:GridView >
85 None.gif    
86 None.gif     </ div >
87 None.gif     </ form >
88 None.gif </ body >
89 None.gif </ html >
90 None.gif

效果图:




代码下载(含数据库):
Download

其他:
这个例子只是用vs2005做了一个简单的演示,没有考虑代码移植,如果需要用其中的代码片段,请做对应的调整。

转载于:https://www.cnblogs.com/netboy/archive/2006/04/27/386230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值