ListBox1控件在页面上显示是有数据的!但在后台就是为NULL
public void dropForListBox()
{
if (DropDownList1.SelectedIndex.Equals(0))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
//this.ListBox1.SelectedIndex
this.ListBox1.DataSource = ds.Tables["变电站"];
this.ListBox1.DataTextField = "站名称";
this.ListBox1.DataValueField = "局编号";
this.ListBox1.DataBind();
con.Close();
}
else
{
if (DropDownList1.SelectedIndex.Equals(1))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 开闭所", con);
DataSet ds = new DataSet();
sda.Fill(ds, "开闭所");
this.ListBox1.DataSource = ds.Tables["开闭所"];
this.ListBox1.DataTextField = "开闭所名称";
this.ListBox1.DataValueField = "开闭所序号";
this.ListBox1.DataBind();
con.Close();
}
else
{
if (DropDownList1.SelectedIndex.Equals(2))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 馈线", con);
DataSet ds = new DataSet();
sda.Fill(ds, "馈线");
this.ListBox1.DataSource = ds.Tables["馈线"];
this.ListBox1.DataTextField = "馈线名称";
this.ListBox1.DataValueField = "馈线序号";
this.ListBox1.DataBind();
con.Close();
} }
}
} protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dropForListBox();
Response.Write(ListBox1.SelectedItem);
if (this.RadioButton1.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站 where 站名称 ='"+this.ListBox1.SelectedValue+"'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
else
{
if (this.RadioButton2.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
else
{
if (this.RadioButton3.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
}
}
}
public void dropForListBox()
{
if (DropDownList1.SelectedIndex.Equals(0))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
//this.ListBox1.SelectedIndex
this.ListBox1.DataSource = ds.Tables["变电站"];
this.ListBox1.DataTextField = "站名称";
this.ListBox1.DataValueField = "局编号";
this.ListBox1.DataBind();
con.Close();
}
else
{
if (DropDownList1.SelectedIndex.Equals(1))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 开闭所", con);
DataSet ds = new DataSet();
sda.Fill(ds, "开闭所");
this.ListBox1.DataSource = ds.Tables["开闭所"];
this.ListBox1.DataTextField = "开闭所名称";
this.ListBox1.DataValueField = "开闭所序号";
this.ListBox1.DataBind();
con.Close();
}
else
{
if (DropDownList1.SelectedIndex.Equals(2))
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from 馈线", con);
DataSet ds = new DataSet();
sda.Fill(ds, "馈线");
this.ListBox1.DataSource = ds.Tables["馈线"];
this.ListBox1.DataTextField = "馈线名称";
this.ListBox1.DataValueField = "馈线序号";
this.ListBox1.DataBind();
con.Close();
} }
}
} protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dropForListBox();
Response.Write(ListBox1.SelectedItem);
if (this.RadioButton1.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站 where 站名称 ='"+this.ListBox1.SelectedValue+"'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
else
{
if (this.RadioButton2.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
else
{
if (this.RadioButton3.Checked)
{
SqlConnection con = new SqlConnection("server=gea-server;database=cqscada;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select 站名称 as 名称 from 变电站", con);
DataSet ds = new DataSet();
sda.Fill(ds, "变电站");
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
}
}
}
2 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 时:把dropForListBox()代码屏蔽掉。
2 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 时:把dropForListBox()代码屏蔽掉。这样是ListBox1.SelectedValue和ListBox1.SelectedItem有了!但是又出现了个新的问题!ListBox1的ListBox1_SelectedIndexChanged事件只触发一次!觉得很奇怪,我AutoPostback= true了!但当我选ListBox1的一个Item后换成别的Item就是不触发ListBox1_SelectedIndexChanged事件!也就是 GridView1控件不刷新