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);
}
}
但是,当我单击“编辑”链接时,它会尝试运行该代码并引发错误。因此,如何仅在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是如何做的!它只会在行上执行代码(在读取或编辑模式时),除了正在编辑的行!
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!!!
您可以添加这样的检查:
you can add a check like this:
添加对
e.Row.RowState
的检查:Add a check for
e.Row.RowState
:Davide 的答案几乎是正确的。但是对于替代行它会失败。这是正确的解决方案:
Davide's answer is almost correct.. However it will fail for alternate rows. Here is the correct solution:
在你的网格视图中,
搜索 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.