海外主机测评

您现在的位置是:首页 > 数据库 > 正文

数据库

dropdownlistdropdownlist设置默认值

cds8202023-04-24数据库87
本文目录一览:1、C#dropdownlist二级联动2、ASP.NET中的DropDownList控件的使用??3、关于,dropdownlist在C#中的联动4、Dro

本文目录一览:

  • 1、C# dropdownlist 二级联动
  • 2、ASP.NET中的DropDownList控件的使用??
  • 3、关于,dropdownlist在C#中的联动
  • 4、DropDownList控件的使用
  • 5、怎么获取DropDownList中选中的值?
  • 6、如何实现DropDownList下拉多选功能

有两种方法:第一种是很简单的,就是第一级dropdownlist是个服务器控件,后台重新加载二级dropdownlist内容;

第二种就是js无刷新联动:代码发给你,自己看吧,半小时应该能看懂……

无刷新2级菜单联动

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 TransitiONal//EN" ""

%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %

html xmlns=""

head runat="server"

title.net2.0回调实现二级无刷新联动/title

script type="text/javascript"

!--

function test(arg)

{

if(arg!="button")

{

var thisArray=arg.split(',');

document.getElementById("DropDownList2").length=0;

for(var i = 0; ithisArray.length ;i++)

{

document.getElementById("DropDownList2").options.add(new Option(thisArray[i].toString(),thisArray[i].toString()));

}

}

else

{

document.getElementById("text").innerHTML = "你选择的大类是:"+document.getElementById("DropDownList1").value+" 小类是:"+document.getElementById("DropDownList2").value;

}

}

//--

/script

style

/style

/head

body

form id="form1" runat="server"

asp:DropDownList ID="DropDownList1" runat="server"

asp:ListItem Text='请选择'/asp:ListItem

asp:ListItem Value=1 Text=1/asp:ListItem

asp:ListItem Value=2 Text=2/asp:ListItem

/asp:DropDownList

asp:DropDownList ID="DropDownList2" runat="server"

asp:ListItem Text='请选择'/asp:ListItem

/asp:DropDownList

input type=button onclick="callserver('3')" value="提交" /

span id="text"/span

/form

/body

/html

using System;

using System.Data;

using System.Configuration;

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.IO;

using System.Text;

public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler

{

protected void Page_Load(object sender, EventArgs e)

{

string str = Page.ClientScript.GetCallbackEventReference(this,"arg","test",null,false);///获取一个对客户端函数的引用

string script = "function callserver(arg){"+str+"}";///组织一个callserver函数

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", script, true);///注册客户端脚本

DropDownList1.Attributes.Add("onchange", "callserver(document.getElementById('DropDownList1').value)");///为DropDownList1添加客户端的onchange事件,触发callserver函数

}

#region ICallbackEventHandler 成员

string selecttext = null;

public string GetCallbackResult()

{

StringBuilder sb = new StringBuilder();

if (selecttext == "1")

{

sb.Append("1-1,1-2,1-3");

}

else if (selecttext == "2")

{

sb.Append("2-1,2-2,2-3");

}

else

{

sb.Append("button");

}

return sb.ToString();///给客户端函数test返回一个参数sb

}

public void RaiseCallbackEvent(string eventArgument)

{

selecttext = eventArgument;///获得客户端提交的参数赋给变量selecttext

}

#endregion

}

ASP.NET中的DropDownList控件的使用??

使用

DropDownList

Web

服务器控件,用户可以从单项选择下拉列表框中进行选择。DropDownList

控件与

ListBox

Web

服务器控件类似。不同之处在于它只在框中显示选定项,同时还显示下拉按钮。当用户单击此按钮时,将显示项的列表。

修改

DropDownList

控件的外观

可以通过以像素为单位设置

DropDownList

控件的高度和宽度来控制其外观。部分浏览器不支持以像素为单位设置高度和宽度,这些浏览器将使用行计数设置。

您无法指定用户单击下拉按钮时列表中显示的项数。所显示列表的长度由浏览器确定。

