日期:2014-05-17 浏览次数:21383 次
public partial class Admin_AddNews : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ListBind(); } } protected void Button1_Click(object sender, EventArgs e) { DateTime addtime = DateTime.Now; SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["constr"]); string insr = "insert into News (NewsTitle,NewsClassID,NewsContent,NewsAddtime,NewsClick,NewsAuthor) values('" + Title.Text + "','" + DropDownList1.SelectedValue + "','" + NewsContent.Text.Trim() +"','" + addtime + "',0,'" + Author.Text + "')"; conn.Open(); SqlCommand cmd = new SqlCommand(insr, conn); cmd.ExecuteNonQuery(); Label1.Text = "添加成功!"; Label1.Visible = true; conn.Close(); } public void ListBind() { SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["constr"]); string sql = "select * from NewsClass order by NewsID desc"; conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "NewsClass"); DropDownList1.DataSource = ds.Tables["NewsClass"].DefaultView; DropDownList1.DataTextField = "NewsClassName"; DropDownList1.DataValueField = "NewsID"; DropDownList1.DataBind(); conn.Close(); } protected void Button2_Click(object sender, EventArgs e) { NewsContent.Text = ""; Title.Text = ""; } }