DropDownList控件的使用
用哪个属性能获得DropDownList的值,比如我选择了其中一个值,我用哪个属性让它返回给一个字符串变量(假如一个变量是s)呢?谢谢哦...
用哪个属性能获得DropDownList的值,比如我选择了其中一个值,我用哪个属性让它返回给一个字符串变量(假如一个变量是s)呢?
谢谢哦 展开
谢谢哦 展开
2个回答
展开全部
//获取选中的文本值 (实际显示的内容)
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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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>
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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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.
<br><br>
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br><br>
<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>
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询