与其他

Web

服务器控件一样,您可以使用样式对象来指定

DropDownList

控件的外观。有关详细信息,请参见

ASP.NET

Server

Controls

and

CSS

Styles。

列表项

DropDownList

控件实际上是列表项的容器,这些列表项都属于

ListItem

类型。每一

ListItem

对象都是带有自己的属性的单独对象。下表说明了这些属性。

属性

说明

Text

指定在列表中显示的文本。

Value

包含与某个项相关联的值。设置此属性可使您将该值与特定的项关联而不显示该值。例如,您可以将

Text

属性设置为美国某个州的名称,而将

Value

属性设置为该州的邮政区名缩写。

Selected

通过一个布尔值指示是否选择了该项。

若要以编程方式处理列表项,请使用

DropDownList

控件的

Items

集合。Items

集合是一个标准集合,您可以向它添加项对象,也可以从中删除项或清除集合等。

当前所选项可在

DropDownList

控件的

SelectedItem

属性中得到。

将数据绑定到控件

可以使用

DropDownList

Web

服务器控件列出使用数据源控件的页可使用的选项。DropDownList

控件中的每一项分别对应数据源中的一项(通常是一行)。

控件显示来自数据源的一个字段。您也可以将控件绑定到第二个字段,以设置一项的值(该值并不显示)。

与其他

Web

服务器控件一样,您可以将任何控件属性(如控件的颜色或大小)绑定到数据。有关详细信息,请参见如何:从数据源填充

List

Web

服务器控件。

DropDownList

事件

当用户选择一项时,DropDownList

控件将引发一个事件(SelectedIndexChanged

事件)。默认情况下,此事件不会导致将页发送到服务器,但可以通过将

AutoPostBack

属性设置为

true

使此控件强制立即发送。

注意

若要在选中

DropDownList

控件时将其发送到服务器,浏览器必须支持

ECMAScript(JScript、JavaScript),并且用户的浏览器要启用脚本撰写。

关于,dropdownlist在C#中的联动

你可以在一个控件发生事件时将它的值赋给一个不可见的TEXTBOX控件,然后在初始化的时候叛断这个TEXTBOX里面有没有值,如果有的话就取它的值,没有的话就表示初始值,依此类推。 private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

if(!IsPostBack)

{

SubSysId = (int)SubSysEnum.SystemManagement;

ModuleId = (int)SmModuleEnum.LogManagement;

PermissionID = SYSTEMLOG;

BTNDelete.Enabled = false;

InitUserList();

Bind();

DDLSys_SelectedIndexChanged(null,null);

DDLMou_SelectedIndexChanged(null,null);

}

}

#region Web 窗体设计器生成的代码

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// summary

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// /summary

private void InitializeComponent()

{

this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);

this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);

this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

this.DDLSys.SelectedIndexChanged += new System.EventHandler(this.DDLSys_SelectedIndexChanged);

this.DDLMou.SelectedIndexChanged += new System.EventHandler(this.DDLMou_SelectedIndexChanged);

this.BtQuery.Click += new System.EventHandler(this.BtQuery_Click);

this.BTNDelete.Click += new System.EventHandler(this.BTNDelete_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void InitUserList()

{

NameValueCollection userList = new AccountSystem(UserOperation).GetAllUserList();

ddlUser.Items.Clear();

ddlUser.Items.Add(new ListItem("所有", ""));

ddlUser.Items.Add(new ListItem("超级管理员", "Admin"));

foreach(string userId in userList.Keys)

ddlUser.Items.Add(new ListItem(userList[userId], userId));

ddlUser.SelectedIndex = 0;

}

private void Bind()

{

//绑定子系统列表

StringCollection textList = new StringCollection(),

valueList = new StringCollection();

LogConst.GetSubSysEnum(textList, valueList);

DDLSys.Items.Clear();

for(int I = 0; I textList.Count; I++)

DDLSys.Items.Add(new ListItem(textList[I], valueList[I]));

DDLSys.SelectedIndex = 0;

//绑定操作类型列表

textList = new StringCollection();

valueList = new StringCollection();

LogConst.GetOperationEnum(textList,valueList);

DDLOprSort.Items.Clear();

for(int I = 0; I textList.Count; I++)

DDLOprSort.Items.Add(new ListItem(textList[I], valueList[I]));

DDLOprSort.SelectedIndex = 0;

}

private void DDLSys_SelectedIndexChanged(object sender, System.EventArgs e)

{

if (this.DDLSys.SelectedValue.Trim()!="-1")

{

StringCollection textList = new StringCollection(),

valueList = new StringCollection();

LogConst.GetModuleEnum((SubSysEnum)Convert.ToInt32(this.DDLSys.SelectedValue.Trim()),textList, valueList);

this.DDLMou.Items.Clear();

for(int I = 0; I textList.Count; I++)

DDLMou.Items.Add(new ListItem(textList[I], valueList[I]));

DDLMou.SelectedIndex = 0;

}

else

{

this.DDLMou.Items.Clear();

DDLMou.Items.Add(new ListItem("所有", "-1"));

DDLMou.SelectedIndex = 0;

}

}

private void DDLMou_SelectedIndexChanged(object sender, System.EventArgs e)

{

if(this.DDLMou.SelectedValue.Trim()=="-1")

{

this.DDLOpr.Items.Clear();

DDLOpr.Items.Add(new ListItem("所有", "-1"));

DDLOpr.SelectedIndex = 0;

}

else

{

//string qq=this.DDLSys.SelectedValue.Trim()+this.DDLMou.SelectedValue.Trim();

this.DDLOpr.Items.Clear();

StringCollection textList = new StringCollection(),

valueList = new StringCollection();

LogConst.GetSysModuleObjects((SmModuleEnum)Convert.ToInt32(this.DDLMou.SelectedValue.Trim()),textList, valueList);

this.DDLOpr.Items.Clear();

for(int I = 0; I textList.Count; I++)

DDLOpr.Items.Add(new ListItem(textList[I], valueList[I]));

DDLOpr.SelectedIndex = 0;

}

}

//获取选中的文本值 (实际显示的内容)

string str = DropDownList1.SelectedItem.Text;

//获取选中的Value值 (隐藏对应的值)

string str = DropDownList1.SelectedValue;

//获取当前先中的Index

int i = DropDownList1.SelectedItem.ItemIndex;

示例

下面的代码示例演示如何创建包含四个项的 DropDownList 控件。

Visual Basic 复制代码

%@ Page Language="VB" AutoEventWireup="True" %

html

script runat="server"

Sub Selection_Change(sender As Object, e As EventArgs)

' Set the background color for days in the Calendar control

' based on the value selected by the user from the

' DropDownList control.

Calendar1.DayStyle.BackColor = _

System.Drawing.Color.FromName(ColorList.SelectedItem.Value)

End Sub

/script

body

form runat="server"

h3 DropDownList Example /h3

Select a background color for days in the calendar.

brbr

asp:Calendar id="Calendar1"

ShowGridLines="True"

ShowTitle="True"

runat="server"/

brbr

table cellpadding="5"

tr

td

Background color:

/td

/tr

tr

td

asp:DropDownList id="ColorList"

AutoPostBack="True"

OnSelectedIndexChanged="Selection_Change"

runat="server"

asp:ListItem Selected="True" Value="White" White /asp:ListItem

asp:ListItem Value="Silver" Silver /asp:ListItem

asp:ListItem Value="DarkGray" Dark Gray /asp:ListItem

asp:ListItem Value="Khaki" Khaki /asp:ListItem

asp:ListItem Value="DarkKhaki" Dark Khaki /asp:ListItem

/asp:DropDownList

/td

/tr

/form

/body

/html

C# 复制代码

%@ Page Language="C#" AutoEventWireup="True" %

html

script runat="server"

void Selection_Change(Object sender, EventArgs e)

{

// Set the background color for days in the Calendar control

// based on the value selected by the user from the

// DropDownList control.

Calendar1.DayStyle.BackColor =

System.Drawing.Color.FromName(ColorList.SelectedItem.Value);

}

/script

body

form runat="server"

h3 DropDownList Example /h3

Select a background color for days in the calendar.

brbr

asp:Calendar id="Calendar1"

ShowGridLines="True"

ShowTitle="True"

runat="server"/

brbr

table cellpadding="5"

tr

td

Background color:

/td

/tr

tr

td

asp:DropDownList id="ColorList"

AutoPostBack="True"

OnSelectedIndexChanged="Selection_Change"

runat="server"

asp:ListItem Selected="True" Value="White" White /asp:ListItem

asp:ListItem Value="Silver" Silver /asp:ListItem

asp:ListItem Value="DarkGray" Dark Gray /asp:ListItem

asp:ListItem Value="Khaki" Khaki /asp:ListItem

asp:ListItem Value="DarkKhaki" Dark Khaki /asp:ListItem

/asp:DropDownList

/td

/tr

/form

/body

/html

下面的代码示例演示如何通过数据绑定创建 DropDownList 控件。

Visual Basic 复制代码

%@ Page Language="VB" AutoEventWireup="True" %

%@ Import Namespace="System.Data" %

html

script runat="server"

Sub Selection_Change(sender as Object, e As EventArgs)

' Set the background color for days in the Calendar control

' based on the value selected by the user from the

' DropDownList control.

Calendar1.DayStyle.BackColor = _

System.Drawing.Color.FromName(ColorList.SelectedItem.Value)

End Sub

Sub Page_Load(sender as Object, e As EventArgs)

' Load data for the DropDownList control only once, when the

' page is first loaded.

If Not IsPostBack Then

' Specify the data source and field names for the Text

' and Value properties of the items (ListItem objects)

' in the DropDownList control.

ColorList.DataSource = CreateDataSource()

ColorList.DataTextField = "ColorTextField"

ColorList.DataValueField = "ColorValueField"

' Bind the data to the control.

ColorList.DataBind()

' Set the default selected item, if desired.

ColorList.SelectedIndex = 0

End If

End Sub

Function CreateDataSource() As ICollection

' Create a table to store data for the DropDownList control.

Dim dt As DataTable = New DataTable()

' Define the columns of the table.

dt.Columns.Add(new DataColumn("ColorTextField", GetType(String)))

dt.Columns.Add(new DataColumn("ColorValueField", GetType(String)))

' Populate the table with sample values.

dt.Rows.Add(CreateRow("White", "White", dt))

dt.Rows.Add(CreateRow("Silver", "Silver", dt))

dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt))

dt.Rows.Add(CreateRow("Khaki", "Khaki", dt))

dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt))

' Create a DataView from the DataTable to act as the data source

' for the DropDownList control.

Dim dv As DataView = New DataView(dt)

Return dv

End Function

Function CreateRow(Text As String, Value As String, dt As DataTable) As DataRow

' Create a DataRow using the DataTable defined in the

' CreateDataSource method.

Dim dr As DataRow = dt.NewRow()

' This DataRow contains the ColorTextField and ColorValueField

' fields, as defined in the CreateDataSource method. Set the

' fields with the appropriate value. Remember that column 0

' is defined as ColorTextField, and column 1 is defined as

' ColorValueField.

dr(0) = Text

dr(1) = Value

Return dr

End Function

/script

body

form runat="server"

h3 DropDownList Data Binding Example /h3

Select a background color for days in the calendar.

brbr

asp:Calendar id="Calendar1"

ShowGridLines="True"

ShowTitle="True"

runat="server"/

brbr

table cellpadding="5"

tr

td

Background color:

/td

/tr

tr

td

asp:DropDownList id="ColorList"

AutoPostBack="True"

OnSelectedIndexChanged="Selection_Change"

runat="server"/

/td

/tr

/form

/body

/html

C# 复制代码

%@ Page Language="C#" AutoEventWireup="True" %

%@ Import Namespace="System.Data" %

