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

ddl的aotopostback 这段代码有误?该如何处理

2012-04-10 
ddl的aotopostback 这段代码有误?C# codeprotected void Page_Load(object sender, EventArgs e){if (!IsP

ddl的aotopostback 这段代码有误?

C# code
protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            DDLFamilyRelationship1st.Items.Insert(0, new ListItem("-请选择-", "0"));            DDLFamilyRelationship2nd.Items.Insert(0, new ListItem("-请选择-", "0"));            DDLFamilyRelationship3th.Items.Insert(0, new ListItem("-请选择-", "0"));            DDLFamilyRelationship4th.Items.Insert(0, new ListItem("-请选择-", "0"));            if (DDLSex.Text == "男")            {                DDLFamilyRelationship1st.Items.Insert(1, new ListItem("父子", "0"));                DDLFamilyRelationship1st.Items.Insert(2, new ListItem("母子", "0"));                DDLFamilyRelationship1st.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship1st.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship1st.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship2nd.Items.Insert(1, new ListItem("父子", "0"));                DDLFamilyRelationship2nd.Items.Insert(2, new ListItem("母子", "0"));                DDLFamilyRelationship2nd.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship2nd.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship2nd.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship3th.Items.Insert(1, new ListItem("父子", "0"));                DDLFamilyRelationship3th.Items.Insert(2, new ListItem("母子", "0"));                DDLFamilyRelationship3th.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship3th.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship3th.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship4th.Items.Insert(1, new ListItem("父子", "0"));                DDLFamilyRelationship4th.Items.Insert(2, new ListItem("母子", "0"));                DDLFamilyRelationship4th.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship4th.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship4th.Items.Insert(5, new ListItem("姐弟", "0"));            }            if (DDLSex.Text == "女")            {                DDLFamilyRelationship1st.Items.Insert(1, new ListItem("父女", "0"));                DDLFamilyRelationship1st.Items.Insert(2, new ListItem("母女", "0"));                DDLFamilyRelationship1st.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship1st.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship1st.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship2nd.Items.Insert(1, new ListItem("父女", "0"));                DDLFamilyRelationship2nd.Items.Insert(2, new ListItem("母女", "0"));                DDLFamilyRelationship2nd.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship2nd.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship2nd.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship3th.Items.Insert(1, new ListItem("父女", "0"));                DDLFamilyRelationship3th.Items.Insert(2, new ListItem("母女", "0"));                DDLFamilyRelationship3th.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship3th.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship3th.Items.Insert(5, new ListItem("姐弟", "0"));                DDLFamilyRelationship4th.Items.Insert(1, new ListItem("父女", "0"));                DDLFamilyRelationship4th.Items.Insert(2, new ListItem("母女", "0"));                DDLFamilyRelationship4th.Items.Insert(3, new ListItem("夫妻", "0"));                DDLFamilyRelationship4th.Items.Insert(4, new ListItem("兄妹", "0"));                DDLFamilyRelationship4th.Items.Insert(5, new ListItem("姐弟", "0"));            }            else            {                DDLFamilyRelationship2nd.Items.Insert(0, new ListItem("-请选择-", "0"));            }        }    } 



[解决办法]
如果你希望通过DDLSex的选择自动更新其他DropDownList数据,请将DDLSex的AutoPostBack设定为true,另外你的所有ListItem的Value均为零,我想是不准备使用SelectedValue属性,重新组织逻辑如下,请按需要修改。
C# code
string[] arr = new string[0] { };if (DDLSex.Text == "男"){    arr = new[] { "-请选择-", "父子", "母子", "夫妻", "兄妹", "姐弟" };}else if (DDLSex.Text == "女"){    arr = new[] { "-请选择-", "父女", "母女", "夫妻", "兄妹", "姐弟" };}else{    //..}DDLFamilyRelationship1st.DataSource = arr;DDLFamilyRelationship1st.DataBind();DDLFamilyRelationship2nd.DataSource = arr;DDLFamilyRelationship2nd.DataBind();DDLFamilyRelationship3th.DataSource = arr;DDLFamilyRelationship3th.DataBind();DDLFamilyRelationship4th.DataSource = arr;DDLFamilyRelationship4th.DataBind(); 

热点排行