GridView更新数据库后 无法显示问题!
利用TextBox接受用户输入,按钮触发事件在数据库中搜索到一条数据后,在页面中用GridView显示,利用更新按钮修改后 可以更新回数据库,但是页面刷新后无法显示更新后的数据. 望高手赐教public partial class admin_Users_Update : System.Web.UI.Page
{
private string tableName = "users";//表名
private NameSearch ns = new NameSearch();//搜索信息类
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 点击按钮事件,出发搜索功能,将
/// 搜索结果绑定到GridView上
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
protected void Button1_Click(object sender, EventArgs e)
{
//搜索信息
this.GridView1.DataSource = ns.getData(this.TextBox1.Text, tableName).Tables[tableName];
this.DataBind();
}
/// <summary>
/// GridView按钮编辑事件
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.DataSource=ns.getData(this.TextBox1.Text,tableName).Tables[tableName];
this.GridView1.EditIndex = e.NewEditIndex;
this.DataBind();
}
/// <summary>
/// GridView按钮取消事件
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.DataSource = ns.getData(this.TextBox1.Text, tableName).Tables[tableName];
this.GridView1.EditIndex = -1;
this.DataBind();
}
/// <summary>
/// GridView更新事件,用于接受用户修改,更新数据库信息
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.GridView1.DataSource = ns.getData(this.TextBox1.Text, tableName).Tables[tableName];
int index = e.RowIndex;
//获得需要修改的行信息
int uid=Convert.ToInt32(this.GridView1.Rows[index].Cells[0].Text.ToString().Trim());
string uname = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString().Trim();
string upass = ((TextBox)this.GridView1.Rows[index].Cells[2].Controls[0]).Text.ToString().Trim();
string email = ((TextBox)this.GridView1.Rows[index].Cells[3].Controls[0]).Text.ToString().Trim();
if (isEmail(email))//验证email地址格式是否合法
{
Users u = new Users();//创建表对象
//给Users对象赋值
u.Uid = uid;
u.Uname = uname;
u.Upass = upass;
u.Email = email;
//更新信息
Update update = new Update();
if (update.ToUpdate(u, tableName))//update方法检验数据是否更新成功
{
string strScript = "window.location.href='Delete.aspx';";
strScript += "window.location.replace('Delete.aspx');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "updateOK", " <script language='javascript'>alert('更新成功');"+script+" </script>");
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "updateOK", " <script language='javascript'>alert('更新失败') </script>");
}
}
else
{
Response.Write(" <script language='javascript'>alert('请输入邮件地址的正确格式!如:xx@xy.com') </script>");
}
}
/// <summary>
/// isEmail方法验证用户修改的Email地址是否合法
/// </summary>
/// <param name="inputEmail"> </param>
/// <returns> 返回bool类型 </returns>
public bool isEmail(string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.) ¦(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4} ¦[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))//格式正确返回真
{
return true;
}
else//不正确返回假
{
return false;
}
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
} --------------------编程问答-------------------- 更新完以后还要在绑定一下数据源!! --------------------编程问答-------------------- 关注中! --------------------编程问答-------------------- 如果你数据库更新了,但是页面上没更新,那就是我上面所说的问题 --------------------编程问答-------------------- 要是因为绑定那么简单 我就不用发贴子问了
--------------------编程问答-------------------- 按钮事件里向数据库提交完数据后,将GridView的数据源从新绑定 --------------------编程问答-------------------- 晕,GridView1_RowUpdating里最后都没有再次绑定数据源,你页面怎么可能显示更新后的内容?
就那么简单。。。
--------------------编程问答-------------------- IsPostback() --------------------编程问答-------------------- --------------------编程问答-------------------- 我已经绑定过了!不好用 我才发帖子问的
只是刚贴出来的代码 没有那句话而已. --------------------编程问答-------------------- 那你把相关代码发来看看,要不就是Page_Load中绑定数据没有加isPostback,你要显示数据必须在Load里只能加载一次!代码看看? --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
/// <summary>
/// 绑定数据信息
/// </summary>
private void BindData()
{
}
protected void grv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
BindData();
} --------------------编程问答-------------------- 重新dataBind()下 晕 --------------------编程问答-------------------- protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
this.GridView1.DataSource = ns.getData(this.TextBox1.Text, tableName).Tables[tableName];
this.DataBind();
}
补充:.NET技术 , ASP.NET