按钮提交到Direct SQL后如何刷新页面?

时间:2011-07-25 06:08:51

标签: c# asp.net visual-studio-2010 calendar

我对我当前的项目日历/活动策划者有疑问:
我有一个OnClick提交到没有存储过程的直接数据库。我的想法是,我日历中当天的页面或单元格需要直接显示在那一天/单元格中 现在我需要点击日期号来查看它(它存储在数据库中)是否有人有任何想法,因为我尝试过使用JavaScript:

<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);
    }

所有这些都是为了帮助项目顺利进行:)

1 个答案:

答案 0 :(得分:1)

你应该在Calendar1上调用DataBind:)

Calendar1.DataBind();