html

script runat="server"

void Selection_Change(Object sender, EventArgs e)

{

// Set the background color for days in the Calendar control

// based on the value selected by the user from the

// DropDownList control.

Calendar1.DayStyle.BackColor =

System.Drawing.Color.FromName(ColorList.SelectedItem.Value);

}

void Page_Load(Object sender, EventArgs e)

{

// Load data for the DropDownList control only once, when the

// page is first loaded.

if(!IsPostBack)

{

// Specify the data source and field names for the Text

// and Value properties of the items (ListItem objects)

// in the DropDownList control.

ColorList.DataSource = CreateDataSource();

ColorList.DataTextField = "ColorTextField";

ColorList.DataValueField = "ColorValueField";

// Bind the data to the control.

ColorList.DataBind();

// Set the default selected item, if desired.

ColorList.SelectedIndex = 0;

}

}

ICollection CreateDataSource()

{

// Create a table to store data for the DropDownList control.

DataTable dt = new DataTable();

// Define the columns of the table.

dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));

dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));

// Populate the table with sample values.

dt.Rows.Add(CreateRow("White", "White", dt));

dt.Rows.Add(CreateRow("Silver", "Silver", dt));

dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));

dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));

dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));

// Create a DataView from the DataTable to act as the data source

// for the DropDownList control.

DataView dv = new DataView(dt);

return dv;

}

DataRow CreateRow(String Text, String Value, DataTable dt)

{

// Create a DataRow using the DataTable defined in the

// CreateDataSource method.

DataRow dr = dt.NewRow();

// This DataRow contains the ColorTextField and ColorValueField

// fields, as defined in the CreateDataSource method. Set the

// fields with the appropriate value. Remember that column 0

// is defined as ColorTextField, and column 1 is defined as

// ColorValueField.

dr[0] = Text;

dr[1] = Value;

return dr;

}

/script

body

form runat="server"

h3 DropDownList Data Binding Example /h3

Select a background color for days in the calendar.

brbr

asp:Calendar id="Calendar1"

ShowGridLines="True"

ShowTitle="True"

runat="server"/

brbr

table cellpadding="5"

tr

td

Background color:

/td

/tr

tr

td

asp:DropDownList id="ColorList"

AutoPostBack="True"

OnSelectedIndexChanged="Selection_Change"

runat="server"/

/td

/tr

/form

/body

/html

怎么获取DropDownList中选中的值?

运用 Label1.Text=DropDownList1.SelectedValue.ToString();

或者DropDownList1.SelectedItem.Text即可。

DropDownList控件又称下拉列表框控件, 控件 列表 中的多行数 据 以隐含 的形式表 示 出 来,当用户需要选择所需列表项时,通过点击 "下三角 "图形 展示 ,用户每次 只能选用一个 数据项。DropDownList控件实际上是列表项 的 容器 ,下拉列表框 用 Items集合表示各项 的内 容。如果在 ASP.NET页面中逐个 的手 动填写 DropDownList控件的列表选项,当列表项很多 的时候会 比较繁琐 ,而且修改 比较麻烦 。 DropDownList控件动态连接到数据库,按指定 条件从数据库 中查询 出列表选项数据,然后绑 定到控件,可以方便快速地显示出多个下拉选 项 。 同时 ,通过修 改数据库 中数据 ,可 以动 态改 变下 拉选项。例如 ,在导 师遴 选系统 中, 研究生导师填写申请信息 的时候 ,需要选 择申 请类型,而且同样的信息在多处页面出现,将 数据库中申请类型表的数据绑定到 DropDownList控件上,能比较好的解决问题。

如何实现DropDownList下拉多选功能

dropdownlist前台绑定的时候才用SQLdatasource,如果后台直接写代码绑定数据用不着。在设计页面双击dropdownlist按钮会直接进入到按钮事件里去,在那里写两个绑定内容,判断条件就是你的dropdownlist所选择的项目内容(dropdownlist.SelectedIte

关于dropdownlist和dropdownlist设置默认值的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~