61阅读

dropdownlist-DropdownList

发布时间:2018-03-15 所属栏目:计算机软件及应用

一 : DropdownList

·在其后面加一项:DropdownList.Items.Add("text")或

DropdownList.Items.Add(newListItem("text","value"))-----因为没指定index,所以会加到最后一项。

·在指定的位置添加一项:DropdownList.Items.Insert(index,newListItem("text","value"))-------此句式带Value值的
DropdownList.Items.Insert(index,"text")--------次句式不带Value值,而是根据Index加入到具体的位置

·移除指定项:DropdownList.Items.Remove(newListItem("Text","Value"))--------根据text进行移除
DropdownList.Items.Remove(dpname.Item.FindByText(""));

DropdownList.Items.Remove(dpname.Item.FindByValue(""));

DropdownList.Items.RemoveAt("index")--------根据index进行移除

·移除所有项:DropdownList.Items.Clear()

·项的总数:DropdownList.Items.Count()

·想找具体的某一项:DropdownList.Items.FindByValue(value)或

DropdownList.Items.FindByText(text)

常见问题:
·指定的参数已超出有效值的范围。参数名
有时候将dropdownlist 的 selectedvalue 属性直接赋值是可以的,但有时候会出现“指定的参数已超出有效值的范围。参数名”这种错误提示,这时应将dropdownlist 的 selectedItem.value赋值即可。

·类型"DBNull"到类型"String"的强制转换无效!
情景:
创建一个Label1,从数据库中取出省份信息,放入DataTable中,那么,我们将取出的ProvinceName(数据库中为Null)赋给Label1,则此时会提示错误
所以我们就需要判断一下我们取出的数据是否是null,利用如下:
If Not DataTable.Rows(0).("字段名").GetType IsSystem.DBNull.Value.GetType Then

VB.net 的IIF(,,)函数
即Label1.text=IIF(DataTable.Rows(0).("字段名").GetType IsSystem.DBNull.Value.GetType ,"",DataTable.Rows(0)("字段名"))

二 : DropDownList详解

DropDownList详解下面向大家分享我在网站设计中用到dropdownlist的一些经验和网上看到的个人觉得对我有所指点的代码,上传的附件为都自己所写,比较简单,但比较全,包括数据库sqldatasource与dropdownlist,gridview的应用,DropDownList 详解DropDownList 控件用于创建下拉列表。[www.61k.com)DropDownList 控件中的每个可选项都是由 ListItem 元素定义的!提示:该控件支持数据绑定!DropDownList 控件是一个下拉式的选单,功能和 RadioButtonList Web控件很类似,提供用户在一群选项中选择一个;不过RadioButtonList Web控件适合使用在较少量的选项群组项目,而DropDownList Web 控件则适合用来管理大量的选项群组项目。一、DropDownList 控件属性1、AutoPostBack属性:用于设置当改变选项内容时,,是否自动回送到服务器。True表示回送;False(默认)表示不回送。2、DataSource属性:用于指定填充列表控件的数据源。3、DataTextField属性:用于指定DataSource中的一个字段,该字段的值对应于列表项的Text属性。4、 DataValueField属性:用于指定DataSource中的一个字段,该字段的值对应于列表项的Value属性。5、Items属性:表示列表中各个选项的集合,如DropDownList.Items(i)表示第i个选项,i从0开始。每个选项都有以下3个基本属性:Text 属性:表示每个选项的文本。Value属性:表示每个选项的选项值。Selected属性:表示该选项是否被选中。Count属性:通过Items.Count属性可获得DropDownList控件的选项数;Add方法:通过items.Add方法可以向 DropDownList控件添加选项;Remove方法:通过items.Remove方法,可从DropDownList控件中删除指定的选项;Insert方法:通过items.insert方法,可将一个新的选项插入到DropDownList控件中;Clear方法:通过items.clear方法可以清空DropDownList控件中的选项。6、SelectedIndex属性:用于获取下拉列表中选项的索引值。如果未选定任何项,则返回值-1(负1)。7、SelectedItem属性:用于获取列表中的选定项。通过该属性可获得选定项的Text 和Value属性值。8、SelectedValue属性:用于获取下拉列表中选定项的值。9、 SelectedIndexchanged事件:当用户选择了下拉列表中的任意选项时,都将引发SelectedIndexChanged事件。二、使用语法代码<ASP:DropDownListId="控件名称"Runat="Server"AutoPostBack="True | False"DataSource="<%数据源%>"DataTextField="数据源的字段"DataValueField="数据源的字段"OnSelectedIndexChanged="事件程序名称"><ASP:ListItem/></ASP:DropDownList>一次性在同一个table绑定多个DropDownlist,并且去掉重复项(即代替distinct),从而提高性能。 收藏代码private void Bind

