1

I have a gridview rowCommand that will get the index of the row when fired. Then I need to retrieve all values of the index to be included in the where claws on the update statement of my button. I use multiple sessions to try that, but it's not working. Here's my code.

    protected void gvawards_RowCommand(object sender, GridViewCommandEventArgs e)
    {
                    btnsaveawards.Visible = true;
                    if (e.CommandName == "Name")
                    {
                        int index = Convert.ToInt32(e.CommandArgument);
                        dremployer2.SelectedItem.Text = gvawards.Rows[index].Cells[1].Text;
                        txtawardtitle.Text = gvawards.Rows[index].Cells[2].Text;
                        txtyearawarded.Text = gvawards.Rows[index].Cells[3].Text;
                        Session["Employer2"] = dremployer2.SelectedValue;
                        Session["Award"] = txtawardtitle.Text;
                        Session["YearAwarded"] = txtyearawarded.Text;

                    }
    }

    protected void btnsaveawards_Click(object sender, EventArgs e)
    {
        int employer2 = int.Parse(Session["Employer2"].ToString());
        string awardtitle = Session["Award"].ToString();
        int yearawarded = int.Parse(Session["YearAwarded"].ToString());
        btnsaveawards.Visible = false;
        con.Open();
        string update = "update tblAwardsData set EmployerID='" + dremployer2.SelectedValue + "',AwardTitle='" + txtawardtitle.Text + "',YearAwarded='" + txtyearawarded.Text + "' where  EmployerID='" + employer2 + "' and AwardTitle='" + awardtitle + "' and YearAwarded='" + yearawarded + "' and IDNumber='" + 11277718 + "'";
        SqlCommand scmupdate = new SqlCommand(update, con);
        scmupdate.ExecuteNonQuery();
        view(); //Rebind the gridview
        dremployer2.SelectedItem.Text = "Select employer";
        txtawardtitle.Text = "";
        txtyearawarded.Text = "".ToString(); ;
        con.Close();
    }
4

1 回答 1

1

I am not sure what is not working for you, but this will definitely not work:

 dremployer2.SelectedItem.Text = gvawards.Rows[index].Cells[1].Text;

You are assigning text only, selected value remains old. You should do this way:

dremployer2.SelectedIndex = dremployer2.Items.IndexOf(dremployer2.Items.FindByText(  gvawards.Rows[index].Cells[1].Text));

So when you save the correct selected value in the Session:

Session["Employer2"] = dremployer2.SelectedValue;
于 2013-09-01T08:37:33.680 回答