dropdownlist

时间:2024-04-05 05:14:39编辑:coo君

asp.net如何实现既要触发dropdownlist改变事件时不刷新整个界面

asp.net实现既要触发dropdownlist改变事件时不刷新整个界面的方法:
1、定义aspx页面:



ASP.net How to use Dropdown list

div
{
margin: 5px;
}






Dropdown list无刷新页面的使用方法

姓别:








<asp:Button ID="btnSubmit" runat="server"
Text="点击获取选择的项" onclick="btnSubmit_Click" />


选择的项目:


选择的下拉列表的值:






2、定义后台cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
//查询填充下拉的值
lblSelectedText.Text = ddlGender.SelectedItem.Text;

//检索选中的值并赋值给指定的文本域
lblSelectedValue.Text = ddlGender.SelectedValue;
}
}


怎么获得DropDownList中选中的值

1、点击“开始”—“Visual Studio 2015”,启动 Visual Studio 2015。2、点击新建项目,新建一个控制台应用程序项目。3、DropDownList包含在System.Collections.Generic名空间中,在使用前请务必确认已引用这个命名空间。4、创建一个Dictionary对象,并声明它的键类型和值类型,如以 key 的类型为 int , value的类型为string来创建一个名为testDic的Dictionary对象。5、使用ADD方法,向testDic对象中添加一系列元素。6、点击Visual Studio 2015工具栏的“启动”,看看效果。

怎样让ASPxDropDownList控件的值发生改变时触发SelectedIndexChanged事件







ICollection CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 9; i++) {
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;
dr[4] = 1.23 * (i+1);

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
RadioButtonList1.DataSource = CreateDataSource();
RadioButtonList1.DataTextField="StringValue";
RadioButtonList1.DataValueField="CurrencyValue";
RadioButtonList1.DataBind();
}
}

void Index_Changed(Object sender, EventArgs e) {

Label1.Text = "You selected " + RadioButtonList1.SelectedItem.Text +
" with a value of $" + RadioButtonList1.SelectedItem.Value +
".";

}







<asp:RadioButtonList id="RadioButtonList1"
OnSelectedIndexChanged="Index_Changed"
AutoPostBack="true"
runat="server"/>









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),并且用户的浏览器要启用脚本撰写。


c# asp.net DropDownList控件怎么用

function checkSelect(vale) { var sel = document.getElementById("month"); var op = document.createElement("option"); if (vale == "" || vale == null) { $("#month").empty(); $("#month").append("请选择月份123" + "4 56" + "78 9 10" + "1112"); } if (vale != "" && vale == "1") { $("#month").empty(); $("#month").append("请选择月份123"); } if (vale != "" && vale == "2") { $("#month").empty(); $("#month").append("请选择月份4 56"); } if (vale != "" && vale == "3") { $("#month").empty(); $("#month").append("请选择月份78 9"); } if (vale != "" && vale == "4") { $("#month").empty(); $("#month").append("请选择月份 101112"); } } 统计时间: 请选择季度 1 2 3 4 请选择月份 1 2 3 4 5 6 7 8 9 10 11 12 是要这种效果吗?

怎么获取DropDownList中选中的值?

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


ASP.NET dropdownlist的二级联动问题(两数据都来自数据库)

呵呵 刚刚做了这个~ 我做的是国家和省份 我就不改代码了哈
给你说的详细一点哈(*^__^*)...
建议你不要直接从方法里写查询数据库的代码!
你一定会获取省和城市的数据吧
比如说 获取到的国家的数据集是cList
获取到省份的是pList
前台:






