繁体   English   中英

每当用户更新gridview时,如何获取用户名会话并将其用户名更新到数据库?

[英]how to get session of username and update his username to the database whenever the user updates the gridview?

我有此表称为EntryTable。 我使用gridview显示了数据。 但是在此之前,用户需要先登录才能使用该服务。 我将用户名硬编码为Admin。 在登录后,在编辑后,单击更新按钮后,他的名字Admin将转到数据库中的MODIFIEDBY列字段。

不要被名称列弄糊涂,与此无关。 仅Modifyed列很重要。 由于我以Admin身份登录,因此我的名字叫admin。 如果我编辑并更新了gridview,则名为admin的名称将转到数据库中的Modifyed列。 因为管理员修改了它。 如果我在gridview中更新第一行,则名称admin会进入Modifyedby column字段,而不是John Tan。 如果我在gridview中更新第二行,则名称admin将进入Modifyedby column字段,而不是kevin wong。 始终为管理员,因为我以管理员身份登录。 这意味着通过会话名称的值更新修改的列。

在此处输入图片说明

在此处输入图片说明

登录名是一个标签,ID称为lblUsername。 在此处输入图片说明

登录ASPX代码

protected void btnLogin_Click(object sender, EventArgs e)
{

    if (txtUserName.Text == "Admin" && txtPassword.Text == "123")
    {
        Session.Add("Username", txtUserName.Text);
        Session.Add("Password", txtPassword.Text);
        FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
        Response.Redirect("BlogEntry.aspx");
    }

    else

        lblError.Text = "Incorrect Username or Password";
}

登录页面,更新和页面加载代码之后。

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        bindResultGridView();

    }
    //Logout.Visible = false;
    string memName = (String)Session["UserName"];
    lblUsername.Text = String.Concat("Welcome Guest!");

    if (Session["Username"] != null && Session["Username"] != String.Empty)
    {
        lblUsername.Text = "Welcome, " + memName + "!";


    }

}


protected void grdBlog_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdBlog.EditIndex = e.NewEditIndex;
        bindResultGridView();
    }
    protected void grdBlog_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int selectedRow = e.RowIndex;   //get selected row
        //  get product id from data key
        int blogid = (int)grdBlog.DataKeys[selectedRow].Value;

    //  get current grid view row
    GridViewRow row = (GridViewRow)grdBlog.Rows[selectedRow];
    TextBox name = (TextBox)row.FindControl("txtName");
    //  find text box for txtPrice
    TextBox blogtype = (TextBox)row.FindControl("txtBlogType");
    TextBox description = (TextBox)row.FindControl("txtDescription");
    TextBox dateentry = (TextBox)row.FindControl("txtDateEntry");
    TextBox blogstory = (TextBox)row.FindControl("txtBlogStory");
    //  Remove $ sign
    string strName = name.Text;
    string strBlogType = blogtype.Text;
    string strDescription = description.Text;
    string strDateEntry = dateentry.Text;
    string strBlogStory = blogstory.Text;
    DateTime datDate;
    if (DateTime.TryParseExact(strDateEntry, new string[] { "dd/MM/yyyy" },
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None, out datDate))
    {
        updateBlogGridviewRecord(blogid, strName, strBlogType, strDescription, datDate, strBlogStory);
    }

    else
    {
        lblError.Visible = true;
        lblError.Text = "Invalid Date";
        lblSuccess.Visible = false;
    }
}

private void updateBlogGridviewRecord(int blogid, string strName, string strBlogType, string strDescription, DateTime datDate, string strBlogStory)
{
    try
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "UPDATE EntryTable SET [Name]=@Name, [BlogType]=@BlogType, [Description]=@Description, [DateEntry]=@DateEntry, [BlogStory]=@BlogStory WHERE [BlogID]=@BlogID";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
        cmd.Parameters.AddWithValue("@BlogID", blogid);
        cmd.Parameters.AddWithValue("@Name", strName);
        cmd.Parameters.AddWithValue("@BlogType", strBlogType);
        cmd.Parameters.AddWithValue("@DateEntry", datDate);
        cmd.Parameters.AddWithValue("@Description", strDescription);
        cmd.Parameters.AddWithValue("@BlogStory", strBlogStory);
        myConnect.Open();

        int result = cmd.ExecuteNonQuery();

        if (result > 0)
        {
            lblSuccess.Visible = true;
            lblSuccess.Text = "Record updated!";
            lblError.Visible = false;
        }
        else
        {
            lblSuccess.Visible = true;
            lblError.Text = "Update fail";
            lblError.Visible = false;
        }

        myConnect.Close();


        //Cancel Edit Mode
        grdBlog.EditIndex = -1;
        bindResultGridView();
    }

    catch
    {
        lblError.Visible = true;
        lblError.Text = "Please Enter Approximate data";
        lblSuccess.Visible = false;
    }
}

遵循@puneet的建议

 protected void grdBlog_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    Session["Username"] = txtName.Text;

    int selectedRow = e.RowIndex;   //get selected row
    //  get product id from data key
    int blogid = (int)grdBlog.DataKeys[selectedRow].Value;

    //  get current grid view row
    GridViewRow row = (GridViewRow)grdBlog.Rows[selectedRow];
    TextBox name = (TextBox)row.FindControl("txtName");
    //  find text box for txtPrice
    TextBox blogtype = (TextBox)row.FindControl("txtBlogType");
    TextBox description = (TextBox)row.FindControl("txtDescription");
    TextBox dateentry = (TextBox)row.FindControl("txtDateEntry");
    TextBox blogstory = (TextBox)row.FindControl("txtBlogStory");
    //  Remove $ sign
    string strName = name.Text;
    string strBlogType = blogtype.Text;
    string strDescription = description.Text;
    string strDateEntry = dateentry.Text;
    string strBlogStory = blogstory.Text;
    DateTime datDate;
    if (DateTime.TryParseExact(strDateEntry, new string[] { "dd/MM/yyyy" },
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None, out datDate))
    {
        updateBlogGridviewRecord(blogid, strName, strBlogType, strDescription, datDate, strBlogStory);
    }

    else
    {
        lblError.Visible = true;
        lblError.Text = "Invalid Date";
        lblSuccess.Visible = false;
    }
}

以下内容未经测试,但在我看来,您可以按照以下内容进行操作。

string strCommandText = "UPDATE EntryTable SET [ModifiedBy]=@Modifier, [Name]=@Name, [BlogType]=@BlogType, [Description]=@Description, [DateEntry]=@DateEntry, [BlogStory]=@BlogStory WHERE [BlogID]=@BlogID";

SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@BlogID", blogid);
cmd.Parameters.AddWithValue("@Name", strName);
cmd.Parameters.AddWithValue("@BlogType", strBlogType);
cmd.Parameters.AddWithValue("@DateEntry", datDate);
cmd.Parameters.AddWithValue("@Description", strDescription);
cmd.Parameters.AddWithValue("@BlogStory", strBlogStory);
cmd.Parameters.AddWithValue("@Modifier", Session["Username"]);

在您的grdBlog_RowUpdating方法中,通过用户输入的文本更改会话值。

Session["Username"] = name.Text;

下次页面刷新时,将出现新名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM