GridView中的可单击行并编辑该行

时间:2011-07-21 06:34:56

标签: c# asp.net gridview row edit

我在这里遇到有关可编辑gridview的问题。我想要做的是使用单个可点击行替换编辑按钮功能。当我单击一行时,应该将我转发到新页面以编辑这些行数据。如何在不使用编辑按钮的情况下实现这一目标?

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;");

    }

}

1 个答案:

答案 0 :(得分:0)

只需根据需要添加控件Default.aspx和default.aspx pageload事件(在非回发条件下)通过ID检索记录并填充控件。

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