后台:
在页面加载的时候帮他们绑定值
(我看你的代码少绑定了一个哦)
countryDdl.DataSource = cList;
countryDdl.DataTextField = "name";// 页面显示的数据--对应你的省份的名
countryDdl.DataValueField = "id";// 值--对应你的省号
countryDdl.DataBind();
一定要页面加载的时候 两个都绑定哦
然后是联动的效果 countryDdl_SelectedIndexChanged
后台代码:
protected void countryDdl_SelectedIndexChanged(object sender, EventArgs e)
{
// 现获取缩选择的国家的值
string countryStr = countryDdl.SelectedValue;
// 然后重新绑定一下你城市的值而已...


明白了么
说的够仔细吧
累死了~


C#dropdownlist的二级联动怎么做啊?

有两种方法:第一种是很简单的,就是第一级dropdownlist是个服务器控件,后台重新加载二级dropdownlist内容;
第二种就是js无刷新联动:代码发给你,自己看吧,半小时应该能看懂……
无刷新2级菜单联动




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

<!--
function test(arg)
{
if(arg!="button")
{
var thisArray=arg.split(',');
document.getElementById("DropDownList2").length=0;
for(var i = 0; i<thisArray.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;
}
}
//-->





















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
}


判断一个值是否存在于DropDownList的下拉列表中

DropDownList有两个值,一个是Value属性的值,另一个是Text属性的值。根据你的需要可通过如下的方式获取到数据值,然后再根据获取到的值判断就可以了。
string sText=DropDownList1.SelectedItem.Text;//这是获取选中的文本值
string sValue=DropDownList1.SelectedValue//获取DropDownList中设定的Value值.


C#中Dropdownlist如何读取数据库绑定并且联动

我告诉你思路,你在页面加载的时候,也就是page_load方法里先给你的专业下拉列表赋值,直接数据库select * form 专业表或其他表,然后在专业Dropdownlist的选定项索引改变事件里,去给主项单元下拉列表绑定数据,查询数据库条件是select 主项单元 form 专业表 where 专业 = '专业Dropdownlist的当前选定值',后面的依次类推。
原理就是在上一个Dropdownlist索引改变事件里给下一级的Dropdownlist绑定值,条件就是上一个Dropdownlist里面的轩定项


C# dropdownlist 二级联动

有两种方法:第一种是很简单的,就是第一级dropdownlist是个服务器控件,后台重新加载二级dropdownlist内容;
第二种就是js无刷新联动:代码发给你,自己看吧,半小时应该能看懂……
无刷新2级菜单联动




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

<!--
function test(arg)
{
if(arg!="button")
{
var thisArray=arg.split(',');
document.getElementById("DropDownList2").length=0;
for(var i = 0; i<thisArray.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;
}
}
//-->




















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
}


DropDownList控件的使用

//获取选中的文本值 (实际显示的内容)
string str = DropDownList1.SelectedItem.Text;
//获取选中的Value值 (隐藏对应的值)
string str = DropDownList1.SelectedValue;
//获取当前先中的Index
int i = DropDownList1.SelectedItem.ItemIndex;

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

Visual Basic 复制代码





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







DropDownList Example

Select a background color for days in the calendar.



<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>









Background color:









<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server">

White
Silver
Dark Gray
Khaki
Dark Khaki














C# 复制代码





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);

}







DropDownList Example

Select a background color for days in the calendar.



<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>









Background color:









<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server">

White
Silver
Dark Gray
Khaki
Dark Khaki















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

Visual Basic 复制代码






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







DropDownList Data Binding Example

Select a background color for days in the calendar.



<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>









Background color:









<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server"/>












C# 复制代码






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;

}







DropDownList Data Binding Example

Select a background color for days in the calendar.



<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>









Background color:









<asp:DropDownList id="ColorList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server"/>








关于在asp.net中使用dropdownlist控件的问题

if (!IsPostBack)
{
this.DropDownList1.Items.Add("请选择");
string s=@"datasource=.\sqlexpress;database=WireLess;uid=sa;pwd=sa";
SqlConnection conn = new SqlConnection(s);
conn.Open();
string sql = "select menuName from menuInfo ";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.DropDownList1.Items.Add(dr[0].ToString());
}
dr.Close();
conn.Close();
}
这样就可以实现,如果还有什么问题可以随时找我。
如果你是用存储过程写的话,看下面的代码:

if (!IsPostBack)
{
this.DropDownList1.Items.Add("请选择");
string s =@"data source=.\sqlexpress;database=WireLess;uid=sa;pwd=sa";
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = new SqlCommand("存储过程的名称",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.DropDownList1.Items.Add(dr[0].ToString());
}
dr.Close();
conn.Close();
}


上一篇:杨广庭

下一篇:xinyuan