保存数据后如何更新gridview?

时间:2012-05-31 10:10:50

标签: c# asp.net mysql

我有一个包含4列的网格视图(UserId,描述,密码,更改密码[按钮])。

当我点击更改密码时,会出现带有3个文本框(用户ID,新密码,确认密码)和保存按钮的面板。

更改密码后面板消失,但gridview中的密码与之前的密码相同。

我想更新密码列。

以下是我的保存按钮单击
代码

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 Server ..请帮助..

3 个答案:

答案 0 :(得分:1)

在button_click事件中,将gridview绑定到新列表。

List<something> k = //your sql stuff
GridView1.DataSource = k;
GridView1.DataBind();

答案 1 :(得分:1)

阅读此tutorial。互联网上的ASP.NET初学者有很多教程。谷歌.. ..

已编辑:保存密码后,从数据库(而不是会话)加载datatable并再次将其绑定到gridview。

像那样

DataTable dt = new DataTable();
dt = //LoadFromDB();    // load data from database not session

gvPassInfo.DataSource = dt;
gvPassInfo.DataBind(); 

答案 2 :(得分:0)

更新事件/保存按钮点击事件结束时更新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;
}
}