DataTable idt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//求得下拉框的参数
Maticsoft.BLL.Hpm bll = new Maticsoft.BLL.Hpm();
idt = bll.GetAllList().Tables[0];
//部门
DataView dv1 = new DataView(idt);
dv1.RowFilter = ("parentcode='10'");
txtha03.DataSource = dv1;
txtha03.DataTextField = "cinvcname";
txtha03.DataValueField = "no";
txtha03.DataBind();
}
}
protected void txtha03_SelectedIndexChanged(object sender, EventArgs e)
{
//班组车间
DataView dv2 = new DataView(idt);
dv2.RowFilter = ("parentcode='" + txtha03.SelectedValue.ToString() + "'");
txtha04.DataSource = dv2;
txtha04.DataTextField = "cinvcname";
txtha04.DataValueField = "no";
txtha04.DataBind();
}
以上是我的代码,DataTable idt, 在击发 txtha03_SelectedIndexChanged事件的时候,txtha04 无法获得数据源,我在即时窗体中测 idt,居然是null(如下提示), 好奇怪的事情,但是txtha03依然存在下拉数据.
idt.Rows.Count
“((ASP.hpsn_hpsna_aspx)this).idt”为 null
------解决方案--------------------------------------------------------
当你刷新页面的时候idt也就不存在了
去掉这个判断if (!Page.IsPostBack)
保证每次加载时都能执行
------解决方案--------------------------------------------------------
每次poskback的时候,页面类都会被重新构造。
idt在新构造的类里面没有被赋值,因此是Null
可以!Page.IsPostBack外面给idt赋值。
或者将idt保存在viewstate中。
------解决方案--------------------------------------------------------
页面间利用ViewState保存DataTable数据
DataTable idt;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//求得下拉框的参数
Maticsoft.BLL.Hpm bll = new Maticsoft.BLL.Hpm();
idt = bll.GetAllList().Tables[0];
ViewState["dt"] = idt;
//部门
DataView dv1 = new DataView(idt);
dv1.RowFilter = ("parentcode='10'");
txtha03.DataSource = dv1;
txtha03.DataTextField = "cinvcname";
txtha03.DataValueField = "no";
txtha03.DataBind();
}
}
protected void txtha03_SelectedIndexChanged(object sender, EventArgs e)
{
//班组车间
DataView dv2 = new DataView((DataTable)ViewState["dt"]);
dv2.RowFilter = ("parentcode='" + txtha03.SelectedValue.ToString() + "'");
txtha04.DataSource = dv2;
txtha04.DataTextField = "cinvcname";
txtha04.DataValueField = "no";
txtha04.DataBind();
}