我通过  DropDownList.Items.Add(new ListItem(ss,"0")); 将string型ss 添加到了DropDownList。
现在DropDownList中只有ss一项。
怎样强制ss就是选定的值,然后通过DropDownList.SelectedValue获取选定的值ss

解决方案 »

  1.   

    DropDownList1.Items[0].Selected = true;DropDownList1.SelectedIndex = 0;
      

  2.   

    除非你是这种要求: 就是既显示ss项,又进行绑定, 然后提交时无论选择了那一项, 实际提交的还是ss那一项的value.这样的话, 第一是要给DropDownList增加AppendDataBoundItems="True" <div>
            <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
                DataSourceID="SqlDataSource1" DataTextField="CategoryName" 
                DataValueField="CategoryID">
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
            </asp:SqlDataSource>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    客户端用Page.ClientScript注册一段js代码, 用以在提交时更改DropDownList的选项.
    protected void Page_Load(object sender, EventArgs e)
    {
            DropDownList1.Items.Add(new ListItem("ss", "0"));
            if (!Page.ClientScript.IsOnSubmitStatementRegistered(this.GetType(), "key"))
            {
                Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "key", "document.getElementById('" + DropDownList1.ClientID + "').value=0;");
            }
        }
    protected void Button1_Click(object sender, EventArgs e)
    {   //用来测试实际提示的选项.
        Label1.Text = DropDownList1.SelectedValue;
    }
      

  3.   

    我是这样因为的
    当页面打开初始时  DropDownList1初始为ss
    而这时ss并不是选定项,。
    怎样初始页面时就指定ss是选定项
      

  4.   


    不会吧? 只要一个项时, 它就是默认的选定项啊. protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Items.Add(new ListItem("ss", "0"));    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = DropDownList1.SelectedValue;
        }
    <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <asp:Label ID="Label1" runat="server"
                Text="Label"></asp:Label>
    </div>就这么简单的测试一下, 点击Button1时, 在Label1上显示DropDownList当前的选定项, 这不明摆着吗?还是我理解有误?
      

  5.   

    只有一个item的话,那选中的就只能是这个item的
      

  6.   

    代码是这样的  帮帮我
    protected void Page_Load(object sender, System.EventArgs e)
            {
                GV.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
                
                if (!IsPostBack)
                {
                    //InitData();
                    selectchx.Items.Clear();
                    selectyqName.Text = Request.QueryString["mingcheng"];
                    string ss = Request.QueryString["leibie"];
                    selectchx.Items.Add(new ListItem(ss,"0"));  (意思这里默认就是选定项)
                    //string gao = selectchx.SelectedValue;
                    //Page.RegisterStartupScript("", "<script>alert('选择的是:" + gao + "')</script>");
                    //Response.Write(selectchx.SelectedItem.Text.ToString());
                    Query();
                    ResetQueryValue();
                    selectchx.Items.Clear();
                    InitData();
                }
            }        /// <summary>
            /// 初始化页面数据
            /// </summary>
            protected void InitData()
            {
                //初始化:类别下拉框中的数据,用Category表中的数据进行绑定
                DataTable dt = Category.Query(new Hashtable());
                selectchx.Items.Clear();
                selectchx.Items.Add(new ListItem("全部", ""));
                foreach (DataRow dr in dt.Rows)
                {
                    selectchx.Items.Add(new ListItem(dr["CategoryName"].ToString(), dr["CategoryId"].ToString()));
                }
            }
            /// <summary>
            /// 根据页面上用户输入的查询条件,查询仪器数据
            /// </summary>
            private void Query()
            {
                //初始化:GridView的数据源
                Hashtable queryItems = new Hashtable();
                queryItems.Add("yqName", selectyqName.Text);
                queryItems.Add("yqleibieId", selectchx.SelectedValue);  (或许是这里问题!但我不知道原因)
                DataTable dt = Yq.Queryyqs(queryItems);            GV.DataSource = dt;
                GV.DataBind();            //保存下拉框的选择项到ViewState数组对象
                ViewState.Add("selectchx", selectchx.SelectedValue);            LabelPageInfo.Text = "查询结果(第" + (GV.PageIndex + 1).ToString() + "页 共" + GV.PageCount.ToString() + "页)";
            }
            /// <summary>
            /// “查询”按钮单击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void selecebutton_Click(object sender, System.EventArgs e)
            {
                Query(); //查询仪器数据
                ResetQueryValue(); //恢复下拉框选择项
            }
      

  7.   

    我知道是哪里的问题了
    现在新的问题是这样,
    DropDownList1.Items.Add(new ListItem("A", "B")); 
    A是文本 B是值
    当数据源是数据库的一张表时
    当我知道了A怎样得到A所对应的B
    A 是 string
    B 是 int
      

  8.   

    还以为你是初学者呢, 原来根本不是:-)没看懂, 呵呵.selectchx.Items.Add(new ListItem(ss,"0"));  (意思这里默认就是选定项) 
    下面加个
    selectchx.SelectedValue = 0;不行吗?
      

  9.   

    其实啊我真是新手  我就是自己看书看的
    用哪里看哪里
    这种方法没想到
    哎 真是防不胜防啊!惭愧
    不过这样写提示 无法将类型“int”隐式转换为“string”
    格式该怎样写  不会
      

  10.   

    int.Parse(selectchx.SelectedValue)默认的SelectedValue为string类型,使用int.Parse方法可以把string类型转换为int类型
      

  11.   

    不对  本质上错了 
    因为queryItems.Add("yqleibieId", selectchx.SelectedValue); 得到的值是数据库表中已经定义好的。
    比如   A对应1    B对应2   C对应3
    当ss=B时 就要求所对应的值必须是2
    所以要根据B查询所对应的是2int g = 查询语句;
    selectchx.Items.Add(new ListItem(ss,"g"));
    这是g=2;
    该怎么写?
    谢谢各位
      

  12.   

    int g = select CategoryId From Category where CategoryName=ss;
    哈哈 sql语句
    大致这样  但是不对
      

  13.   

    用循环吧,得到这个值就将SELECT标记为TRUE
      

  14.   

    晕死:DropDownList1.Items.FindByText("ss").Selected=true
    DropDownList1.Items.FindByText("ss")获取的是Text属性值是ss的下拉项
    也可以试用
    DropDownList1.Items.FindByValue("1").Selected=true
      

  15.   

    asp.net属性菜单中有一个设定初始值的选项,输入想要显示在dropdownlist中的第一个数据就好了