2017-06-17 61 views
0

我是c sharp的新手,我正在開發一個簡單的項目,但發現自己卡住了。下拉列表中的用戶點擊事件處理程序沒有觸發

我有一個文本字段,只顯示時間和一些下拉列表,我想分別改變當時的顏色,背景和字體大小。我的代碼配置如下,但沒有看到發生在選擇上。

任何想法爲什麼我的事件沒有被處理?

這是CS後面的代碼:

public partial class WebTime : System.Web.UI.Page 
{ 
    protected void Page_Init(object sender, EventArgs e) 
    { 

     timeLabel.Text = DateTime.Now.ToString("hh:mm:ss"); 
    } 


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     timeLabel.BackColor = Color.FromName(DropDownList1.SelectedValue); 
    } 

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     timeLabel.ForeColor = Color.FromName(DropDownList2.SelectedValue); 
    } 

    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     timeLabel.Font.Size = Convert.ToInt32(DropDownList3.DataValueField); 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      timeLabel.BackColor = Color.FromName(DropDownList1.SelectedValue); 
      timeLabel.ForeColor = Color.FromName(DropDownList2.SelectedValue); 
      timeLabel.Font.Size = Convert.ToInt32(DropDownList3.DataValueField); 
     } 
    } 
} 

這是前端的asp:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebTime.aspx.cs" Inherits="WebTime" %> 

    <!DOCTYPE html> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title>&nbsp;Simple Web Form Example</title> 
     <style type="text/css"> 
      .timeStyle { 
       color: #FFFF00; 
       font-size: xx-large; 
       background-color: #000000; 
      } 
      .listOption{ 
       margin: 10px 30px 10px 30px; 
      } 
     </style> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
     <div> 

      <h1>Current time on the Web server:</h1> 
      <p> 
       <asp:Label ID="timeLabel" runat="server" CssClass="timeStyle"></asp:Label> 
      </p> 

     </div> 
      <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="listOption"> 
        <asp:ListItem Text="Black" Value="black" Selected="True" /> 
        <asp:ListItem Text="Red" Value="red" /> 
        <asp:ListItem Text="Blue" Value="blue" /> 
        <asp:ListItem Text="Green" Value="green" /> 
      </asp:DropDownList> 
      <asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" CssClass ="listOption"> 
       <asp:ListItem Text="Yellow" Value="#FFFF00" Selected="True" /> 
       <asp:ListItem Text="Red" Value="#FF0000" /> 
       <asp:ListItem Text="Blue" Value="#0000FF" /> 
       <asp:ListItem Text="Green" Value="#008000" /> 
     </asp:DropDownList> 
     <asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" CssClass="listOption"> 
       <asp:ListItem Text="24" Value="24px" Selected="True" /> 
       <asp:ListItem Text="32" Value="32px" /> 
       <asp:ListItem Text="64" Value="64px" /> 
       <asp:ListItem Text="128" Value="128px" /> 
     </asp:DropDownList> 
    </form> 
</body> 
</html> 

回答

0

我發現我自己的答案。需要爲每個下拉列表設置短期版本Autopostback = True

更多細節是更好地在這裏解釋這個問題應該是重複

C# dropdownlist change event