📄 defaultcs.aspx.cs
字号:
using System;
using System.Data.OleDb;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.WebControls;
namespace Telerik.EditorExamplesCSharp.Editor.Examples.Database1
{
/// <summary>
/// Summary description for DefaultCS.
/// </summary>
public class DefaultCS : Telerik.QuickStart.XhtmlPage
{
protected HtmlInputHidden EditedNews;
protected DataGrid NewsGrid;
protected RadEditor NewsEditor;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ReadAllRecords();
}
}
// Method bellow is called on SubmitClicked event - (associated with it in InitializeComponent())
// Important!!! The method is called AFTER Page_Load method is executed, so make sure you do not
// inadvertently overwrite the editor content there before you process it here!
public void NewsEditor_SubmitClicked(object sender, EventArgs e)
{
OleDbConnection connection = CreateConnection();
OleDbCommand command = null;
if (EditedNews.Value != string.Empty)
{
command = new OleDbCommand("UPDATE News SET NewsDate = Now(), NewsText = @content WHERE NewsID = @nid", connection);
command.Parameters.Add("content", NewsEditor.Html);
command.Parameters.Add("nid", Convert.ToInt32(EditedNews.Value));
}
else
{
command = new OleDbCommand("INSERT INTO News (NewsDate, NewsText) VALUES (Now(), @content)", connection);
command.Parameters.Add("content", NewsEditor.Html);
}
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
}
connection.Close();
this.Response.Redirect(this.Request.Url.PathAndQuery);
}
private OleDbConnection CreateConnection()
{
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Request.MapPath(this.TemplateSourceDirectory + "\\News.mdb") + ";User ID=;Password=;";
connection.Open();
return connection;
}
private void NewsGrid_ItemCommand( object sender, DataGridCommandEventArgs args)
{
OleDbConnection connection = CreateConnection();
if (args.CommandName == "Delete")
{
OleDbCommand com = new OleDbCommand("DELETE FROM News WHERE NewsID = @nid", connection);
com.Parameters.Add("nid", args.Item.Cells[0].Text);
com.ExecuteNonQuery();
connection.Close();
}
else if (args.CommandName == "Edit")
{
OleDbCommand command = new OleDbCommand("SELECT NewsText FROM News WHERE NewsID = @nid", connection);
command.Parameters.Add("nid", args.Item.Cells[0].Text);
OleDbDataReader record = command.ExecuteReader();
if (record.Read())
{
NewsEditor.Html = record.GetString(0);
EditedNews.Value = args.Item.Cells[0].Text;
}
else
{
NewsEditor.Html = "";
EditedNews.Value = "";
}
record.Close();
}
// Add code to delete row from data source.
ReadAllRecords();
}
private void ReadAllRecords ()
{
OleDbConnection connection = CreateConnection();
OleDbCommand command2 = new OleDbCommand("SELECT NewsID, NewsDate, NewsText FROM News", connection);
NewsGrid.DataSource = command2.ExecuteReader();
NewsGrid.DataBind();
connection.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
// Associate event handlers with events
this.NewsEditor.SubmitClicked += new EventHandler(this.NewsEditor_SubmitClicked);
this.NewsGrid.ItemCommand += new DataGridCommandEventHandler(this.NewsGrid_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -