首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

联动控件有关问题,老大们进来看看代码

2012-03-12 
联动控件问题,老大们进来看看代码?我想把联动的两个DropDownList放到1个ascx控件中,然后写上属性,方便调用

联动控件问题,老大们进来看看代码?
我想把联动的两个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   ListItem( "家用电器 ",   "1 "));
                        items.Add(new   ListItem( "食品 ",   "2 "));



                        DropDownList1.DataSource   =   items;
                        DropDownList1.DataBind();
                }
        }

        public   int   Select1
        {
                get   {   return   int.Parse(DropDownList1.SelectedValue.ToString());   }
                set   {   DropDownList1.SelectedValue   =   value.ToString();   }
        }

        public   int   Select2
        {
                get   {   return   int.Parse(DropDownList2.SelectedValue.ToString());   }
                set   {   DropDownList2.SelectedValue   =   value.ToString();   }
        }
}


[解决办法]
1.你在select的onchange事件里把该控件改变了,所以不能通过回发验证
2.SelectedValue属性是只读的,不能赋值。你可以给SelectedIndex赋值
[解决办法]
在客户端添加的option服务器端并不能得到.
[解决办法]
呵呵,他回答了.没刷新,没看见

热点排行