原来DropDownList1的集合中有内容,现在通过数据库得到了另一个值,只要求将得到的值显示在DropDownList,就像默认设置Selected="True"一样的效果,不要求将值加入到集合中,集合中的数据保持不变化。我试过将DropDownList1.SelectedItem.Text=“要赋的值”;但是这样原来集合中的数据减少了一个。希望高手指点一下,谢谢!
解决方案 »
- 这个验证码怎么输入http://bbs.yiyacn.com/register.php
- 关于取得GRIDVIEW的TemplateField中获取TEXTBOX值的问题!请高手帮忙。急!!!
- 存储过程中的事务处理
- 从第一个窗口下得到一个值,传到第二个窗口里面,而且第一个窗口不刷新,当前窗口变为第二个窗口
- 请大家帮忙看一下这个sql怎么写(特别是高手,解决即给分)?
- 数据输出问题(在线等待大家解答)
- 用asp.net进行移动设备应用程序开发时的问题
- Linq to SQL 怎么更新数据库结构??
- 关于实现一个HyperLink集的问题!
- 求助!SQL Server 2005 配置SQLServer Session搭建总是出错!
- 如何看待把后台代码写到前台的写法?
- 求救!!哪位大哥能帮忙把这个字符串搞一下,谢谢谢谢
<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>后台给文本框赋值不用说了吧
string s = "BB"; //假设从数据库中获取的一个值
this.DropDownList1.Items.FindByText(s).Selected = true;
不过这种效果也不是没用:可以先加入进去,当你点击下拉的时候通过JS将这个显示的列移除!
你应该就是我说的应该是效果而不是不加值,不加值只是假象,要么还有就用个div绝对定位到DropDownList1 上面这样也可以!
所以是特殊需要么!是这样,数据库中原来有些不规范的格式,想通过这种方式让用户在修改的时候选择正确的格式,但是修改时显示的还必须是原来数据,只是DropDownList1集合中将不保存原来数据,是存在指定的内容。
string s = "BB";
this.DropDownList1.Items.Insert(this.DropDownList1.Items.Count-1,s);
this.DropDownList1.SelectedIndex = this.DropDownList1.Items.Count-1;在前面的话,如果首次加载的话,先清空你的DropDownList绑定的数据,然后重新绑定后,在执行前面的操作。
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>
<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, "请选择");
<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楼综合起来的内容改进的,但是还不是我本意。
ddl.Items.Add(li);
string s = ddl.Text;
<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>
if(x!=null) x.Selected=true;
ListItem x = DropDownList1.Items.FindByText(s);
if(x!=null) x.Selected=true;
x==null的时候还不能加入下拉 那加不加入数据库呢
产生了一块空白,第二次再点就好了,不过已经可以了,最起码达到想要的样了,看看图。
1、运行后先显示原来数据库中的数据52、第一次点击下拉列表后的效果,5消失了,原来集合内容1、2、3不变,唯一遗憾就是多了一块空白。3、第二次再点击后一切恢复正常我想可以结贴了!非常感谢44楼的
<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删除了其中一项,但是下拉列表没出现,点击第二次才能选下拉列表的值