添加页(连上)

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WeAdd.aspx.cs" Inherits="GridWive.WeAdd" %>


<!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="example" runat="server">
     <div>
        <table>
            <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="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 WeAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = Request.Form["content1"];
            if (!IsPostBack)
            {
                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();
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text;
            string content = Label1.Text;
            string userid = this.ddlUser.SelectedItem.Value;
            string classid = this.ddlClassName.SelectedItem.Value;
            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 = "INSERT INTO T_News1 (NewsTitle,NewsContent,NewsCreator,Classid,CreateTime)VALUES(@titile,@content,@creator,@classid,GETDATE())";
            cmd.Parameters.AddWithValue("@titile", title);
            cmd.Parameters.AddWithValue("@content", content);
            cmd.Parameters.AddWithValue("@creator", userid);
            cmd.Parameters.AddWithValue("@classid",classid);
            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Redirect("grid.aspx");
            }
        }
    }
}

原文地址:https://www.cnblogs.com/duanlinlin/p/2994395.html