我使用gridview点击一个单元格。一旦我点击单元格,我就会打开一个模态框。
模态有一个更新和取消按钮 我想在后面的代码中获取单击的单元格中选定的行和列索引,以确定单击的单元格是否具有文本值。
我可以在我这里的代码中执行此操作:
protected void Grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
int selectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
int selectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
}
这就是我所拥有的,我在帮助评论中需要代码:
protected void btnUpdate_Click(object sender, EventArgs e)
{
int selectedRowIndex = //need help
int selectedColumnIndex = // need help
if (Grd.Rows[selectedRowIndex].Cells[selectedColumnIndex].Text == " ")
{
//new, insert into db
}
else
{
//update existing db record
}
}
答案 0 :(得分:1)
您可以使用ViewState
在回发之间传递选定的行索引和选定的列索引。将以下属性添加到
private int SelectedRowIndex
{
get
{
if (ViewState["SelectedRowIndex"] == null)
{
return 0;
}
else
{
return (int)ViewState["SelectedRowIndex"];
}
}
set
{
ViewState["SelectedRowIndex"] = value;
}
}
private int SelectedColumnIndex
{
get
{
if (ViewState["SelectedColumnIndex"] == null)
{
return 0;
}
else
{
return (int)ViewState["SelectedColumnIndex"];
}
}
set
{
ViewState["SelectedColumnIndex"] = value;
}
}
然后将选定的行索引和选定的列索引分配给Grd_RowCommand
protected void Grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
this.SelectedRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
this.SelectedColumnIndex = Convert.ToInt32(Request.Form["__EVENTARGUMENT"].ToString());
}
并获取btnUpdate_Click
protected void btnUpdate_Click(object sender, EventArgs e)
{
int selectedRowIndex = this.SelectedRowIndex;
int selectedColumnIndex = this.SelectedColumnIndex;
if (Grd.Rows[selectedRowIndex].Cells[selectedColumnIndex].Text == " ")
{
//new, insert into db
}
else
{
//update existing db record
}
}