using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Waxie.WaxiePacket
{
/// <summary>
/// product_new1 的摘要说明。
/// </summary>
public class product_new1 : Waxie.Page
{
protected System.Web.UI.WebControls.Label LabelPType;
protected System.Web.UI.WebControls.TextBox TextBoxPCode;
protected System.Web.UI.WebControls.Label LabelCName;
protected System.Web.UI.WebControls.TextBox TextBoxPName;
protected System.Web.UI.WebControls.TextBox TextBoxH;
protected System.Web.UI.WebControls.TextBox TextBoxL;
protected System.Web.UI.WebControls.TextBox TextBoxA;
protected System.Web.UI.WebControls.TextBox TextBoxB;
protected System.Web.UI.WebControls.TextBox TextBoxH1;
protected System.Web.UI.WebControls.TextBox TextBoxL1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.DropDownList DropDownList11;
protected System.Web.UI.WebControls.DropDownList DropDownList12;
protected System.Web.UI.WebControls.DropDownList DropDownList21;
protected System.Web.UI.WebControls.DropDownList DropDownList22;
protected System.Web.UI.WebControls.DropDownList DropDownList31;
protected System.Web.UI.WebControls.DropDownList DropDownList32;
protected System.Web.UI.WebControls.DropDownList DropDownList41;
protected System.Web.UI.WebControls.DropDownList DropDownList42;
protected System.Web.UI.WebControls.DropDownList DropDownList51;
protected System.Web.UI.WebControls.DropDownList DropDownList52;
protected System.Web.UI.WebControls.DropDownList DropDownList61;
protected System.Web.UI.WebControls.DropDownList DropDownList62;
protected System.Web.UI.WebControls.Label LabelMsg;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBoxW1;
protected System.Web.UI.WebControls.TextBox TextBoxW2;
protected System.Web.UI.WebControls.TextBox TextBoxW3;
protected System.Web.UI.WebControls.TextBox TextBoxW4;
protected System.Web.UI.WebControls.TextBox TextBoxW5;
protected System.Web.UI.WebControls.TextBox TextBoxW6;
protected System.Web.UI.WebControls.TextBox TextBoxA1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
int PID=0;
int CID=0;
ViewState["PState"]="New";
if(Request.QueryString["ProductID"]!=null)
{
PID=int.Parse(Request.QueryString["ProductID"]);
ViewState["PState"]="Edit";
}
ViewState["PID"]=PID;
switch((string)ViewState["PState"])
{
case "New":
if(Request.QueryString["typename"]==null||Request.QueryString["typename"]=="")
throw(new Exception("NO-typename"));
LabelPType.Text=Request.QueryString["typename"];
if(Request.QueryString["CustomerID"]==null||Request.QueryString["CustomerID"]=="")
throw(new Exception("NO-CustomerID"));
CID=int.Parse(Request.QueryString["CustomerID"]);
break;
case "Edit":
break;
default:
throw(new Exception("?"));
break;
}
DataSet ds=GetMetialsDataSet();
BindDDL(DropDownList11,DropDownList12);
BindDDL(DropDownList21,DropDownList22);
BindDDL(DropDownList31,DropDownList32);
BindDDL(DropDownList41,DropDownList42);
BindDDL(DropDownList51,DropDownList52);
BindDDL(DropDownList61,DropDownList62);
}
} private DataSet GetMetialsDataSet()
{
DataSet ds=(DataSet)Cache["Metials"];
if(ds!=null)return ds;
lock("Cache_Metials")
{
ds=(DataSet)Cache["Metials"];
if(ds!=null)return ds;
string sql="SELECT 材料类型.类型,材料价格.名字,材料价格.材料价格ID FROM (SELECT DISTINCT 类型 FROM 材料价格) AS 材料类型 JOIN 材料价格 ON 材料类型.类型 = 材料价格.类型 ORDER BY 材料类型.类型, 材料价格.名字 FOR XML AUTO,XMLDATA";
SqlConnection conn=GetConnection();
SqlCommand cmd=new SqlCommand(sql,conn);
ds=new DataSet("Metials");
ds.ReadXml(cmd.ExecuteXmlReader(),XmlReadMode.Fragment);
Cache["Metials"]=ds;
return ds;
}
} private void BindDDL(DropDownList ddl1,DropDownList ddl2)
{
DataSet ds=GetMetialsDataSet();
ArrayList a=new ArrayList();
a.Add("没有材料");
foreach(DataRow row in ds.Tables["材料类型"].Rows)
{
a.Add(row["类型"].ToString());
}
if(ddl1.Items.Count==0)
{
ddl1.DataSource=a;
ddl1.DataBind();
ddl1.SelectedIndex=1;
ddl2.Items.Clear();
ddl2.Items.Add("请选择一种材料");
}
else
{
if(ddl1.SelectedIndex==0)
{
string[] ss={"请选择一种材料"};
ddl2.Items.Clear();
ddl2.Items.Add("请选择一种材料");
}
else
{
ListItemCollection ls=new ListItemCollection();
foreach(DataRow row in ds.Tables["材料类型"].Rows[ddl1.SelectedIndex-1].GetChildRows(ds.Relations[0]))
{
ls.Add(new ListItem(row["名字"].ToString(),row["材料价格ID"].ToString()));
}
ddl2.DataSource=ls;
ddl2.DataBind();
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DropDownList11.SelectedIndexChanged += new System.EventHandler(this.DropDownList11_SelectedIndexChanged);
this.DropDownList21.SelectedIndexChanged += new System.EventHandler(this.DropDownList21_SelectedIndexChanged);
this.DropDownList31.SelectedIndexChanged += new System.EventHandler(this.DropDownList31_SelectedIndexChanged);
this.DropDownList41.SelectedIndexChanged += new System.EventHandler(this.DropDownList41_SelectedIndexChanged);
this.DropDownList51.SelectedIndexChanged += new System.EventHandler(this.DropDownList51_SelectedIndexChanged);
this.DropDownList61.SelectedIndexChanged += new System.EventHandler(this.DropDownList61_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DropDownList11_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList11,DropDownList12);
} private void DropDownList21_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList21,DropDownList22);
} private void DropDownList31_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList31,DropDownList32);
} private void DropDownList41_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList41,DropDownList42);
} private void DropDownList51_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList51,DropDownList52);
} private void DropDownList61_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindDDL(DropDownList61,DropDownList62);
} }
}

