简体   繁体   English

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

[英]MSDN DropDownList example does always return the first value

I really need some help because I can't figure it out myself. 我真的需要一些帮助,因为我自己也无法解决。 Despite I have followed many tutorial my dropdownlist is always returning the first value. 尽管我遵循了许多教程,但我的下拉列表始终返回第一个值。 So today I've decided to use a code that it should work since it's from msdn.microsoft web site. 因此,今天我决定使用应该工作的代码,因为它来自msdn.microsoft网站。

I've tried into my index.aspx both c# codes but neither works, it's always return to 'white' (first value). 我已经在两个c#代码中都尝试了index.aspx,但是都没有用,它总是返回“ white”(第一个值)。 Can anyone please enlighten me. 任何人都可以启发我。

(ps: I've to use .NET 3.5 mvc2 and I'm using vs 2008) (ps:我必须使用.NET 3.5 mvc2,而我使用的是vs 2008)

Thanks. 谢谢。

edit: my index.aspx 编辑:我的index.aspx

edit2: Well, the inexperience made ​​me assume the wrong notion, I was working onto mvc.ViewPage instead of UI.Page. edit2:好吧,缺乏经验让我假设了错误的观念,我正在研究mvc.ViewPage而不是UI.Page。 Now it's all working. 现在一切正常。 Thanks again. 再次感谢。

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

The most common error with populating a DropDownList is that the DropDownList is bound to data in the page load. 填充DropDownList时最常见的错误是DropDownList绑定到页面加载中的数据。 Please make sure that the call to populate it is inside a condition for If (!IsPostback) 请确保填充它的调用在If (!IsPostback)的条件内

This will allow it to load the first time but not on postback when you want it to retain the selected choice. 如果您希望保留选定的选项,它将允许它第一次加载,但不能回发。 If you don't prevent the binding on postback the data is repopulated in the select element and the selected value is lost. 如果不阻止回发绑定,则会在select元素中重新填充数据,并且所选值将丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM