2014-04-02 21 views
0

我在ascx頁面中有一個asp日曆實用程序。我想在同一日期的第二次點擊時取消選擇所選日期。以下是我的代碼。請在此幫助。取消選中日曆中的選定日期

<%@ Control Language="C#" AutoEventWireup="true"CodeFile="Calendar.ascx.cs"Inherits="WebUserControl" %> 

<table> 
    <tr> 
    <td width="100%"> 

     <asp:DropDownList ID="ddlyear" runat="server" 
      onselectedindexchanged="ddlyear_SelectedIndexChanged" AutoPostBack="true"> 

       <asp:ListItem Text="2014" Value="2014" Selected="True"></asp:ListItem> 
       <asp:ListItem Text="2015" Value="2015"></asp:ListItem> 
       <asp:ListItem Text="2016" Value="2016"></asp:ListItem> 
       <asp:ListItem Text="2017" Value="2017"></asp:ListItem> 
       <asp:ListItem Text="2018" Value="2018"></asp:ListItem> 
       <asp:ListItem Text="2019" Value="2019"></asp:ListItem> 
       <asp:ListItem Text="2020" Value="2020"></asp:ListItem> 
       <asp:ListItem Text="2021" Value="2021"></asp:ListItem> 
       <asp:ListItem Text="2022" Value="2022"></asp:ListItem> 
       <asp:ListItem Text="2023" Value="2023"></asp:ListItem> 
       <asp:ListItem Text="2024" Value="2024"></asp:ListItem> 

     </asp:DropDownList> 
    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td width="100%" dir="ltr"> 

     <asp:DataList ID="DataList1" runat="server" HorizontalAlign="Center" 
      RepeatDirection="Horizontal" RepeatColumns="4" 
      onitemdatabound="DataList1_ItemDataBound" 
      > 
     <HeaderTemplate> 

     </HeaderTemplate> 
     <ItemTemplate> 
      <asp:Label ID="txt1" runat="server" Text='<%#Eval("Month") %>' Visible="false" Font-Names="Arial"></asp:Label> 
      <asp:HiddenField ID="hdn1" runat="server" /> 
      <asp:Calendar ID="Calendar1" runat="server" NextPrevFormat="CustomText" SelectionMode="Day" NextMonthText="" PrevMonthText="" Font-Names="A" OtherMonthDayStyle-BorderStyle="NotSet" OtherMonthDayStyle-Wrap="False" OtherMonthDayStyle-ForeColor="#CCCCCC"> 
      <TitleStyle 
       BackColor="#6EC347" 
       ForeColor="White" 
       Height="36" 
       Font-Size="Large" 
       Font-Names="Arial" 
       /> 
        <SelectedDayStyle 
       BackColor="Green" 
       BorderColor="SpringGreen" 
       /> 
      </asp:Calendar> 
     </ItemTemplate> 
     </asp:DataList> 
    </td> 
    </tr> 
    </table> 

我背後這個ASCX頁的代碼是

public partial class WebUserControl : System.Web.UI.UserControl 
{ 
int month = 1; 
public event EventHandler YearChanged; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     loadcalendar(); 

    } 

} 
private void loadcalendar() 
{ 

    DataTable dt = new DataTable(); 
    dt.Columns.Add("Month", typeof(string)); 
    dt.Rows.Add("January"); 
    dt.Rows.Add("February"); 
    dt.Rows.Add("March"); 
    dt.Rows.Add("April"); 
    dt.Rows.Add("May"); 
    dt.Rows.Add("June"); 
    dt.Rows.Add("July"); 
    dt.Rows.Add("August"); 
    dt.Rows.Add("September"); 
    dt.Rows.Add("October"); 
    dt.Rows.Add("Novemeber"); 
    dt.Rows.Add("December"); 

    DataList1.DataSource = dt; 
    DataList1.DataBind(); 

} 

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
      e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     string year = ddlyear.SelectedValue.ToString(); 
     String str = ((Label)e.Item.FindControl("txt1")).Text; 

     DateTime Now = DateTime.Now; 
     DateTime TempDate = new DateTime(Convert.ToInt32(year), month, 1); 

     // DateTime TempDate = new DateTime(Now.Year,Now.Month, 1); 
     ((Calendar)e.Item.FindControl("Calendar1")).VisibleDate = TempDate; 
     month = month + 1; 
    } 

} 

protected void ddlyear_SelectedIndexChanged(object sender, EventArgs e) 
{ 

     loadcalendar(); 
     YearChanged(sender, e); 


} 

}

我Aspx.cs頁面代碼是這樣的

 SqlConnection cnn = new SqlConnection(); 
     string connStr = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString; 
     cnn.ConnectionString = connStr; 
     cnn.Open(); 
     string eid; 
     string ename = DropDownList2.SelectedValue.ToString(); 
     if (ename == "1") 
     { 
      eid = "1"; 
     } 
     else 
     { 
      eid = "2"; 
     } 
     int cid = Int32.Parse(DropDownList1.SelectedValue.ToString()); 
     //String sqlSelect = String.Format("Select Customer_Id from Customer where Customer_Name='{0}'", cname); 
     //SqlCommand cmd1 = new SqlCommand(sqlSelect, cnn); 
     //int cid = (int)cmd1.ExecuteScalar(); 
     DataList dl; 
     dl = ((DataList)patchCalendar.FindControl("DataList1")); 


     foreach (DataListItem dli in dl.Items) 
     { 
       string month = Convert.ToString(((Label)dli.FindControl("txt1")).Text); 
       string day = Convert.ToString(((Calendar)dli.FindControl("Calendar1")).SelectedDate.Day); 
       string year = Convert.ToString(((Calendar)dli.FindControl("Calendar1")).SelectedDate.Year); 
       string date = ((Calendar)dli.FindControl("Calendar1")).SelectedDate.ToShortDateString(); 
       if (date == "1/1/0001") 
       { 
        year = null; 
        date = null; 
        day = null; 
       } 
       else 
       { 

        SqlCommand cmd = new SqlCommand("insert into Patching_Dates values(@cid,@eid,@year,@month,@day,@date)", cnn); 
        cmd.Parameters.AddWithValue("@cid", cid); 
        cmd.Parameters.AddWithValue("@eid", eid); 
        cmd.Parameters.AddWithValue("@year", year); 
        cmd.Parameters.AddWithValue("@month", month); 
        cmd.Parameters.AddWithValue("@day", day); 
        cmd.Parameters.AddWithValue("@date", date); 

        cmd.ExecuteNonQuery(); 
       } 
     } 
     cnn.Close(); 
     string act = "Patching Dates Added"; 
     cnn.Open(); 
     SqlCommand cmd2 = new SqlCommand("insert into Logs values(@Username,@Activity,@Time)", cnn); 
     cmd2.Parameters.AddWithValue("@Username", (string)(Session["user"])); 
     cmd2.Parameters.AddWithValue("@Activity", act); 
     cmd2.Parameters.AddWithValue("@Time", System.DateTime.Now.ToString()); 
     cmd2.ExecuteNonQuery(); 
     cnn.Close(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
    } 
+0

看到這個 - http://stackoverflow.com/questions/2627716/deselect-dates-in-asp-net-calendar-control這將幫助你! –

+0

@JalpeshVadgama否這是行不通的 –

+0

你是否在處理選定索引中的相同內容? –

回答

0

首先檢查其CALENDAR1控制值,如果是的,然後通過這個重置它

Calendar1.SelectedDates.Clear(); 
+0

我的日曆在ascx文件中。 –

相關問題