如何在保存数据后更新网格视图
本文关键字:更新 网格 视图 数据 保存 | 更新日期: 2023-09-27 18:35:47
我有一个网格视图,有 4 列(用户 ID、描述、密码、更改密码[按钮])。
当我单击更改密码时,会出现带有3个文本框(UserID,新密码,确认密码)和保存按钮的面板。
更改密码后,面板消失,但网格视图中的密码与以前相同。
我想更新密码列。
以下是我的保存按钮点击
法典
protected void BindGridView()
{
try
{
DataTable dt = new DataTable();
dt = (DataTable)Session["userinfo"];
gvPassInfo.DataSource = dt;
gvPassInfo.DataBind();
}
catch (Exception ex)
{
//lblMessage.Text = DataObjects.Error_Message();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
clsUser objuser = new clsUser();
string user = txtUserid.Text;
string NewPassword = txtNewPassword.Text;
string ConfirmPassword = txtConfirmNewPassword.Text;
objuser.UpdateSystemPassword(user, NewPassword);
Response.Write("<script LANGUAGE='JavaScript' >alert('Password Changed Successfully...'); document.location='" +ResolveClientUrl("~''PasswordInformation_Details.aspx") + "'; </script>");
BindGridView();
panelChangePassword.Visible = false;
}
protected void btnSearch1_Click(object sender, EventArgs e)
{
try
{
using (MySqlConnection conn = new MySqlConnection(clsUser.connStr))
{
conn.Open();
string strQuery = "select DISTINCT user_id,description,sap_system_password from sap_password_info where user_id is not null";
if (txtSid.Text !="")
{
strQuery += " AND sid = '" + txtSid.Text + "'";
}
if (txtClient.Text != "")
{
strQuery += " AND client_no = '" + txtClient.Text + "'";
}
if (txtUser.Text != "")
{
strQuery += " AND user_id = '" + txtUser.Text + "'";
}
MySqlCommand cmd = new MySqlCommand(strQuery, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
Session["userinfo"] = dt;
Response.Redirect("~''PasswordInformation_Details.aspx");
}
}
catch (Exception ex)
{
//lblMessage.Text = DataObjects.Error_Message();
lblMsg.Text = ex.Message.ToString();
}
}
代码是C#,后端是MySQL DB服务器..请帮助..
在button_click事件中,将网格视图绑定到新列表。
List<something> k = //your sql stuff
GridView1.DataSource = k;
GridView1.DataBind();
阅读本教程。互联网上有很多针对 ASP.NET 初学者的教程。谷歌它..
已编辑:保存密码后,从数据库(而不是会话)加载datatable
并将其再次绑定到网格视图。
诸如此类
DataTable dt = new DataTable();
dt = //LoadFromDB(); // load data from database not session
gvPassInfo.DataSource = dt;
gvPassInfo.DataBind();
更新事件/保存按钮单击事件结束时的GridView
编辑
您正在更新数据库中的记录,但没有获取新数据,这就是您获取旧信息的原因
因此,在以下语句从数据库中获取数据并使用新数据更新会话数据集之后,您的问题将解决...
objuser.UpdateSystemPassword(user, NewPassword);
//get the updated data from the database after this statement
//don't forget to update the session with new data
在旧页面中编写此代码
Dictionary<string,string> infor = new Dictionary<string,string>();
infor["sid"] = txtSid.Text;
infor["client_no"] = txtClient.Text;
..
...
Session["queryInfo"] = infor;
在新文件中
GetDataFromDB()
{
Dictionary<string,string> infor = (Dictionary<string,string>)Session["queryInfo"];
try
{
using (MySqlConnection conn = new MySqlConnection(clsUser.connStr))
{
conn.Open();
string strQuery = "select DISTINCT user_id,description,sap_system_password from sap_password_info where user_id is not null";
if (infor["sid"] !="")
{
strQuery += " AND sid = '" + infor["sid"] + "'";
}
//... do like above for remaining if conditions
MySqlCommand cmd = new MySqlCommand(strQuery, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
Session["userinfo"] = dt;
//Response.Redirect("~''PasswordInformation_Details.aspx");
}
}
catch (Exception ex)
{
throw;
}
}