绑定DropDownList的中的值

来源:互联网 发布:卓讯外贸数据 编辑:程序博客网 时间:2024/05/01 18:29

一、DropDownList

   1.1 DropDownList绑定数据

   1.1.1 DropDownList 固定绑定

       这种方式适合那些已经固定的数据绑定到DropDownList上。

     例

            <asp:DropDownList  runat="server" ID="ddlArea" Width="120px" >

                  <asp:Listitem value="0">选择项</asp:Listitem>    

                  <asp:Listitem value="1">第一项</asp:Listitem>   

                   <asp:Listitem value="2">第二项</asp:Listitem>                       
             </asp:DropDownList>

 

                      

 1.1.2 DropDownList 动态绑定

    前台:    

               

   后台:两种方法:(注意,每次绑定都要清除一下原来的记录,例:ddlArea.Items.Clear();)

    第一种:

        SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
        SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
        DataTable dt = new DataTable();
        dap.Fill(dt);
        DropDownList1.Items.Clear();
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "job_desc";
        DropDownList1.DataValueField = "job_id";
        DropDownList1.DataBind();

       DropDownList1.Items.Insert(0, new ListItem("选择数据", "随机绑定"));//插入默认项,此举必须放到数据绑定之后效果:

   第二种:
         SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
        SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
        DataTable dt = new DataTable();
        dap.Fill(dt);
        if (dt.Rows.Count != 0)
            {   

               DropDownList1.Items.Clear();             
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                 DropDownList1.Items.Add(new ListItem(dt.Rows[i]["显示值"].ToString(), dt.Rows[i]["usbkey"].ToString()));
                }
                DropDownList1.Items.Insert(0, "选择网吧");
                DropDownList1.Items[0].Value = "0"; 或

// DropDownList1.Items.Insert(0, new ListItem("选择数据", "随机绑定"));//插入默认项,此举必须放到数据绑定之
            }

        else

        {

                 DropDownList1.Items.Insert(0, "无网吧记录");
                DropDownList1.Items[0].Value = "0";
//或DroDownList.Item[0].Select=true;

        }

二DropDownList1的取值问题:

  2.1 取DropDownList1的索引值,也就是选择 value 值<asp:Listitem value="1">第一项</asp:Listitem>   取1


    .net中 DropDownList1.SelectedValue.ToString()
javascirpt  var ddl1=document.getElementByIdx_x("DropDownList1").selectedIndex;

 2.2 取DropDownList1的选项,也就是选择item值<asp:Listitem value="1">第一项</asp:Listitem>  取 第一项


.net 中DropDownList1.SelectedItem.ToString();
//DropDownList1.SelectedItem.text;

javascript  document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value

三DropDownList1事件问题:

    重点:使用OnTextChanged,OnSelectedIndexChanged事件时,必须设置   

<asp:DropDownList runat="server"    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1">

四如何避免DropDownList下拉框中的值重复添加?

AppendDataBoundItems是否填加重复值。真为添加,假为不填加

原因:DropDownList控件AppendDataBoundItems属性设置为"True"了的,改为False即可。
例如:如果专业后的DropDownList控件AppendDataBoundItems属性设置为"True",那么选择院系后专业里的值会不断添加。

五区别

  ddl.Items.Insert(0,new   ListItem("不选该项","0"));  这是在首项添加数据。  
  Items.Add是在最后添加

 DropDownList1.Items.Add(new   ListItem("Text","value"));   是在最后添加
  DropDownList1.Items.Insert(Index,new   ListItem("Text","value"));这是在首项添加数据。

六:从数据库中读取数据,并绑定到DropDownList中

if (dt.Rows[0]["Value"].ToString ()=="True")
   {
    DropDownListState.Items.FindByValue("1").Selected =true;
   }
   else
   {
    DropDownListState.Items.FindByValue("0").Selected =true;
   }

原创粉丝点击