2012-07-21 270 views
0

不改變標籤我有這個Default.aspx.cs按鈕上單擊

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Xml; 
using System.IO; 


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

     Calendar1.SelectedDate = DateTime.Today; 
     info.Visible = false; 
     meeting.Visible = true; 
     member.Visible = true; 
     label.Visible = true; 
    } 

    protected void Button3_Click(object sender, EventArgs e) 
    { 
     DateTime Now = DateTime.Today; 
     DateTime wed1 = getFirstWednesdayOfMonth(Now); 
     DateTime wed3 = getThirdWednesdayOfMonth(Now); 

     if (Now < wed1) 
     { 
      Label5.Text = "The next meeting is on" + "\n" + wed1.ToShortDateString() + "\nat 7 pm in room S242"; 
     } 
     else if (Now < wed3) 
     { 
      Label5.Text = "The next meeting is on" + "\n" + wed3.ToShortDateString() + "\nat 7 pm in room S242"; 
     } 
    } 

    protected DateTime getFirstWednesdayOfMonth(DateTime seedDate) 
    { 
     DateTime wed1 = new DateTime(seedDate.Year, seedDate.Month, 1); 
     while (wed1.DayOfWeek != DayOfWeek.Wednesday) 
     { 
      wed1 = wed1.AddDays(1); 
     } 
     return wed1; 
    } 
    protected DateTime getThirdWednesdayOfMonth(DateTime seedDate) 
    { 
     DateTime wed3 = new DateTime(seedDate.Year, seedDate.Month, 15); 
     while (wed3.DayOfWeek != DayOfWeek.Wednesday) 
     { 
      wed3 = wed3.AddDays(1); 
     } 
     return wed3; 
    } 

    protected void btnInfo_Click(object sender, EventArgs e) 
    { 
     btnInfo.Visible = false; 
     info.Visible = true; 

    } 

    protected void Button2_Click(object sender, EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      if (validateFirst.IsValid && validateLast.IsValid && validatePhone.IsValid && validateEmail.IsValid) 
      { 
       lblThanks.Text = "Thanks! you will contacted in 2 business days!"; 
       info.Visible = false; 
      } 
     } 
    } 
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (ListBox1.SelectedValue == "analyst") 
     { 


     } 
    } 
} 

和Default.aspx的

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

<!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"> 
<head id="Head1" runat="server"> 
    <title></title> 
    <style type="text/css"> 
     .heading 
     { 
      text-align: center; 
      border-style: double; 
      background-color:#F2A988; 
      font-family:Sans-Serif; 
      font-weight:bold; 

     } 
     #wrapper { 
      margin: 0 auto; 
      width: 400px; 
      text-align:left; 
     } 
     .style1 
     { 
      text-align: center; 
      font-family:Sans-Serif; 
     } 
     .style2 
     { 
      font-family: Arial, Helvetica, sans-serif; 
     } 
    </style> 
</head> 
<body style="height: 642px; width: 911px; " id="wrapper"> 
    <form id="form1" runat="server"> 

    <div class="heading"> 
     ABC Computer Club - Inquiry Form</div> 
     <br /> 

    <asp:Calendar ID="Calendar1" runat="server" Font-Names="Arial"></asp:Calendar> 



     <br /> 
    <asp:Label ID="lblThanks" runat="server" 
     style="font-family: Arial, Helvetica, sans-serif; font-weight: 700"></asp:Label> 
    <br /> 



     <br /> 

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:Button ID="btnInfo" runat="server" Font-Names="Arial" 
     style="font-weight: 700" 
     Text="I'd like to receive more information!" Width="261px" 
     onclick="btnInfo_Click" /> 

    <div runat="server" id="info" 
     style="position:relative; top: -1px; left: 7px; width: 463px; height: 415px;"> 
     <span class="style2">If you would like to join the club or receive more 
     information about the club, fill in the form below and click the submit button.</span><br /> 
     <br /> 
     <asp:Label ID="Label1" runat="server" 
      style="font-weight: 700; font-family: Arial, Helvetica, sans-serif" 
      Text="First Name:"></asp:Label> 
&nbsp;<asp:TextBox ID="txtFName" runat="server"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Label ID="Label2" runat="server" 
      style="font-weight: 700; font-family: Arial, Helvetica, sans-serif" 
      Text="Last Name:"></asp:Label> 