dropdownlist属性 DropDownList详解

DropDownList(DropDownList ddl2, DropDownList ddl3, DropDownList ddl4, DropDownList ddl5){string conn = cs.GetDsn();SqlConnection cn = new SqlConnection(conn);string strSqlDDL = "select Country,CustName, Substring(CONVERT(varchar(100), ActInstDate, 101),1,2) ActInstDate,ResellerName from tbcalicoinfo where surveystatus='Completed'";DataSet ds = new DataSet();SqlDataAdapter da = new SqlDataAdapter(strSqlDDL, cn);cn.Open();da.Fill(ds, "tbcalicoinfo");DataTable dt = new DataTable(); ds.Tables["tbcalicoinfo"].DefaultView.RowFilter = "country is not null";//RowFilter 过滤ds.Tables["tbcalicoinfo"].DefaultView.Sort = "country asc"; //Sort 排序ddl2.DataSource = ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true, "country");// ToTable去重复ddl2.DataTextField = "country";ddl2.DataBind();ddl2.Items.Insert(0, new ListItem("--All--", "All"));ds.Tables["tbcalicoinfo"].DefaultView.RowFilter = "ActInstDate is not null";ds.Tables["tbcalicoinfo"].DefaultView.Sort = "ActInstDate asc";ddl3.DataSource = ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true, "ActInstDate");;ddl3.DataTextField = "ActInstDate";ddl3.DataBind();ddl3.Items.Insert(0, new ListItem("--All--", "All"));//DataRow[] dr2 = dt.Select("ResellerName is not null","ResellerName asc");ds.Tables["tbcalicoinfo"].DefaultView.RowFilter = "ResellerName is not null";ds.Tables["tbcalicoinfo"].DefaultView.Sort = "ResellerName asc";ddl4.DataSource = ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true, "ResellerName");ddl4.DataTextField = "ResellerName";ddl4.DataBind();ddl4.Items.Insert(0, new ListItem("--All--", "All"));ds.Tables["tbcalicoinfo"].DefaultView.RowFilter = "CustName is not null";ds.Tables["tbcalicoinfo"].DefaultView.Sort = "CustName asc";ddl5.DataSource = ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true, "CustName");ddl5.DataTextField = "CustName";ddl5.DataBind();ddl5.Items.Insert(0, new ListItem("--All--", "All"));cn.Close();}实现DropDownList无刷新二级联动一、数据库设计:字段名 数据类型 说明ClassID 自动编号 类编号ClassName varchar(8) 类名UpClassID int(4) 上级类编号ClassLevel int(4) 类级别,1为大类,2为小类二涉及代码1、首先,我们新建一个页面DropTest.aspx,在其中放入两个DropDownList控件:DropDownList1和DropDownList2,其完整代码如下:页面设计代码代码<%@ Page language="c#" Codebehind="DropTest.aspx.cs" AutoEventWireup="false" Inherits="studyWEB.DropTest" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML><HEAD><title>WebForm2</title><meta content="Microsoft Visual Studio .NET 7.1" n

dropdownlist属性 DropDownList详解

ame="GENERATOR"><meta content="C#" name="CODE_LANGUAGE"><meta content="JavaScript" name="vs_defaultClientScript"><meta content="http://schemas.microsoft.com/intellisense/ie5"; name="vs_targetSchema"><script>function load(ClassID){ //ClassID为接收传递的大类编号var drp2 = document.getElementById("DropDownList2");function RemoveAll(oElem) { //清除DropDownList2的所有项var i = 0;for (i = oElem.length; i >= 0; i--){oElem.options.remove(i);}}RemoveAll(drp2)var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");var oDoc = new ActiveXObject("MSXML2.DOMDocument");oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false); //调用读取小类数据的页面,将大类// 编号值传递过去oHttpReq.send("");result = oHttpReq.responseText;oDoc.loadXML(result);items1 = oDoc.selectNodes("//CLASSNAME/Table/ClassName"); //读取所有请求大类所属小类的类名items2 = oDoc.selectNodes("//CLASSNAME/Table/ClassID"); //读取所有请求大类所属小类的编号var itemsLength=items1.length;for(i=0;i<itemsLength;i++) //将小类的类名和编号赋予DropDownList2{var newOption = document.createElement("OPTION");newOption.text=items1[i].text;newOption.value=items2[i].text;drp2.options.add(newOption);}}</script></HEAD><body MS_POSITIONING="flowLayout"><form id="Form1" method="post" runat="server"><asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList><asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList><asp:TextBox id="TH" runat="server" BorderStyle="None" ForeColor="White" BorderColor="White"></asp:TextBox><asp:Label id="Label1" runat="server"></asp:Label><asp:Button id="Button1" runat="server" Text="Button"></asp:Button></form></body></HTML>该页面的后台文件(DropDownList1.aspx.cs)中 Page_Load内的代码如下:代码pageload代码if(!this.IsPostBack){SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where ClassLevel=1",con);DataSet ds = new DataSet();da.Fill(ds);this.DropDownList1.DataSource=ds.Tables[0].DefaultView;this.DropDownList1.DataTextField = "ClassName";this.DropDownList1.DataValueField = "ClassID";this.DropDownList1.DataBind();this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)"); //将ClassID作为参数传递给脚本函数load(ClassID),如果要传递的是ClassName,应将value改为innerText,但如果大类为中文,则调用小类时出现无法显示的问题// this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;"); //读取DropDownList2的值,将其赋给一个TextBox控件TH,以获取DropDownList2的值,此页面实现如下功能:首先从数据库内读取所有类级别为1(即大类)的类名和类编

扩展:dropdownlist / html.dropdownlist / jquery dropdownlist

dropdownlist属性 DropDownList详解

号,绑定到DropDownList1控件上;然后通过 DropDownList1的Attributes属性调用javascript函数load(ClassID);load()函数通过调用 DropChild.aspx页面,读取XML流,得到大类所属小类的ClassName和ClassID。(www.61k.com)2、新建 DropChild.aspx页面文件,其中不插入任何控件和文本,只在其后台文件(DropChild.aspx.cs)中的Page_Load中加入以下代码:代码if(this.Request["ClassID"]!=null){int state = Convert.ToInt32(this.Request["ClassID"]);SqlConnection con = new SqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassID from classname where UpClassID='"+state+"'",con);DataSet ds = new DataSet("CLASSNAME");da.Fill(ds);XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);writer.Formatting = Formatting.Indented;writer.Indentation = 4;writer.IndentChar = ' ';ds.WriteXml(writer);writer.Flush();Response.End();writer.Close();该方法得到用户选择的大类的编号,通过查询以后得到一个DataSet对象,使用该对象的WriteXML方法直接将内容写到 Response.OutputStream里面然后传递到客户端,客户端的load方法通过result =oHttpReq.responseText;句话得到一个XML字符串,最后解析此串。另外,测试获取DropDownList2值,添加了TextBox控件TH,当点击Button时,处理事件代码如下:代码private void Button1_Click(object sender, System.EventArgs e){Label1.Text=TH.Text

扩展:dropdownlist / html.dropdownlist / jquery dropdownlist

三 : DropDownList控件

DropDownList控件是一个相对比较简单的数据绑定控件,它在客户端被解释成<select></select>这样的HTML标记,也就是只能有一个选项处于选中状态。(www.61k.com]DropDownList控件常见属性:AutoPostBack属性:这个属性的用法在讲述基本控件的时候已经讲过,是用来设置当下拉列表项发生变化时是否主动向服务器提交整个表单,默认是false,即不主动提交。如果设置为true,就可以编写它的SelectedIndexChanged事件处理代码进行相关处理(注意:如果此属性为false即使编写了SelectedIndexChanged事件处理代码也不会马上起作用)。DataTextField属性:设置列表项的可见部分的文字。DataValueField属性:设置列表项的值部分。Items属性:获取控件的列表项的集合。SelectedIndex属性:获取或设置 DropDownList 控件中的选定项的索引。SelectedItem属性:获取列表控件中索引最小的选定项。SelectedValue属性:取列表控件中选定项的值,或选择列表控件中包含指定值的项。另外,DropDownList控件默认情况下是第一个选项处于选中状态,如果我们想在绑定数据之后让某个选项处于选中状态,可以利用它的Items属性,DropDownList控件的Items属性其实是一个ListItemCollection的实例,ListItemCollection类有两个重要方法:public ListItem FindByText (string text):在选项集合中查找指定文字的选项。public ListItem FindByValue (string value) :在选项集合中查找指定值的选项。

扩展:元素缺少必需的特性alt / vs2010注释多行 / sql序号自动增加

本文标题:dropdownlist-DropdownList
本文地址: http://www.61k.com/1090821.html

61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1