C# MSDN DropDownList示例始终返回第一个值
我真的需要一些帮助,因为我自己搞不懂。尽管我遵循了很多教程,我的dropdownlist总是返回第一个值。所以今天我决定使用一个代码,它应该可以工作,因为它来自web站点 我在index.aspx中尝试了两个c#代码,但都不起作用,它总是返回到“白色”(第一个值)。 谁能给我点化一下吗 (注:我必须使用.NET3.5MVC2,我使用的是VS2008) 谢谢 编辑:my index.aspx edit2:嗯,是因为缺乏经验我假设我的想法是错误的,我使用的是mvc.ViewPage而不是UI.Page。现在一切都好了。再次感谢C# MSDN DropDownList示例始终返回第一个值,c#,asp.net,C#,Asp.net,我真的需要一些帮助,因为我自己搞不懂。尽管我遵循了很多教程,我的dropdownlist总是返回第一个值。所以今天我决定使用一个代码,它应该可以工作,因为它来自web站点 我在index.aspx中尝试了两个c#代码,但都不起作用,它总是返回到“白色”(第一个值)。 谁能给我点化一下吗 (注:我必须使用.NET3.5MVC2,我使用的是VS2008) 谢谢 编辑:my index.aspx edit2:嗯,是因为缺乏经验我假设我的想法是错误的,我使用的是mvc.ViewPage而不是UI.P
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!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" >
<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>
<head runat="server">
<title> DropDownList Data Binding Example </title>
</head>
<body>
<form id="form1" 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>
</table>
</form>
</body>
</html>
无效选择\u更改(对象发送方,事件参数e)
{
//在日历控件中设置天数的背景色
//基于用户从中选择的值
//下拉列表控件。
Calendar1.DayStyle.BackColor=
System.Drawing.Color.FromName(ColorList.SelectedItem.Value);
}
无效页面加载(对象发送方,事件参数e)
{
//当
//页面首先被加载。
如果(!IsPostBack)
{
//指定文本的数据源和字段名
//项目的属性和值(ListItem对象)
//在DropDownList控件中。
ColorList.DataSource=CreateDataSource();
ColorList.DataTextField=“ColorTextField”;
ColorList.DataValueField=“ColorValueField”;
//将数据绑定到控件。
ColorList.DataBind();
//如果需要,设置默认选定项。
ColorList.SelectedIndex=0;
}
}
ICollection CreateDataSource()
{
//创建一个表来存储DropDownList控件的数据。
DataTable dt=新的DataTable();
//定义表的列。
Add(新的数据列(“ColorTextField”,typeof(String));
添加(新的数据列(“ColorValueField”,typeof(String));
//使用示例值填充表格。
添加(CreateRow(“白色”,“白色”,dt));
添加(CreateRow(“银”,“银”,dt));
添加(CreateRow(“深灰色”、“暗灰色”、dt));
添加(CreateRow(“卡其色”,“卡其色”,dt));
添加(CreateRow(“深色卡其色”、“深色卡其色”,dt));
//从DataTable创建一个DataView作为数据源
//对于DropDownList控件。
数据视图dv=新数据视图(dt);
返回dv;
}
DataRow CreateRow(字符串文本、字符串值、DataTable dt)
{
//使用中定义的DataTable创建DataRow
//CreateDataSource方法。
DataRow dr=dt.NewRow();
//此数据行包含ColorTextField和ColorValueField
//字段,如CreateDataSource方法中定义的
//具有适当值的字段。请记住第0列
//定义为ColorTextField,第1列定义为
//ColorValueField。
dr[0]=文本;
dr[1]=数值;
返回dr;
}
DropDownList数据绑定示例
DropDownList数据绑定示例
选择日历中天数的背景色。
背景色:
填充DropDownList最常见的错误是DropDownList绑定到页面加载中的数据。请确保填充它的调用在(!IsPostback)If的条件内
这将允许它第一次加载,但当您希望它保留所选选项时,不会在回发时加载。如果不阻止回发时的绑定,则会在select元素中重新填充数据,并丢失选定的值。填充DropDownList时最常见的错误是DropDownList绑定到页面加载中的数据。请确保填充它的调用在(!IsPostback)If的条件内
这将允许它第一次加载,但当您希望它保留所选选项时,不会在回发时加载。如果不阻止回发时的绑定,则会在select元素中重新填充数据,并丢失选定的值。填充DropDownList时最常见的错误是DropDownList绑定到页面加载中的数据。请确保填充它的调用在(!IsPostback)If的条件内
这将允许它第一次加载,但当您希望它保留所选选项时,不会在回发时加载。如果不阻止回发时的绑定,则会在select元素中重新填充数据,并丢失选定的值。填充DropDownList时最常见的错误是DropDownList绑定到页面加载中的数据。请确保填充它的调用在(!IsPostback)If的条件内
这将允许它第一次加载,但当您希望它保留所选选项时,不会在回发时加载。如果不阻止回发时绑定,数据将重新填充到select元素中,选定的值将丢失。链接中的用户询问:如何在回发后再次获得默认值作为选定值,但我需要相反的结果,我选择的值应该保留在下拉列表中。我删除了确实不正确的链接,并添加了一些额外的详细信息查看代码在我看来,填充下拉列表的调用位于if(!IsPostBack)中,但我设置了一个断点,每次我选择一个值时,它都会在if中的断点处停止(!IsPostBack),我还使用了if(!Page.IsPostBack)。是否在DropDownList上设置了AutoPostBack=true?链接中的用户询问:如何在postba之后再次获得默认值作为选定值