zoukankan      html  css  js  c++  java
  • DropDownList 绑定(转载)

    一: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"; 


    二: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(); 
    javascript document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value 
    三:DropDownList1事件问题: 
    重点:使用OnTextChanged,OnSelectedIndexChanged事件时,必须设置 

    复制代码代码如下:

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


    OnTextChanged,OnSelectedIndexChanged这两个事件具体有什么区别,我也没测试出来,只知道OnSelectedIndexChanged这个事件要比OnTextChanged执行的早,也就是如果这两个事件都存在,会首先执行OnSelectedIndexChanged这个事件,然后才执行OnTextChanged. 
    四:如何避免DropDownList下拉框中的值重复添加? 
    AppendDataBoundItems是否填加重复值。真为添加,假为不填加 
    原因:DropDownList控件AppendDataBoundItems属性设置为"True"了的,改为False即可。 
    例如:如果专业后的DropDownList控件AppendDataBoundItems属性设置为"True",那么选择院系后专业里的值会不断添加。 
    五:区别 

    复制代码代码如下:

    depart_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 (ds.Tables[0].Rows[0]["State"].ToString ()=="True") 

    DropDownListState.Items.FindByValue("1").Selected =true; 

    else 

    DropDownListState.Items.FindByValue("0").Selected =true; 
  • 相关阅读:
    Zend Framework 项目 index.php 的问题
    《MySQL 必知必会》读书总结
    phpstorm 无法格式化代码
    Windows 下 zip 版的 MySQL 的安装
    配置Windows下的PHP开发环境
    phpstorm 激活服务器
    《DOM Scripting》
    《JavaScript高级程序设计》
    sql 查找所有已经分配部门的员工
    sql 表的连接 inner join、full join、left join、right join、natural join
  • 原文地址:https://www.cnblogs.com/lh123/p/3868088.html
Copyright © 2011-2022 走看看