这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class upd_article : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null || Session["password"] == null)
{
Response.Redirect("login.aspx");
}
if (!IsPostBack)
{
datashows();
}
}
protected void datashows()
{
conn.Open();
String strsql = "select news_bigclass.bigclassid,bigclassname,newid,title,newsfrom,newstime,writer,hit from news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid";
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "wenzhan");
GridView1.DataSource = ds.Tables["wenzhan"].DefaultView;
GridView1.DataBind();
conn.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
datashows();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
datashows();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
conn.Open();
string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox tb1, tb2, tb3,tb4,tb5,tb6;
tb1 = (TextBox)(row.Cells[0].Controls[0]);
tb2 = (TextBox)(row.Cells[1].Controls[0]);
tb3 = (TextBox)(row.Cells[2].Controls[0]);
tb4 = (TextBox)(row.Cells[3].Controls[0]);
tb5 = (TextBox)(row.Cells[4].Controls[0]);
tb6 = (TextBox)(row.Cells[5].Controls[0]);
string strsql = "update news_article inner join news_bigclass on news_article.bigclassid=news_bigclass.bigclassid set title='" + tb1.Text + "',bigclassname=" + Convert.ToInt32(tb2.Text) + ",writer='" + tb3.Text + "',newsfrom='" + tb4.Text + "',hit='" + tb5.Text + "',newstime='" +Convert .ToDateTime (tb6 )+ "' where news_article.newid=" + uid;
SqlCommand comm = new SqlCommand(strsql, conn);
comm.ExecuteNonQuery();
conn.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
conn.Open();
string uid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string strSql = "delete from news_article where newid=" + uid;
SqlCommand comm = new SqlCommand(strSql, conn);
comm.ExecuteNonQuery();
conn.Close();
datashows();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1 .PageIndex = e.NewPageIndex;
GridView1.DataBind();
TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
tb.Text = (GridView1.PageIndex + 1).ToString();
//GridView1.PageIndex = e.NewPageIndex;
//datashows();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
try
{
TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
int num = Int32.Parse(tb.Text);
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
GridView1_PageIndexChanging(null, ea);
}
catch { }
}
}
}
我在数据库中设置的日期格式是datetime类型
下面是图片