2009-08-23 96 views
0

在btnSave - 按钮事件处理程序中,在以下两行中,SelectedValue始终返回null。asp.net下拉列表中没有选择任何值

not.TargetSessionCode = this.sessionsDropDownList1.SelectedValue; 
not.CourseCode = this.coursesDropDownList1.SelectedValue; 

有谁能说为什么?

这是我的aspx代码:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="TeacherComposeNotice.aspx.cs" Inherits="Ice_Web_Portal_v_3_0_WebApplication.Teacher.TeacherComposeNotice" Title="Untitled Page" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <table style="width: 270px"> 
     <tr> 
      <td style="text-align: right"> 
       Session:</td> 
      <td colspan="2"> 
       <asp:DropDownList ID="sessionsDropDownList1" runat="server" Width="178px"> 
       </asp:DropDownList></td> 
      <td colspan="1" style="width: 6px"> 
       <asp:Label ID="userNameLabel" runat="server" Text="usernameLabel" Width="167px"></asp:Label></td> 
     </tr> 
     <tr> 
      <td style="text-align: right"> 
       Courses:</td> 
      <td colspan="2"> 
       <asp:DropDownList ID="coursesDropDownList1" runat="server" Width="178px"> 
       </asp:DropDownList></td> 
       <td colspan="1"><asp:Label id="labCourses" runat="server" Width="167px" Text="labCourses"></asp:Label></td> 
     </tr> 
     <tr> 
      <td style="text-align: right"> 
       Subject:</td> 
      <td colspan="3"> 
       <asp:TextBox ID="mailSubjectTextBox" runat="server" Width="343px"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td style="text-align: right" valign="top"> 
       Content:</td> 
      <td colspan="3"> 
       <asp:TextBox ID="mailContentTextBox" runat="server" Height="181px" Width="343px" Font-Names="Verdana"></asp:TextBox></td> 
     </tr> 
     <tr> 
      <td style="text-align: right" valign="top"> 
       Expiry Date :</td> 
      <td colspan="3"> 
      </td> 
     </tr> 
     <tr> 
      <td style="text-align: right" valign="top"> 
      </td> 
      <td colspan="3" style="text-align: center"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td style="text-align: right" valign="top"> 
      </td> 
      <td colspan="3" style="text-align: left"> 
       <asp:Label ID="errorMessageLabel" runat="server" ForeColor="#C00000" Text="***" Width="314px"></asp:Label></td> 
     </tr> 
     <tr> 
      <td style="text-align: right" valign="top"> 
      </td> 
      <td colspan="3" style="text-align: left"> 
       <asp:Button ID="btnBack" runat="server" OnClick="btnBack_Click" Text="Back" /> 
       <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" /></td> 
     </tr> 
    </table> 
</asp:Content> 

这是我的代码隐藏:

