编辑GridView时如何避免RowDataBound?

ale*_*nso 4 c# asp.net gridview rowdatabound

目前,我在RowDataBound中有以下代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label groupID = (Label)e.Row.FindControl("idgroup");
            LinkButton myLink = (LinkButton)e.Row.FindControl("groupLink");
            myLink.Attributes.Add("rel", groupID.Text);
        }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我单击"编辑"链接时,它会尝试运行该代码并引发错误.因此,如何在GridView处于读取模式时运行该代码?但不是在编辑时......

ale*_*nso 7

这是怎么做的!除了正在编辑的行外,它只会在行上执行代码(在读取或编辑模式时)!

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == DataControlRowState.Alternate))
            {
                Label groupID = (Label)e.Row.FindControl("idgroup");
                LinkButton myLink = (LinkButton)e.Row.FindControl("groupLink");
                myLink.Attributes.Add("rel", groupID.Text);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Dav*_*ras 6

你可以添加这样的支票:

if (e.Row.RowState != DataControlRowState.Edit)
{
  // Here logic to apply only on initial DataBinding...
}
Run Code Online (Sandbox Code Playgroud)