原来DropDownList1的集合中有内容,现在通过数据库得到了另一个值,只要求将得到的值显示在DropDownList,就像默认设置Selected="True"一样的效果,不要求将值加入到集合中,集合中的数据保持不变化。我试过将DropDownList1.SelectedItem.Text=“要赋的值”;但是这样原来集合中的数据减少了一个。希望高手指点一下,谢谢!

解决方案 »

  1.   

    DropDownList1.Items.Add(new ListItem("要赋的值", ""));
      

  2.   

    前台:
    <head runat="server">
        <title></title>
        <style type="text/css">
            .cls1
            {
                position: absolute;
                z-index: 1;
                width: 100px;
            }
            .cls2
            {
                position: absolute;
                z-index: 2;
                width: 80px;
            }
        </style>
        <script type="text/javascript">
            function set() {
           
                var ddl = document.getElementById("DropDownList1");
                var rect = ddl.getBoundingClientRect();
                var tb = document.getElementById("TextBox1");
                tb.style.top = rect.top;
                tb.style.left = rect.left;
                           }
        </script>    
    </head>
    <body onload="set();">
        <form id="form1" runat="server">  
            <asp:DropDownList ID="DropDownList1" runat="server" class="cls1">
            </asp:DropDownList>
           <asp:TextBox ID="TextBox1" runat="server" class="cls2"></asp:TextBox>
    </body>后台给文本框赋值不用说了吧
      

  3.   

    你从数据库得到的那一个值在原先的DropDownList绑定过后的集合中存在嘛???是的话,这样:
    string s = "BB"; //假设从数据库中获取的一个值
    this.DropDownList1.Items.FindByText(s).Selected = true;
      

  4.   

    那么你加ViewState或者cookie 用键值对的形式存起来!
      

  5.   

    你从数据库取值出来说要在DropDownList1里显示,但是你说又要不加如里面的集合!你这不是想吃喜欢懒得削皮!
    不过这种效果也不是没用:可以先加入进去,当你点击下拉的时候通过JS将这个显示的列移除!
      

  6.   


    你应该就是我说的应该是效果而不是不加值,不加值只是假象,要么还有就用个div绝对定位到DropDownList1 上面这样也可以!
      

  7.   


    所以是特殊需要么!是这样,数据库中原来有些不规范的格式,想通过这种方式让用户在修改的时候选择正确的格式,但是修改时显示的还必须是原来数据,只是DropDownList1集合中将不保存原来数据,是存在指定的内容。
      

  8.   

    我顶,如果不存在的话,像我那样写就会报错,因为找不着对象。如果不存的话,那就只能先将这个值添加进去,然后让其被选中状态。
    string s = "BB";
    this.DropDownList1.Items.Insert(this.DropDownList1.Items.Count-1,s);
    this.DropDownList1.SelectedIndex = this.DropDownList1.Items.Count-1;在前面的话,如果首次加载的话,先清空你的DropDownList绑定的数据,然后重新绑定后,在执行前面的操作。
      

  9.   

        <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>    <script language="javascript" type="text/javascript">
        var oldhtml="";
        function  add()
        {
            var addvalue=$("#txtadd").val();
            if(addvalue.length>0)
            {
            
                var html=  $("#select").html();
                oldhtml=html;
                html+="<option name='selectoption' value='-999'>"+addvalue+"</option>";
                
                $("#select").html(html);
                $("option[name='selectoption']").each(function(i){
                    if($(this).val()=="-999")
                    {
                       $(this).attr("selected",true); 
                    }            });
            }
            else
            {
                alert("添加值不能为空!");
            }
        }
        
        function deletevalue()
        {
            $("#select").html(oldhtml); 
        }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            输入:<input id="txtadd" type="text" />
            <input id="Button1" type="button" value="添加值" onclick="add()" />
            <select id="select" onclick="deletevalue()">
                <option name="selectoption"  value="1">1</option>
                <option name="selectoption" value="2">2</option>
                <option name="selectoption" value="3">3</option>
            </select>
        </div>
        </form>
    </body>
    </html>
      

  10.   

    不过你也可以点击事件的时候把加进来的option 的html清空掉设置成不可选这样效果稍微好些!
      

  11.   

    AUTOITEMCOLUMNS=FALSE;DROP.ITEMS.ADD(NEW LISTITEM("TEXT","VALUE"));DROP.SELECTVALUE="TEXT";
      

  12.   

    DropDownList1.Text="要赋的值"不可以吗?
      

  13.   

    不是吧,我在前台加一个新的DropDownList2,在后台写一句DropDownList2.Text="要赋的值"在Page_Load里这样都不行,运行后什么都不显示如下图
      

  14.   

    我觉得用DropDownList2.Text="要赋的值"这样是不对的,不然你在实际环境中试试。
      

  15.   

    不是的,你看看我开始说的,我不要求加到集合中去,我要求只要开始显示就好了,集合中已有的数据保持不变,我记得以前asp能实现。
      

  16.   

        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
            <asp:ListItem>哈哈</asp:ListItem>
            <asp:ListItem>嘿嘿</asp:ListItem>
            <asp:ListItem>嘻嘻</asp:ListItem>
        </asp:DropDownList>
    你要加到哪儿?尾部的话就直接this.DropDownList2.Items.Add(new ListItem("呵呵", "hehe"));
    //或者
    DropDownList2.Items.Insert(DropDownList2.Items.Count, "忽忽");
    添加到第一个 //添加到开始
            DropDownList2.Items.Insert(0, "请选择");
      

  17.   

    没办法了,我现在只能这样了<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
    string s = "5";
                if (this.DropDownList1.Items.FindByText(s) == null)
                {
                    //this.DropDownList1.Items.Insert(this.DropDownList1.Items.Count, s);
                    //this.DropDownList1.SelectedIndex = this.DropDownList1.Items.Count-1;
                }
                else
                    this.DropDownList1.Items.FindByText(s).Selected = true;而我本意是想保持原集合内容不变,新的值指示开始显示一下就好了,当用户选择下拉列表是里面就还是只有1、2、3而不再有5了。但是现在只能是先把5加进去才能显示,采纳了6楼、16楼综合起来的内容改进的,但是还不是我本意。
      

  18.   

    ListItem li = new ListItem("要显示的值", "数据库的值");
                              
    ddl.Items.Add(li);
    string s = ddl.Text;
      

  19.   

    我觉得你可以 在需要时添加这个 要显示在DDL中的值,然后在DDL的onfocus中调用JS删除你要的值<html>
    <head runat="server">
        <title>无标题页</title>
        <script type="text/javascript">
            function RemoveDDL()
            {
                document.getElementById('<%=DropDownList1.ClientID %>').options.remove(0);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" onfocus="RemoveDDL();" runat="server" Height="19px" Width="206px">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
        
        </div>
        </form>
    </body>
    </html>
      

  20.   

    ListItem x = DropDownList1.Items.FindByText(s);
    if(x!=null) x.Selected=true;
      

  21.   

    这样不行,成了欺骗使用者了,如果原来集合里有1、2、3,而让1这个值显示的是5,即ListItem li = new ListItem("5", "1");那么用户想选1的时候却看不到1,会认为没有想要的值。
      

  22.   

    那x==null呢?怎么既让他能显示同时又不改变DropDownList1.Items里的内容呢?
      

  23.   

    ddlItem.Items.Insert(0, new ListItem("这样", "恩,这样"));
      

  24.   

    你这个需求不是一般的变态
    ListItem x = DropDownList1.Items.FindByText(s);
    if(x!=null) x.Selected=true;
    x==null的时候还不能加入下拉 那加不加入数据库呢 
      

  25.   

    呵呵,这个方法奏效,只是有一点小瑕疵,就是当第一次点下拉列表的时候由于运行了RemoveDDL()
    产生了一块空白,第二次再点就好了,不过已经可以了,最起码达到想要的样了,看看图。
    1、运行后先显示原来数据库中的数据52、第一次点击下拉列表后的效果,5消失了,原来集合内容1、2、3不变,唯一遗憾就是多了一块空白。3、第二次再点击后一切恢复正常我想可以结贴了!非常感谢44楼的
      

  26.   

    我测试了下不会出现空白<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="PTC.test" %><!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>
        <script type="text/javascript">
            function RemoveDDL()
            {
                document.getElementById('<%=DropDownList1.ClientID %>').options.remove(4);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" onfocus="RemoveDDL();" runat="server" Height="19px" Width="206px">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                <asp:ListItem>4</asp:ListItem>
            </asp:DropDownList>
        
        </div>
        </form>
    </body>
    </html>
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    ListItem li = new ListItem("5", "5");
                    DropDownList1.Items.Add(li);
                    DropDownList1.SelectedIndex = 4;
                }
                
            }
        }
    缺点确实有,就是点击第一次时执行JS删除了其中一项,但是下拉列表没出现,点击第二次才能选下拉列表的值