0

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

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

更改密码后,面板消失,但网格视图中的密码与以前相同。

我想更新密码列。

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

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

4

3 回答 3

1

在 button_click 事件中,将您的 gridview 绑定到一个新列表。

List<something> k = //your sql stuff
GridView1.DataSource = k;
GridView1.DataBind();
于 2012-05-31T10:14:02.570 回答
1

Read this tutorial. And there are so many tutorials for the beginners of ASP.NET on internet. Google it..

Edited: After save of password ,load your datatable from database(not from session) and bind it again to your gridview.

like that

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

gvPassInfo.DataSource = dt;
gvPassInfo.DataBind(); 
于 2012-05-31T10:29:12.910 回答
0

在更新事件/保存按钮单击事件GridView结束时更新

编辑

you are updating the record in the database but you are not fetching new data that's the reason you are getting old information

so after following statement fetch the data from database and update the session dataset with new data then your problem will solve...

 objuser.UpdateSystemPassword(user, NewPassword);
 //get the updated data from the database after this statement
 //don't forget to update the session with new data

Write this code in old page

Dictionary<string,string> infor = new Dictionary<string,string>();
infor["sid"] = txtSid.Text;
infor["client_no"] = txtClient.Text;
..
...
Session["queryInfo"] = infor;

In New File

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;
}
}
于 2012-05-31T10:16:24.457 回答