&nbsp;<asp:TextBox ID="txtLName" runat="server"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Label ID="Label3" runat="server" 
      style="font-weight: 700; font-family: Arial, Helvetica, sans-serif" 
      Text="Phone #:"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Label ID="Label4" runat="server" 
      style="font-weight: 700; font-family: Arial, Helvetica, sans-serif" 
      Text="Email:"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> 
     <br /> 
     <br /> 
     <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Submit" /> 
     <br /> 
     <br /> 
     <asp:RequiredFieldValidator ID="validateFirst" runat="server" 
      ControlToValidate="txtFName" ErrorMessage="First Name is Required" 
      style="font-family: Arial, Helvetica, sans-serif"></asp:RequiredFieldValidator> 
     <br /> 
     <br /> 
     <asp:RequiredFieldValidator ID="validateLast" runat="server" 
      ControlToValidate="txtLName" ErrorMessage="Last Name is Required" 
      style="font-family: Arial, Helvetica, sans-serif"></asp:RequiredFieldValidator> 
     <br /> 
     <br /> 
     <asp:RegularExpressionValidator ID="validatePhone" runat="server" 
      ControlToValidate="txtPhone" ErrorMessage="Phone Number is invalid" 
      style="font-family: Arial, Helvetica, sans-serif" 
      ValidationExpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"></asp:RegularExpressionValidator> 
     <br /> 
     <br /> 
     <asp:RegularExpressionValidator ID="validateEmail" runat="server" 
      ControlToValidate="txtEmail" ErrorMessage="Invalid Email" 
      style="font-family: Arial, Helvetica, sans-serif" 
      ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 
    </div> 

    <div runat="server" id="meeting" 
     style="position:absolute; top: 150px; left: 580px; width: 232px; height: 64px; text-align: center; margin-top: 0px; font-family: Arial; font-weight:bold;"> 
     <br /> 
&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Label ID="Label5" runat="server" Text="Test"></asp:Label> 
     <br /> 
     </div> 



    <div runat="server" id="label" style="width: 308px; position:absolute; top: 45px; left: 550px; margin-top: 0px;" 
     class="style1"> 
     Click Below to find out when 
     <br /> 
     the club meets next</div> 

    <div runat="server" id="member" 



     style="position:absolute; top: 28px; left:880px; width: 274px; height: 441px; font-family:Sans-Serif;"> 
     <b>View Members by Program:<br /> 
     <asp:ListBox ID="ListBox1" runat="server" Height="175px" Width="213px" 
      onselectedindexchanged="ListBox1_SelectedIndexChanged"> 
      <asp:ListItem Value="all">All</asp:ListItem> 
      <asp:ListItem Value="programmer">Computer Programmer</asp:ListItem> 
      <asp:ListItem Value="itss">Information Technologies Support Services</asp:ListItem> 
      <asp:ListItem Value="analyst">Systems Analyst</asp:ListItem> 
     </asp:ListBox> 
     <br /> 
     <br /> 
     </b></div> 

    <div style="position:absolute; top: 99px; left: 600px; width: 192px; text-align: center; margin-top: 0px;"> 
     <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
      Text="Next Meeting" /> 
    </div> 

    </form> 
</body> 
</html> 

的問題是,每當我點擊「下次會議」什麼也沒有發生,而它應該將標籤文本設置爲月的第一個或第三個星期三。它曾經工作過,但從我複製整個代碼到一個新的網站,它不工作。可能是什麼問題?

+1

請將您的標題修改爲更加描述實際問題的內容。 – Oded 2012-07-21 21:22:47

+0

另外,你是否檢查過你的新網站,看它是否被正確配置爲使用正確的.NET版本? – Oded 2012-07-21 21:24:42

+1

定義「不工作」。不回覆,回帖後不更改標籤,什麼? – freefaller 2012-07-21 21:24:57

回答

1

你需要第三種情況

DateTime getFirstWednesdayOfNextMonth(DateTime seedDate) 
{ 
    int year = seedDate.Year; 
    int month = seedDate.Month+1; 
    if (month > 12) 
    { 
     month = 1; 
     year = year + 1; 
    } 
    DateTime wed1 = new DateTime(year, month, 1); 
    while (wed1.DayOfWeek != DayOfWeek.Wednesday) 
    { 
     wed1 = wed1.AddDays(1); 
    } 
    return wed1; 
} 

