dropdownlist控件动态添加
我想实现这样一个功能,用dropdownlist既可以自己写内容,又可以再下拉列表中选择一个作为输入内容,怎么实现?什么控件可以实现?...
我想实现这样一个功能,用dropdownlist既可以自己写内容,又可以再下拉列表中选择一个作为输入内容,怎么实现?什么控件可以实现?
展开
4个回答
展开全部
动态生成的dropdownlist
在页面重新加载时视图状态丢失,导致先前的设置都没有任何用途了。所以采取将dropdownlist当前选中的值通过javascript脚本保存在hidden域中的方式,可以避免设置信息丢失。在页面重新加载时将hidden域中的值取出即可。以下是一个名为testDropdown的web窗体测试代码。
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="testDropdown.aspx.cs"
Inherits="EmployeeInfoReferPurview_files_testDropdown" %>
<!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 language=javascript>
function getvalue(obj,obj1,obj2)
{
obj.value = obj1.value;
obj2.value = obj1.innertext;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h2>取下拉框值</h2>
<div>
<asp:Table ID="tbl"
runat="server">
</asp:Table>
<asp:Button ID="Button5" runat="server"
Text="取得Dropdownlist的下拉值" OnClick="Button5_Click"/><br />
<br />
<asp:TextBox ID="txtq" runat="server"
Width="871px"></asp:TextBox>
<br />
<asp:DropDownList ID="ddlq"
runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox></div>
</form>
</body>
</html>
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;
using System.Drawing;
public partial class
EmployeeInfoReferPurview_files_testDropdown : System.Web.UI.Page
{
DropDownList[] ddllst = new DropDownList[3];
ListItem li1 = new ListItem("q", "0");
ListItem li2 = new ListItem("w", "1");
ListItem li3= new ListItem("e", "2");
TextBox[] txtlst = new TextBox[3];
HiddenField[] hflst = new HiddenField[3];
HiddenField[] hftextlst = new
HiddenField[3];
protected override void OnInit(EventArgs e)
{
//createtable();
}
protected void Page_Load(object sender,
EventArgs e)
{
createtable();
if (!Page.IsPostBack)
{
for (int i = 0; i < 3; i++)
{
txtlst[i].ForeColor = Color.Red;
}
}
}
protected void createtable()
{
for (int i = 0; i < 3; i++)
{
TableRow tr = new TableRow();
tr.ID = "tr"+i;
TableCell tc = new TableCell();
tc.ID = "tc" + i;
DropDownList ddl = new
DropDownList();
ddl.ID = "ddl" + i;
ddl.Items.Add(li1);
ddl.Items.Add(li2);
ddl.Items.Add(li3);
//ddl.Items.FindByValue(li2.Value).Selected = true;
HiddenField hf = new HiddenField();
hf.ID = "hf" + i;
hf.Value = ddl.SelectedValue;
HiddenField hftext = new
HiddenField();
hftext.ID = "hftext" + i;
hftext.Value =
ddl.SelectedItem.Text;
ddl.Attributes.Add("onclick",
"javascript:getvalue(hf" + i + ",ddl" + i + ",hftext" + i + ");");
ddllst[i] = ddl;
hflst[i] = hf;
hftextlst[i] = hftext;
//HiddenField hf = new
HiddenField();
//hf.ID = "hf" + i;
//hf.Value = ddl.SelectedValue;
tc.Controls.Add(ddl);
tc.Controls.Add(hf);
tc.Controls.Add(hftext);
tr.Controls.Add(tc);
tbl.Rows.Add(tr);
TableRow tr1 = new TableRow();
tr1.ID = "tr1"+i;
TableCell tc1 = new TableCell();
tc1.ID = "tc1" + i;
TextBox txt = new TextBox();
txt.ID = "txt" + i;
//txt.ForeColor = Color.Red;
txtlst[i] = txt;
tc1.Controls.Add(txt);
tr1.Controls.Add(tc1);
tbl.Rows.Add(tr1);
}
}
protected void Button5_Click(object sender,
EventArgs e)
{
//int inta = ddllst.Length;
//string
valid=((DropDownList)e.Item.FindControl("ddlValid")).SelectedValue;
//string a = ddllst[0].SelectedValue;
//string b = ddllst[1].SelectedValue;
//string c =
ddllst[2].SelectedValue;
//string d = ddlq.SelectedValue;
//txtq.Text = a + "-" + b +"-"+ c
+"-"+d;
for (int i = 0; i < 3; i++)
{
//string a =
((DropDownList)tbl.FindControl(ddllst[i].ID)).SelectedValue;
string b = hflst[i].Value
+"-"+hftextlst[i];
txtq.Text += b + "//";
//txtq.Text += a+" "+b+"//";
}
}
}
在页面重新加载时视图状态丢失,导致先前的设置都没有任何用途了。所以采取将dropdownlist当前选中的值通过javascript脚本保存在hidden域中的方式,可以避免设置信息丢失。在页面重新加载时将hidden域中的值取出即可。以下是一个名为testDropdown的web窗体测试代码。
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="testDropdown.aspx.cs"
Inherits="EmployeeInfoReferPurview_files_testDropdown" %>
<!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 language=javascript>
function getvalue(obj,obj1,obj2)
{
obj.value = obj1.value;
obj2.value = obj1.innertext;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h2>取下拉框值</h2>
<div>
<asp:Table ID="tbl"
runat="server">
</asp:Table>
<asp:Button ID="Button5" runat="server"
Text="取得Dropdownlist的下拉值" OnClick="Button5_Click"/><br />
<br />
<asp:TextBox ID="txtq" runat="server"
Width="871px"></asp:TextBox>
<br />
<asp:DropDownList ID="ddlq"
runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox></div>
</form>
</body>
</html>
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;
using System.Drawing;
public partial class
EmployeeInfoReferPurview_files_testDropdown : System.Web.UI.Page
{
DropDownList[] ddllst = new DropDownList[3];
ListItem li1 = new ListItem("q", "0");
ListItem li2 = new ListItem("w", "1");
ListItem li3= new ListItem("e", "2");
TextBox[] txtlst = new TextBox[3];
HiddenField[] hflst = new HiddenField[3];
HiddenField[] hftextlst = new
HiddenField[3];
protected override void OnInit(EventArgs e)
{
//createtable();
}
protected void Page_Load(object sender,
EventArgs e)
{
createtable();
if (!Page.IsPostBack)
{
for (int i = 0; i < 3; i++)
{
txtlst[i].ForeColor = Color.Red;
}
}
}
protected void createtable()
{
for (int i = 0; i < 3; i++)
{
TableRow tr = new TableRow();
tr.ID = "tr"+i;
TableCell tc = new TableCell();
tc.ID = "tc" + i;
DropDownList ddl = new
DropDownList();
ddl.ID = "ddl" + i;
ddl.Items.Add(li1);
ddl.Items.Add(li2);
ddl.Items.Add(li3);
//ddl.Items.FindByValue(li2.Value).Selected = true;
HiddenField hf = new HiddenField();
hf.ID = "hf" + i;
hf.Value = ddl.SelectedValue;
HiddenField hftext = new
HiddenField();
hftext.ID = "hftext" + i;
hftext.Value =
ddl.SelectedItem.Text;
ddl.Attributes.Add("onclick",
"javascript:getvalue(hf" + i + ",ddl" + i + ",hftext" + i + ");");
ddllst[i] = ddl;
hflst[i] = hf;
hftextlst[i] = hftext;
//HiddenField hf = new
HiddenField();
//hf.ID = "hf" + i;
//hf.Value = ddl.SelectedValue;
tc.Controls.Add(ddl);
tc.Controls.Add(hf);
tc.Controls.Add(hftext);
tr.Controls.Add(tc);
tbl.Rows.Add(tr);
TableRow tr1 = new TableRow();
tr1.ID = "tr1"+i;
TableCell tc1 = new TableCell();
tc1.ID = "tc1" + i;
TextBox txt = new TextBox();
txt.ID = "txt" + i;
//txt.ForeColor = Color.Red;
txtlst[i] = txt;
tc1.Controls.Add(txt);
tr1.Controls.Add(tc1);
tbl.Rows.Add(tr1);
}
}
protected void Button5_Click(object sender,
EventArgs e)
{
//int inta = ddllst.Length;
//string
valid=((DropDownList)e.Item.FindControl("ddlValid")).SelectedValue;
//string a = ddllst[0].SelectedValue;
//string b = ddllst[1].SelectedValue;
//string c =
ddllst[2].SelectedValue;
//string d = ddlq.SelectedValue;
//txtq.Text = a + "-" + b +"-"+ c
+"-"+d;
for (int i = 0; i < 3; i++)
{
//string a =
((DropDownList)tbl.FindControl(ddllst[i].ID)).SelectedValue;
string b = hflst[i].Value
+"-"+hftextlst[i];
txtq.Text += b + "//";
//txtq.Text += a+" "+b+"//";
}
}
}
展开全部
Form里面可以将ComboBox的属性设置成DropDown来满足同时支持填入和下拉的要求,页面上好像不行,好像还真没见过哪个页面上下拉框也可以自由输入的,一般都是分成一个下拉框和一个编辑框,旁边加上可选按钮
DropDownList1.Items.Add("hello");
DropDownList1.Items.Add("world");
DropDownList1.Items.Add("everyone");
DropDownList1.Items.Add("hello");
DropDownList1.Items.Add("world");
DropDownList1.Items.Add("everyone");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
winform里的可以
asp.net里自带的dropdownlist控件不能进行输入编辑,只能选择。
第三方控件可以,或者利用隐藏文本
asp.net里自带的dropdownlist控件不能进行输入编辑,只能选择。
第三方控件可以,或者利用隐藏文本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Asp.net吗?
百度搜索一下Asp.net Combobox
很多源代码的
百度搜索一下Asp.net Combobox
很多源代码的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询