我有一个gridview rowCommand,它会在触发时获取行的索引。然后我需要检索索引的所有值,以包含在我的按钮的update语句中的where claws中。我使用多个会话来尝试,但它不起作用。这是我的代码。
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();
}
答案 0 :(得分:1)
我不确定哪些不适合你,但这肯定不起作用:
dremployer2.SelectedItem.Text = gvawards.Rows[index].Cells[1].Text;
您只分配文本,所选值仍旧。你应该这样做:
dremployer2.SelectedIndex = dremployer2.Items.IndexOf(dremployer2.Items.FindByText( gvawards.Rows[index].Cells[1].Text));
因此,当您在会话中保存正确的选定值时:
Session["Employer2"] = dremployer2.SelectedValue;