日期:2014-05-19  浏览次数:20453 次

联动控件问题,老大们进来看看代码?
我想把联动的两个DropDownList放到1个ascx控件中,然后写上属性,方便调用,但是取值和设置值这两个功能都不能实现。大家帮忙看看
这个是Test.ascx文件
<%@   Control   Language= "C# "   AutoEventWireup= "true "   CodeFile= "Test.ascx.cs "   Inherits= "Test "   %>
<table>
<tr>
  <td>
                <asp:DropDownList   ID= "DropDownList1 "   runat= "server "   onChange= "redirect(this.options.selectedIndex) ">
                </asp:DropDownList>
  </td>
  <td>      
                <asp:DropDownList   ID= "DropDownList2 "   runat= "server ">
                </asp:DropDownList>
  </td>
</tr>
</table>

<script   language= "javascript "   type= "text/javascript ">
var   groups=document.getElementById( " <%=DropDownList1.ClientID%> ").options.length
var   group=new   Array(groups)
for   (i=0;   i <groups;   i++)
group[i]=new   Array()

group[0][0]=new   Option( "请选择 ", "0 ");
group[1][0]=new   Option( "电冰箱 ", "1 ");group[1][1]=new   Option( "洗衣机 ", "2 ");
group[2][0]=new   Option( "方便面 ", "3 ");group[2][1]=new   Option( "饼干 ", "4 ");

var   temp   =   document.getElementById( " <%=DropDownList2.ClientID%> ")
function   redirect(x)
{
        for   (m=temp.options.length-1;m> 0;m--)
        temp.options[m]=null
        for   (i=0;i <group[x].length;i++)
        {
                temp.options[i]=new   Option(group[x][i].text,group[x][i].value)
        }
                temp.options[0].selected=true
}
</script>

下面的是Test.ascx.cs文件
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
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;

public   partial   class   Test   :   System.Web.UI.UserControl
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                if   (!IsPostBack)
                {
                        ListItemCollection   items   =   new   ListItemCollection();
                        items.Add(new   ListItem( "请选择 ",   "0 "));
                        items.Add(new   ListIt