나 자신을 이해할 수 없기 때문에 도움이 필요합니다. 많은 자습서를 따랐지만 드롭 다운 목록은 항상 첫 번째 값을 반환합니다. 그래서 오늘은 msdn.microsoft 웹 사이트에서 작동하기 때문에 코드를 사용하기로 결정했습니다.MSDN DropDownList 예제는 항상 첫 번째 값을 반환합니다.
내 index.aspx 두 C# 코드를 시도했지만 둘 다 작동하지, 항상 '흰색'(첫 번째 값) 반환합니다. 누구나 나를 밝혀 줄 수 있습니까?
(PS : 나는 .NET 3.5 MVC2를 사용했습니다 내가 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>
링크의 사용자는 다음과 같이 질문합니다. 포스트 백 이후에 기본값 인 <도시 선택>을 다시 선택한 값으로 가져 오는 방법은 무엇입니까?하지만 반대 값이 필요합니다. 쓰러지 다. – tesla
실제로 잘못된 링크를 제거했습니다. 그리고 추가 세부 사항을 추가했습니다. – guymid
드롭 다운을 채우는 호출이 if (! IsPostBack) 내부에 있다고 생각하는 코드를 보았지만 중단 점을 설정했습니다. 매번 값을 선택하면 if (! IsPostBack) 내부의 중단 점에서 멈추고 if (! Page.IsPostBack)도 사용했습니다. – tesla