2011-07-25 116 views
1

我有一個關於我當前的項目日曆/活動策劃者的問題:
我有一個OnClick提交到沒有存儲過程的直接數據庫。這個想法是,我的日曆中的一天中的頁面或單元格需要直接顯示到那天/單元格中。
現在我需要點擊天數,看它(它存儲在數據庫中)是否有人有任何想法,因爲我曾試圖用JavaScript:如何刷新按鈕提交到直接SQL後的頁面?

<a onClick="window.location.reload()"></a> 

不在一個按鈕,因爲我需要一個按鈕另一個Onclick事件。謝謝你的建議。 :)

我當前的代碼背後是:

using System; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Data.SqlClient; 
using System.Collections; 

public partial class _Default : System.Web.UI.Page 
{ 
    Hashtable _scheduleData; 

    DataView todo = new DataView(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (Calendar1.SelectedDate == DateTime.MinValue) 
      Calendar1.SelectedDate = Calendar1.TodaysDate; 

     _scheduleData = GetSchedule(); 

     Calendar1.Caption = "<br/><h1>Plan School Activiteiten<h1><br />"; 

     Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday; 
     Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth; 
     Calendar1.TitleFormat = TitleFormat.MonthYear; 
     Calendar1.ShowGridLines = true; 
     Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left; 
     Calendar1.DayStyle.CssClass = "Daysoftheweek"; 
     Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top; 
     Calendar1.DayStyle.Height = new Unit(75); 
     Calendar1.DayStyle.Width = new Unit(100); 
     Calendar1.TodayDayStyle.CssClass = "Today"; 
     Calendar1.TodaysDate.ToShortDateString(); 
     Calendar1.VisibleDate = Calendar1.TodaysDate; 
     Calendar1.SelectedDayStyle.CssClass = "SelectStyle"; 

    } 


    private Hashtable GetSchedule() 
    { 
     Hashtable schedule = new Hashtable(); 

     string cnnString = ConfigurationManager.ConnectionStrings["Stefan"].ConnectionString; 
     DataTable dt = new DataTable(); 

     using (SqlConnection con = new SqlConnection(cnnString)) 
     using (SqlCommand cmd = new SqlCommand("select * from Calender", con)) 
     { 
      using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
      { 
       da.Fill(dt); 
      } 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 


     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      string date = Convert.ToDateTime(dt.Rows[i]["date"]).ToShortDateString(); 
      schedule[date] = dt.Rows[i]["todo"].ToString(); 
     } 
     return schedule; 
    } 


    void Page_PreRender() 
    { 
     todo = (DataView)calendarSrc.Select(DataSourceSelectArguments.Empty); 
     todo.Sort = "date"; 


    } 

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
    { 
     string date = e.Day.Date.ToShortDateString(); 
     if (_scheduleData[date] != null) 
     { 

      Literal lit = new Literal(); 
      lit.Text = "<br />"; 
      e.Cell.Controls.Add(lit); 

      Label lbl = new Label(); 
      lbl.Text = (string)_scheduleData[e.Day.Date.ToShortDateString()]; 
      lbl.Font.Size = new FontUnit(FontSize.Small); 
      e.Cell.Controls.Add(lbl); 

     } 
    } 

    protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
    { 
     FormView1.ChangeMode(FormViewMode.Edit); 

    } 

    protected void butAddNew_Click(object sender, EventArgs e) 
    { 
     FormView1.ChangeMode(FormViewMode.Insert); 
     Calendar1.DataBind(); 
    } 
} 

我已經找到了解決方法是如下不是Calender1.DataBind(); 但在這裏我們去

protected void butAddNew_Click(object sender, EventArgs e) 
    { 
     FormView1.ChangeMode(FormViewMode.Insert); 
    } 
    protected void todoSrc_Inserted(object sender, SqlDataSourceStatusEventArgs e) 
    { 
     Refresh(); 
    } 
    protected void todoSrc_Deleted(object sender, SqlDataSourceStatusEventArgs e) 
    { 
     Refresh(); 
    } 
    protected void todoSrc_Updated(object sender, SqlDataSourceStatusEventArgs e) 
    { 
     Refresh(); 
    } 

    private void Refresh() 
    { 
     Response.Redirect(Request.RawUrl); 
    } 

泰所有幫助項目順利:)

+0

在想ViewState的當我點擊按鈕,但我不知道如何解決:P –

+1

我想問題是日曆在OnClick代碼執行之前呈現數據,因此您沒有看到更改?如果是這樣,只需在按鈕單擊事件處理程序中調用'Calendar1.DataBind();'。 –

+0

不把asp:按鈕從FormView的onload到Page_Load和Put DataBind();到Page_Load現在它運作良好 –

回答

1

你應該調用DataBind在CALENDAR1 :)

Calendar1.DataBind();