GridView编辑时如何避免RowDataBound?

发布于 2024-12-04 10:33:06 字数 494 浏览 1 评论 0原文

目前,我在 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);
        }
}

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

Currently, I have the following code in the 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);
        }
}

However, when I click on the Edit link, it tries to run that code and throws an error. Therefore, how can I run that code ONLY when the GridView is in read mode? But not when editing...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清晨说晚安 2024-12-11 10:33:06

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

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

Here is how to do it! It will only execute the code over the rows (when reading or editing mode) except for the row that is being edited!!!

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);
            }
        }
    }
脱离于你 2024-12-11 10:33:06

您可以添加这样的检查:

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

you can add a check like this:

if (e.Row.RowState != DataControlRowState.Edit)
{
  // Here logic to apply only on initial DataBinding...
}
老娘不死你永远是小三 2024-12-11 10:33:06

添加对 e.Row.RowState 的检查:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
    //In Edit mode
}

Add a check for e.Row.RowState:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
    //In Edit mode
}
眼泪淡了忧伤 2024-12-11 10:33:06

Davide 的答案几乎是正确的。但是对于替代行它会失败。这是正确的解决方案:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit && e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate))
{ 
    // Here logic to apply only on rows not in edit mode
}

Davide's answer is almost correct.. However it will fail for alternate rows. Here is the correct solution:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit && e.Row.RowState != (DataControlRowState.Edit | DataControlRowState.Alternate))
{ 
    // Here logic to apply only on rows not in edit mode
}
贱人配狗天长地久 2024-12-11 10:33:06

在你的网格视图中,
搜索 OnrowDataBound 事件,该事件将像 OnrowDataBound="GridView1_RowDataBound" 删除该代码并禁用上述代码。

In your gridview,
search for OnrowDataBound event which will like OnrowDataBound="GridView1_RowDataBound" remove that code and disable the above code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文