表中有数据
aa  bb
--- ---
f1  0
f2  0
f3  1
f4  0
aa是在DropDownList显示的值
当bb=1时DropDownList默认就是F3如何操作呀,谢了

解决方案 »

  1.   

    前台:
    <asp:DropDownList ID="ddlRateName" runat="server" Style="font-size: 11px; color: blue"  
                                            DataSource = '<%# BindRateName() %>' DataTextField = "rateName" SelectedValue='<%# Bind("rateName") %>'>
                                        </asp:DropDownList>后台:
     #region 绑定职称
        protected DataTableReader BindRateName()
        {
            string selectSql = "select distinct rateName from zd_employee where rateName is not null and rateName != '' order by rateName ";
            SqlData da = new SqlData();
            DataTableReader dr = null;        try
            {
                dr = da.ExceDr(selectSql);
            }
            catch
            {
                Response.Write("<script>alert('绑定职称信息失败!')</script>");
                return null;
            }        return dr;
        }
        #endregion
      

  2.   

    绑定DropDownList的时候可以绑定一个是显示值一个是value
    DropDownList1.DataTextField="aa"//显示的值
    DropDownList1.DataValueField="bb"隐藏的值,取的时候就用DropDownList1.SelectedValue
    这样就可以取到bb的值了
      

  3.   

    DropDownList有一个selected 属性,判断b的值是否等于1,然后取出相应的a值,然后赋值
      

  4.   

    SelectedValue 是这个属性,刚才说错了
      

  5.   

    this.DropDownList1.DataSource = dataTable;
    this.DropDownList1.DataTextField = "aa";
    this.DropDownList1.DataValueField = "bb";
    this.DropDownList1.DataBind();
    this.DropDownList1.SelectedValue = "1";
      

  6.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    InitData();
                }
            }        private void InitData()
            {
                DataTable table = GenerateDataTable();
                this.DropDownList1.DataSource = table;
                this.DropDownList1.DataTextField = "aa";
                this.DropDownList1.DataValueField = "bb";
                this.DropDownList1.DataBind();
            }        /// <summary>
            /// 产生DataTable
            /// </summary>
            /// <returns></returns>
            private DataTable GenerateDataTable()
            {
                DataTable table = new DataTable();
                DataColumn aa = new DataColumn("aa", typeof(string));
                DataColumn bb = new DataColumn("bb", typeof(int));
                table.Columns.Add(aa);
                table.Columns.Add(bb);            DataRow row = table.NewRow();
                row["aa"] = "f1";
                row["bb"] = 0;
                table.Rows.Add(row);            row = table.NewRow();
                row["aa"] = "f2";
                row["bb"] = 0;
                table.Rows.Add(row);            row = table.NewRow();
                row["aa"] = "f3";
                row["bb"] = 1;
                table.Rows.Add(row);            row = table.NewRow();
                row["aa"] = "f4";
                row["bb"] = 0;
                table.Rows.Add(row);            return table;
            }        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {            Response.Write(string.Format("您选择的值是:{0}", DropDownList1.SelectedValue));
            }
        }
    }<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged ="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
      

  7.   

    在加载数据或者绑定后添加这一句
    DropDownList1.Items.Insert(0, "F3");
      

  8.   

          
    1. 如果自己写数据绑定 方法:
             //绑定年      
             public static void BindYear(DropDownList list)
            {            list.Items.Clear();
                ListItem item = new ListItem("2010");
                list.Items.Insert(0, item);
                item = new ListItem("2009");
                list.Items.Insert(1, item);
                item = new ListItem("2008");
                list.Items.Insert(2, item);
            }2.如果查询数据库数据绑定 方法:
            //绑定部门下拉框
            public static void BindDept(DropDownList list)
            {
                DataSet ds = new DataSet();
                ds = 你的查询方法
                list.DataSource = ds;
                list.DataTextField = "deptname";
                list.DataValueField = "deptcode";
                list.DataBind();        }
      

  9.   

    设下拉列表的datatextfield这是给用户看的,datavaluefield是点击时它对应的值
      

  10.   

    谢谢大家了可我要是当bb=1时DropDownList默认值显示的是F3谢谢
      

  11.   

     DataTable table = 你获取数据的集合表
                this.DropDownList1.DataSource = table;
                this.DropDownList1.DataTextField = "aa";
                this.DropDownList1.DataValueField = "bb";
                this.DropDownList1.DataBind();
                foreach (ListItem item in  this.DropDownList1.Items)
                {
                    if (item.Value.ToString() == "1")
                    {
                        item.Selected = true;
                        break;
                    }
                }
      

  12.   

    楼上的我明白你的思路了,谢谢我想请问一下我是用控制SqlDataSource2的,我如何获取数据的集合表万分感谢
      

  13.   

    直接连接就是了 配置一下你的数据连接字符串 和sql语句
      

  14.   

    如果2个表有直接或间接关系,应该为两个表建立外键关系,这样的话实现DropDownList 联动就简单得多。
      

  15.   

    先将数据绑上
    再用SelectedValue属性来设置
      

  16.   

    将数据绑上去后再用DropDownList1.SelectedValue=“1”这个属性来设置下,搞定
      

  17.   

    楼上的我也知道DropDownList1.SelectedValue=“1”就可以默认值了,可我要求的是自动判断的
      

  18.   


    我非常喜欢这种写法。实际开发使用时,应该注意 BindRateName() 内部应该首先查询数据缓存,用查询Sql语句作为缓存单元的key进行查询,当没有缓存时才真正查询数据库。
      

  19.   

    楼主并没有说明怎么知道“默认值”是什么,所以很难回答。假设默认值使用页面上一个 GetDefaultValue() 方法来返回,那么那一行就可以设置为:DataSource = '<%# BindRateName() %>' SelectedValue='<%# GetDefaultValue() %>'
      

  20.   

    我已经说明了当bb=1时DropDownList默认就是F3
      

  21.   

    绑定DropDownList后用Foreach遍历下...
      

  22.   

    ddropdownlist 有 text 和 value 两个属性的 当text == 11   value 等于 F3
      

  23.   

           this.DropDownList1.DataValueField = "bb";
            this.DropDownList1.DataTextField = "aa";
      

  24.   

      datatable dt=取出数据.
      this.DropDownList1.DataSource = dt;
      this.DropDownList1.DataValueField = "bb";
      this.DropDownList1.DataTextField = "aa";
      this.DropDownList1.DataBind();