解决方案 »

  1.   

    protected System.Web.UI.WebControls.DropDownList DropDownList11;
    protected System.Web.UI.WebControls.DropDownList DropDownList12;
    protected System.Web.UI.WebControls.DropDownList DropDownList21;
    protected System.Web.UI.WebControls.DropDownList DropDownList22;
    protected System.Web.UI.WebControls.DropDownList DropDownList31;
    protected System.Web.UI.WebControls.DropDownList DropDownList32;
    protected System.Web.UI.WebControls.DropDownList DropDownList41;
    protected System.Web.UI.WebControls.DropDownList DropDownList42;
    protected System.Web.UI.WebControls.DropDownList DropDownList51;
    protected System.Web.UI.WebControls.DropDownList DropDownList52;
    protected System.Web.UI.WebControls.DropDownList DropDownList61;
    protected System.Web.UI.WebControls.DropDownList DropDownList62;是6个Select组合。
    例如
    protected System.Web.UI.WebControls.DropDownList DropDownList11;
    protected System.Web.UI.WebControls.DropDownList DropDownList12;当DDL11改动后,返回输出新的DDL12的选项。但是问题出现了。
    无论是由于Select的onchange引发postback,还是按一个普通的submit来postback
    所有的SelectedIndexChanged都触发了。
    而且所有的SelectedIndex都为0。。真是找不到为什么这样了。。只好怪罪ViewState没有记住每个DropDownList的状态?