,並添加結束其他對您Button3_Click

protected void Button3_Click(object sender, EventArgs e)  
{  
    DateTime Now = DateTime.Today;  
    DateTime wed1 = getFirstWednesdayOfMonth(Now);  
    DateTime wed3 = getThirdWednesdayOfMonth(Now);  
    DateTime wedNext = getFirstWednesdayOfNextMonth(Now);  

    if (Now < wed1)  
    {  
     Label5.Text = "The next meeting is on" + "\n" + wed1.ToShortDateString() + "\nat 7 pm in room S242";  
    }  
    else if (Now < wed3)  
    {  
     Label5.Text = "The next meeting is on" + "\n" + wed3.ToShortDateString() + "\nat 7 pm in room S242";  
    }  
    else  
    {  
     Label5.Text = "The next meeting is on" + "\n" + wedNextMonth.ToShortDateString() + "\nat 7 pm in room S242";  
    }  
}  

順便說一句,當你知道這個月的第一個星期三時,計算另外兩個很簡單,就是將第一個星期加14。

DateTime wed1 = getFirstWednesdayOfMonth(Now); 
    DateTime wed3 = wed.AddDays(14); 
    DateTime wedNext = wed3.AddDays(14); 
    // But it could be in the same month (1,8,15,22,29) 
    if(wedNext.Month == wed3.Month) 
    wedNext = wedNext.AddDays(7); 
2

該代碼從未正常工作。也許它在2012年7月18日之前似乎有效。2012年7月這是本月的第三個星期三,因爲那時是(Now < wed3) == true

現在Now < wed1Now < wed3都不是真的,這是更改標籤文本屬性的唯一方法。

if (Now < wed1) 
{ 
    Label5.Text = "The next meeting is on" + "\n" + wed1.ToShortDateString() + "\nat 7 pm in room S242"; 
} 
else if (Now < wed3) 
{ 
    Label5.Text = "The next meeting is on" + "\n" + wed3.ToShortDateString() + "\nat 7 pm in room S242"; 
} 

也許你需要告訴我們你的真實需求。你的程序應該做什麼?

您可能還想將page_load中的默認設置設爲if(!IsPostback)檢查,否則總是會覆蓋Calendar.SelectedDate

if(!Page.IsPostBack) 
{ 
    // not on postbacks, f.e. when the user selected a date 
    Calendar1.SelectedDate = DateTime.Today; 
    // .... 
} 

但是有一點,如果沒有第一或第三個星期三是可用的,它 應顯示在下個月的第一個星期三。對?

這就是我所要求的缺失要求。由於第三個星期三已經過去,要返回NEX月份的第三個星期三:

public static DateTime getNextWeekDay(DateTime date, DayOfWeek dayOfWeek, int weekNum) 
{ 
    if (weekNum < 1 || weekNum > 4) 
     throw new ArgumentException("Number of week must be between 1 and 4.", "weekNum"); 

    int inWeekNum = date.Date.Day/7; 
    if (inWeekNum >= weekNum) 
    { 
     var nextMonth = date.AddMonths(1); 
     date = new DateTime(nextMonth.Year, nextMonth.Month, 1); 
    } 
    DateTime currentDate = date; 
    int currentWeekNum = 0; 
    while (currentWeekNum < weekNum) 
    { 
     if (currentDate.DayOfWeek == dayOfWeek) 
      currentWeekNum++; 
     if (currentWeekNum == weekNum) 
      break; 
     else 
      currentDate = currentDate.AddDays(1); 
    } 
    return currentDate; 
} 

佑可以使用這種方式:

DateTime wed1 = getNextWeekDay(Now, DayOfWeek.Wednesday, 1); 
DateTime wed3 = getNextWeekDay(Now, DayOfWeek.Wednesday, 3); 
+0

我真正的要求是我想顯示下個月的第一或第三個星期三。我通過設置標籤來完成。 – unknownsatan 2012-07-21 21:46:16

+0

@AnkurKaushal:既然已經是過去了,那麼這個月沒有「下一個可用的三分之一」。程序也能正常工作嗎? – 2012-07-21 21:47:47

+0

除「下一個會議」按鈕外,一切正常。 :/ – unknownsatan 2012-07-21 21:50:04