MSDN DropDownList示例始终返回第一个值

时间:2013-12-13 13:33:07

标签: c# asp.net

我真的需要一些帮助,因为我无法弄清楚自己。尽管我已经遵循了很多教程,但我的下拉列表总是返回第一个值。所以今天我决定使用它应该有效的代码,因为它来自msdn.microsoft网站。

我已经尝试了我的index.aspx两个c#代码,但都不起作用,它总是返回'white'(第一个值)。 任何人都可以请赐教。

(ps:我要使用.NET 3.5 mvc2而我正在使用vs 2008)

感谢。

编辑:我的index.aspx

edit2:嗯,缺乏经验让我假设错误的概念,我正在研究mvc.ViewPage而不是UI.Page。现在一切正常。再次感谢。

<%@ 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>

1 个答案:

答案 0 :(得分:3)

填充DropDownList最常见的错误是DropDownList绑定到页面加载中的数据。请确保填充它的调用符合If (!IsPostback)

的条件

当您希望它保留所选择的选项时,这将允许它第一次加载而不是回发。如果不在回发时阻止绑定,则会在select元素中重新填充数据,并且选定的值将丢失。

相关问题