前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebEdit.aspx.cs" Inherits="GridWive.WebEdit" %>
<!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 runat="server">
<title></title>
<link rel="stylesheet" href="Kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="Kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src="Kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="Kindeditor/lang/zh_CN.js"></script>
<script charset="utf-8" src="Kindeditor/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function (K) {
var editor1 = K.create('#content1', {
cssPath: 'Kindeditor/plugins/code/prettify.css',
uploadJson: 'Kindeditor/asp.net/upload_json.ashx',
fileManagerJson: 'Kindeditor/asp.net/file_manager_json.ashx',
allowFileManager: true,
afterCreate: function () {
var self = this;
K.ctrl(document, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
K.ctrl(self.edit.doc, 13, function () {
self.sync();
K('form[name=example]')[0].submit();
});
}
});
prettyPrint();
});
</script>
</head>
<body>
<asp:Label ID="Label1" runat="server" Text="" Visible="false"></asp:Label>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>编号</td>
<td>
<asp:TextBox ID="txtId" runat="server" Enabled="False" ReadOnly="True"
Width="183px"></asp:TextBox>
</td>
</tr>
<tr><td>标题</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" Height="17px" Width="182px"></asp:TextBox>
</td>
</tr>
<tr><td>内容</td>
<td>
<textarea id="content1" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;" runat="server"></textarea>
</td>
</tr>
<tr><td>类别</td>
<td>
<asp:DropDownList ID="ddlClassName" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr><td>用户</td>
<td>
<asp:DropDownList ID="ddlUser" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan=2>
<asp:Button ID="btnUpdate" runat="server" Text="保存" onclick="btnUpdate_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace GridWive
{
public partial class WebEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = Request.Form["content1"];
if (!IsPostBack)
{
string id = Request.QueryString["id"];
GetNews1(id);
LoadUser();
LoadClass();
}
}
private void LoadUser()
{
string strcon = @"Data Source=PC-DLL;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=linlin";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = " SELECT * FROM T_User";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
this.ddlUser.DataSource = dt;
this.ddlUser.DataTextField = "RealName";
this.ddlUser.DataValueField = "UserId";
this.ddlUser.DataBind();
}
private void LoadClass()
{
string strcon = @"Data Source=PC-DLL;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=linlin";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = " SELECT * FROM T_NewsClass";
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
this.ddlClassName.DataSource = dt;
this.ddlClassName.DataTextField = "ClassName";
this.ddlClassName.DataValueField = "ClassId";
this.ddlClassName.DataBind();
}
private void GetNews1(string id)
{
string strcon = @"Data Source=PC-DLL;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=linlin";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = " SELECT * FROM T_News1 WHERE Id=@id";
cmd.Parameters.AddWithValue("@id", id);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
txtId.Text = dt.Rows[0]["Id"].ToString();
txtTitle.Text = dt.Rows[0]["NewsTitle"].ToString();
content1.InnerHtml = dt.Rows[0]["NewsContent"].ToString();
LoadUser();
string userid = dt.Rows[0]["NewsCreator"].ToString();
foreach (ListItem item in this.ddlUser.Items)
{
if (item.Value == userid)
{
item.Selected = true;
}
}
LoadClass();
string classid = dt.Rows[0]["ClassId"].ToString();
foreach (ListItem item in this.ddlClassName.Items)
{
if (item.Value == classid)
{
item.Selected = true;
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
#region 获取用户输入或选择的值
string title = txtTitle.Text;
string content = Label1.Text;
string userid = this.ddlUser.SelectedItem.Value;
string calssid = this.ddlClassName.SelectedItem.Value;
#endregion
#region 更改数据
string strcon = @"Data Source=PC-DLL;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=linlin";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "UPDATE T_News1 SET NewsTitle=@newstitle,NewContent=@newscontent,NewsCreator=@creator,ClassId=@classid WHERE Id=@id";
cmd.Parameters.AddWithValue("@newstitle",title);
cmd.Parameters.AddWithValue("@newscontent",content);
cmd.Parameters.AddWithValue("@creator",userid);
cmd.Parameters.AddWithValue("@classid",calssid);
cmd.Parameters.AddWithValue("@id",txtId.Text);
if (cmd.ExecuteNonQuery()>0)
{
Response.Redirect("grid.aspx");
}
cmd.Dispose();
conn.Dispose();
#endregion
}
}
}