【发布时间】:2011-10-09 23:21:15
【问题描述】:
我有一个关于可编辑网格视图的问题。我想要做的是通过使用单个可点击行来替换编辑按钮功能。当我单击一行时,它应该将我转发到一个新页面以编辑这些行数据。如何在不使用编辑按钮的情况下实现这一点?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// only apply changes if its DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#EEFF00'");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
//e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "CategoryID") + "'";
e.Row.Attributes.Add("style", "cursor:pointer;");
}
}
【问题讨论】:
-
我已经和我一起测试了你的代码,它按预期工作。我看不出任何问题。
-
@V4Vendetta 我的当前页面是 default2.aspx。问题是 Default.aspx 没有处于编辑模式,只是一个带有 gridview 数据的普通页面。无法编辑任何内容。
-
@Muhammad 我的 Default.aspx 无法编辑任何内容。它应该是可编辑的,对吧?
-
没有相关的代码吗?
-
@V4Vendetta 你在这里误会了。我想要的是当单击该行时,它会提示到 default.aspx 页面。这个页面应该处于编辑模式,就像我们点击编辑按钮一样。我选择的行数据应该可以在这里编辑。有什么建议,或者我应该使用 rowCommand 还是什么?
标签: c# asp.net gridview row edit