如何添加一条记录后,直接在GridView中显示出来.
我在直接添加后,后重新绑定GridView,不知道为何,总是在下一次才显示出来.比如添记录1后不马上显示,在我添加2后才显示出记录1publicvoidDataBindT...
我在直接添加后,后重新绑定GridView,不知道为何,总是在下一次才显示出来.
比如添记录1后不马上显示,在我添加2后才显示出记录1
public void DataBindToGridView()
{
OleDbConnection ocon = DB.CreateCon();
ocon.Open();
int i = Convert.ToInt32(DropDownList1.SelectedValue);
string cmdtext = "select * from MainInfo where mid = " + i;
OleDbCommand ocmd = new OleDbCommand(cmdtext, ocon);
ocmd.ExecuteNonQuery();
OleDbDataReader odr = ocmd.ExecuteReader();
if (odr.Read())
{
string sql = odr["mcontent"].ToString();
odr.Close();
OleDbCommand cmd = new OleDbCommand(sql, ocon);
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
ocon.Close();
}
ButtonField bf = new ButtonField();
bf.Text = "修改";
bf.HeaderText = "Operate";
bf.CommandName = "UpdateCommand";
}
private void AddNews()
{
string simg;
bool valid;
if (this.fulNewsImage.PostedFile.FileName.Length == 0)
{
simg = "kong.jpg";
valid = true;
}
else
{
string iFullName = this.fulNewsImage.PostedFile.FileName;
simg = iFullName.Substring(iFullName.LastIndexOf("\\") + 1);
string imgtype = simg.Substring(simg.LastIndexOf(".")+1);
if (imgtype == "jpg" || imgtype == "bmp" || imgtype == "gif" || imgtype == "png")
{
fulNewsImage.SaveAs(Server.MapPath("images/" + simg));
fulNewsImage.BackColor = Color.Green;
valid = true;
}
else
{
Response.Write("<script>alert('非法图片格式')</script>");
fulNewsImage.BackColor = Color.Red;
valid = false;
}
}
if (valid==true)
{
OleDbConnection con = DB.CreateCon();
con.Open();
string sql = "insert into News(n_title,n_content,n_img,n_addtime) values ( @title,@content,@img,'" + DateTime.Now + "')";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.Add("@title", OleDbType.VarChar).Value = this.tbNewsTitle.Text;
cmd.Parameters.Add("@content", OleDbType.VarChar).Value = this.tbNewsContent.Text;
cmd.Parameters.Add("@img", OleDbType.VarChar).Value = simg.ToString();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('添加新闻成功!')</script>");
}
}
else
{
return;
}
}
protected void btnLook_Click(object sender, EventArgs e)
{
DataBindToGridView();
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
AddNews();
DataBindToGridView();
}
怎么清空 展开
比如添记录1后不马上显示,在我添加2后才显示出记录1
public void DataBindToGridView()
{
OleDbConnection ocon = DB.CreateCon();
ocon.Open();
int i = Convert.ToInt32(DropDownList1.SelectedValue);
string cmdtext = "select * from MainInfo where mid = " + i;
OleDbCommand ocmd = new OleDbCommand(cmdtext, ocon);
ocmd.ExecuteNonQuery();
OleDbDataReader odr = ocmd.ExecuteReader();
if (odr.Read())
{
string sql = odr["mcontent"].ToString();
odr.Close();
OleDbCommand cmd = new OleDbCommand(sql, ocon);
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
ocon.Close();
}
ButtonField bf = new ButtonField();
bf.Text = "修改";
bf.HeaderText = "Operate";
bf.CommandName = "UpdateCommand";
}
private void AddNews()
{
string simg;
bool valid;
if (this.fulNewsImage.PostedFile.FileName.Length == 0)
{
simg = "kong.jpg";
valid = true;
}
else
{
string iFullName = this.fulNewsImage.PostedFile.FileName;
simg = iFullName.Substring(iFullName.LastIndexOf("\\") + 1);
string imgtype = simg.Substring(simg.LastIndexOf(".")+1);
if (imgtype == "jpg" || imgtype == "bmp" || imgtype == "gif" || imgtype == "png")
{
fulNewsImage.SaveAs(Server.MapPath("images/" + simg));
fulNewsImage.BackColor = Color.Green;
valid = true;
}
else
{
Response.Write("<script>alert('非法图片格式')</script>");
fulNewsImage.BackColor = Color.Red;
valid = false;
}
}
if (valid==true)
{
OleDbConnection con = DB.CreateCon();
con.Open();
string sql = "insert into News(n_title,n_content,n_img,n_addtime) values ( @title,@content,@img,'" + DateTime.Now + "')";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.Parameters.Add("@title", OleDbType.VarChar).Value = this.tbNewsTitle.Text;
cmd.Parameters.Add("@content", OleDbType.VarChar).Value = this.tbNewsContent.Text;
cmd.Parameters.Add("@img", OleDbType.VarChar).Value = simg.ToString();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('添加新闻成功!')</script>");
}
}
else
{
return;
}
}
protected void btnLook_Click(object sender, EventArgs e)
{
DataBindToGridView();
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
AddNews();
DataBindToGridView();
}
怎么清空 展开
4个回答
展开全部
int i = Convert.ToInt32(DropDownList1.SelectedValue);
string cmdtext = "select * from MainInfo where mid = " + i;
你是要获取下拉框的什么值吗?建议你重新写一下select数据库的语句试试
string cmdtext = "select * from MainInfo where mid = " + i;
你是要获取下拉框的什么值吗?建议你重新写一下select数据库的语句试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
清空一下 然后重新执行SELECT
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在page_load()
{
if (!IsPostBack)
{
}
}
{
if (!IsPostBack)
{
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询