日期:2014-05-17  浏览次数:20362 次

救命啊…………这个问题可怎么解决?
我定义了这么一个下拉列表
<asp:DropDownList ID="DropDownList1" 
  runat="server" AutoPostBack="True">
  </asp:DropDownList>
动态绑定的数据源,但是每次点击都会回第一项,怎么回事啊?
protected void Page_Load(object sender, EventArgs e)
  {
  if (!this.IsPostBack)
  {
  LoginUser.Text = Convert.ToString(Session["username"]);
  int machineid = 1;
  GetDBconn dbconn = new GetDBconn(machineid);
  string strConString = dbconn.GetLocalConnStr();
  SqlConnection conn = new SqlConnection(strConString);
  string strSQL = "select machine_name,conn_string from conn_string order by id";
  SqlCommand cmd = new SqlCommand(strSQL, conn);
  cmd.Connection.Open();
  SqlDataReader myReader = cmd.ExecuteReader();
  while (myReader.Read())
  {
  DropDownList1.Items.Add(new ListItem(myReader["machine_name"].ToString(), myReader["conn_string"].ToString()));
  }
  DropDownList1.SelectedIndex = Convert.ToInt32(Session["machineid"]);
  cmd.Connection.Close();
  }
  }

我在加载页面的时候会自动在数据库的表中将数据绑定到这个下拉列表,但是不管点哪个值他就回第一项了。
PS:我是在母板页中定义的这个下拉列表

高手帮忙了!!!!!!!!!!!!!!!!!!!!

------解决方案--------------------
DropDownList1.SelectedIndex = Convert.ToInt32(Session["machineid"]);
这个。。。。。。。。。。。。。。。。。
------解决方案--------------------
AutoPostBack="True" 这个去掉看看
------解决方案--------------------
DropDownList1.SelectedIndex = Convert.ToInt32(Session["machineid"]);

这句应该放到if外面的,if里面是初始化加载,以后点击,每次都触发Postback,不会走if,这句话当然起不了作用了。