【问题标题】:Clickable row in GridView and edit the rowGridView 中的可点击行并编辑该行
【发布时间】: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


【解决方案1】:

只需将控件添加到 Default.aspx 并在 default.aspx 页面加载事件(在非回发条件下)通过 ID 检索记录并填充控件。

现在,当按下提交按钮时,更新记录并重定向回您的原始页面

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多