我用ObjectDataSource空间绑定了一个DropDownList控件,方法返回的是一个dataset,
 public DataSet GetDepartmentName()
    {
        SqlConnection sqlConn = new SqlConnection(strConn);
        DataSet dataset = new DataSet();
        using (sqlConn)
        {
            sqlConn.Open();
            string strCmd = "select DepartmentName from DepartmentInformation";
            SqlCommand sqlCmd = new SqlCommand(strCmd, sqlConn);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
            adapter.Fill(dataset);
        }
        return dataset;
    }
然后DropDownList 也把数据都显示出来了 但是我调用的时候怎么样都是去的第一行的名字 怎么办啊

解决方案 »

  1.   

    一、dropdownlist控件的值绑定方法:
    1、直接输入item项
    <asp:DropDownList ID="DropDownList1" runat="server" >
      <asp:ListItem>张三</asp:ListItem>
      <asp:ListItem>李四</asp:ListItem>
    </asp:DropDownList>                      
    这恐怕是最简单的,看下面这种
    2、数据源控件绑定
    <asp:DropDownList ID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="name">                            </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
      SelectCommand="SELECT [name] FROM [yh]"></asp:SqlDataSource>
    这种实用、方便写,再看下面这种
    3、使用dataset或datareader绑定控件(以dataset为例)
    SqlDataAdapter da = new SqlDataAdapter("select id,name from hy",conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    conn.Close();
    DropDownList1.DataSource = ds.Tables[0];
    DropDownList1.DataTextField="name";
    DropDownList1.DataValueField = "id";
    DropDownList1.DataBind();
    这种高级一点,或许还有一些方法,发现中;
    二、而实际应用中,很多时候不是简单的一个绑定值那么简单,例如:当dropdownlist控件绑定值后,而你又希望指定初始值,就是显示的值,例子很多就不举了,下面是自己总结的几种方法(只放前后台主要代码):
    第一种:
    前台代码:
    <asp:DropDownList ID="DropDownList1" runat="server" >
      <asp:ListItem>张三</asp:ListItem>
      <asp:ListItem>李四</asp:ListItem>
    </asp:DropDownList>
    后台代码:
    DropDownList1.Item.Inset(0,"李四");//这是插入第一个值为李四;
    DropDownList.Items.FindByValue("李四").selected = true;//这是调用findbyvalue方法指定初始值;
    第二种:
    前台代码:
    <asp:DropDownList ID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="name">                            </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
      SelectCommand="SELECT  [name] FROM [yh]"></asp:SqlDataSource>
    后台代码:
    DropDownList1.SelectedValue = "李四";  //使用item方法貌似不行,会提示没有引入实例错误;
    第三种:
    前台代码:前面2种都可以;
    后台代码:
    DropDownList1.SelectedIndex = 1;//通过控件索引来指定,1代表第二个值;其实还有一种,比较经常用到,实例说明:(在此直观的说明)
    实例问题:绑定控件的值为id,但显示为name,同样首先指定默认值,通过选项,修改id;
    区别:默认值是通过数据库数值或传的数据来指定的,而不是指定一个默认字符串;
    解决方法:
    1、前台代码:
    <asp:DropDownList ID="DropDownList1" runat="server" >
      <asp:ListItem>张三</asp:ListItem>
      <asp:ListItem>李四</asp:ListItem>
    </asp:DropDownList>
    后台代码:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              string yhid = Request.Params["userid"].ToString();                    
              DropDownList1.Items.FindByValue(yhid).Selected= true;
    }
    }//这里只是简单阐述,如果是从dataset读出来的值,是一样的效果;
    2、前台代码:
    <asp:DropDownList ID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="id">                            </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
      SelectCommand="SELECT [id] [name] FROM [yh]"></asp:SqlDataSource>
    后台代码:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              string yhid = Request.Params["userid"].ToString();                    
    DropDownList1.SelectedValue = yhid; }
    }
    3、或者可以通过sql语句直接读取id所对应的name,就可以直接使用赋值了:三、DropDownList数据绑定第一项为空的方法
    DropDownList1.DataSource = ds.Tables[0];
    DropDownList1.DataTextField="name";
    DropDownList1.DataValueField = "id";
    DropDownList1.Items.Insert(0,new ListItem()); 按照上面的绑定完之后
    你想选择一项然后在数据库里查出对应的数据 返回dataset是吧?那就在Dropdownlist的事件里 string wherestr =dropdownlist.SelectedValue;然后把这个wherestr  传到SQL里。
      

  2.   

    页面回发了,设置dropdownList的ispostback,
      

  3.   

    在load事件里面加上page.page.ispostback   就好了、就像四楼说的页面回发啦
      

  4.   

    回发了。!ispostback。。贴图片很累吧,用《Csdn收音机》截图功能轻松解决!