public partial class TeacherComposeNotice : System.Web.UI.Page 
     { 
      string username = string.Empty; 

      protected void Page_Load(object sender, EventArgs e) 
      { 
       username = (string)Request.QueryString["username"]; 

       if (!IsPostBack) 
       { 
        LoadAllSessionsToDDL(); 
        LoadAllCoursesToDDL(); 
       } 

      } 

      private void LoadAllSessionsToDDL() 
      { 
       this.sessionsDropDownList1.Items.Clear(); 

       List<Ice_Web_Portal.BO.Session_> items = Ice_Web_Portal.BO.Session_.GetAllSessions(); 

       this.sessionsDropDownList1.DataSource = items; 
       this.sessionsDropDownList1.DataTextField = "SessionName"; 
       this.sessionsDropDownList1.DataValueField = "SessionCode"; 
       this.sessionsDropDownList1.DataBind(); 

       this.sessionsDropDownList1.SelectedIndex = 0; 
      } 

      private void LoadAllCoursesToDDL() 
      { 
       this.coursesDropDownList1.Items.Clear(); 

       List<Course> items = Course.GetCourses(); 

       this.coursesDropDownList1.DataSource = items; 
       this.coursesDropDownList1.DataTextField = "CourseName"; 
       this.coursesDropDownList1.DataValueField = "CourseCode"; 
       this.coursesDropDownList1.DataBind(); 
      } 

      protected void btnBack_Click(object sender, EventArgs e) 
      { 
       Server.Transfer("~/Teacher/TeacherControlPanel.aspx?username=" + username); 
      } 

      protected void btnSave_Click(object sender, EventArgs e) 
      { 
       Ice_Web_Portal.BO.Teacher teacher = Ice_Web_Portal.BO.Teacher.GetTeacherByUsername(username); 

       if (teacher != null) 
       { 
        Notice not = new Notice(); 
        not.HangingDate = DateTime.Now; 
        not.TeacherCode = teacher.TeacherCode; 
        not.TargetSessionCode = this.sessionsDropDownList1.SelectedValue; 
        not.CourseCode = this.coursesDropDownList1.SelectedValue; 
        not.NoticeSubject = this.mailSubjectTextBox.Text; 
        not.NoticeContent = this.mailContentTextBox.Text; 
        not.ExpiryDate = DateTime.Now.AddMonths(1); 
        not.IsExpired = false; 

        bool success = Notice.Save(not); 

        if (success) 
        { 
         errorMessageLabel.Text = "Saved"; 
        } 
        else 
        { 
         errorMessageLabel.Text = "Save failed"; 
        } 
       } 
       else 
       { 
        //labErrorMessage.Text = "No teacher found"; 
       } 
      } 
     } 

回答

1

很奇怪。你的代码似乎很完美。我试图用下面的代码重建你的页面:

public partial class DropdownTests : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      LoadAllCoursesToDDL(); 
     } 
    } 

    private List<Course> GetDummyCourses() 
    { 
     return new List<Course>{ 
      new Course{ CourseCode = "A12", CourseName = "Introduction to Programming"}, 
      new Course{ CourseCode = "B112", CourseName = "Theory of Computing"} 
     }; 
    } 

    private void LoadAllCoursesToDDL() 
    { 
     this.coursesDropDownList1.Items.Clear(); 

     List<Course> items = GetDummyCourses(); 

     this.coursesDropDownList1.DataSource = items; 
     this.coursesDropDownList1.DataTextField = "CourseName"; 
     this.coursesDropDownList1.DataValueField = "CourseCode"; 
     this.coursesDropDownList1.DataBind(); 
    } 

    protected void btnBack_Click(object sender, EventArgs e) 
    { 
     //Server.Transfer("~/Teacher/TeacherControlPanel.aspx?username=" + username); 
    } 

    protected void btnSave_Click(object sender, EventArgs e) 
    { 
     string code = coursesDropDownList1.SelectedItem.Value; 
     string code2 = coursesDropDownList1.SelectedValue; 
    } 
} 

public class Course 
{ 
    public string CourseCode { get; set; } 
    public string CourseName { get; set; } 
} 

它只是工作得很好。如果您想直接使用coursesDropdownList1.SelectedItem.Value而不是“SelectedValue”。应该不重要,但只是试试看。

作为一个尝试,我还会创建一个新页面,在那里复制aspx代码并为其创建代码隐藏。在我看来,必定有更多的东西导致它失败。你是否在运行时动态地创建和添加一些字段到UI?一些自定义服务器控件等?你的问题可能是你丢失了ViewState。

+0

不,我没有动态添加任何东西。还有什么要检查? courseDropDownLoat.SelectedItem为null。 – anonymous 2009-08-23 14:42:34

+0

您是否尝试“重新编程”此页面。基本上将ASPX代码+代码隐藏放在一个新文件中。通常这可以帮助你。顺便说一句,我的简单例子是否适合你? – Juri 2009-08-23 14:58:54

+1

我还记得另一件事。检查你的母版页或你指定的地方“EnableViewState = false” – Juri 2009-08-23 14:59:36

1

我有同样的问题,但enableViewState = true是使它工作的唯一解决方案。 很奇怪。