469,602 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Remove Control from Gridview Row

AG
I have a gridview with a template column containing an imagebutton to delete
the row.
Under some condition I don't want the row to be deleted, so would like to
remove the button.

In the RowDataBound event, I can find the button, but can't seem to remove
it.
The code below does not throw any exception, but the button is not removed.

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvUsers.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If CriteriaIsMet Then
Dim ibtN As ImageButton =
CType(e.Row.Cells(1).FindControl("ibtnDelete"), ImageButton)
If ibtN IsNot Nothing Then
e.Row.Cells(1).Controls.Remove(ibtN)
End If
End If
End If
End Sub
How can I remove it?
Or must I settle for ibtN.Visible = False.

TIA.

--

AG
Email: discuss at adhdata dot com


Oct 16 '06 #1
6 10539
Yes, ibtN.Visible = False will be just fine.Actually, it will achieve your
goal. The button will be removed from the resulting html.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"AG" <NO**********@newsgroups.nospamwrote in message
news:ut**************@TK2MSFTNGP04.phx.gbl...
>I have a gridview with a template column containing an imagebutton to
delete the row.
Under some condition I don't want the row to be deleted, so would like to
remove the button.

In the RowDataBound event, I can find the button, but can't seem to remove
it.
The code below does not throw any exception, but the button is not
removed.

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
gvUsers.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If CriteriaIsMet Then
Dim ibtN As ImageButton =
CType(e.Row.Cells(1).FindControl("ibtnDelete"), ImageButton)
If ibtN IsNot Nothing Then
e.Row.Cells(1).Controls.Remove(ibtN)
End If
End If
End If
End Sub
How can I remove it?
Or must I settle for ibtN.Visible = False.

TIA.

--

AG
Email: discuss at adhdata dot com


Oct 16 '06 #2
Hi AG,

As for the sub controls in TemplateColumn(cell), you can remove it,
however, the removing operation should be done in GridView's RowCreated
event rather that RowDataBound event. This is because "RowDataBound" event
only fires when the GridView is performing databinding, this is not done in
every page request. For example, when another button/control on the page
postback the page, the GridView won't need to redo databinding, thus
"RowDataBound" event is not fired and your control removing in this event
will lose its effect in seqential postback.

On the contrary, the RowCreated event is called in every request, and in
the event, we can access the complete control collection of each
GridViewRow(defined at design-time). And if you put the control removing
code here, it will be reflected in every page request(no matter the
GridView is performing databinding or not...). e.g.

=====================

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowIndex
mod 3 )= 0 Then

Dim btn As ImageButton =
e.Row.Cells(2).FindControl("testButton") as ImageButton

If Not btn Is Nothing Then
e.Row.Cells(2).Controls.Remove(btn)
End If

End If
End Sub

=======================
Though the above one can work. IMO, I also recommend you use
ImageButton.Visible= False to archive the same goal since it'll be much
simpler and you can use that code in "RowDataBound" event(i assume the
"CriteriaIsMet" relate to databinding data) because Visible property will
be persited in Control(page)'s viewstate and will be remain between page
postback.

Hope this help further clarify this. If you have any other questions on
this, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Oct 17 '06 #3
AG
Steven,

It seems that there is no ideal solution.
The reason I was trying to remove the button is that hiding it only works if
it's viewstate is enabled. I like to try to mininize viewstate by turning it
off where not really needed. If viewstate is not enabled for the button and
the gridview is not bound on a postback, the button is visible again.

The problem with removing the button in the RowCreated event is knowing
which row to remove it in.
That would mean examining the content of each row to determine if it is the
one.
--

AG
Email: discuss at adhdata dot com

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:u2**************@TK2MSFTNGXA01.phx.gbl...
Hi AG,

As for the sub controls in TemplateColumn(cell), you can remove it,
however, the removing operation should be done in GridView's RowCreated
event rather that RowDataBound event. This is because "RowDataBound"
event
only fires when the GridView is performing databinding, this is not done
in
every page request. For example, when another button/control on the page
postback the page, the GridView won't need to redo databinding, thus
"RowDataBound" event is not fired and your control removing in this event
will lose its effect in seqential postback.

On the contrary, the RowCreated event is called in every request, and in
the event, we can access the complete control collection of each
GridViewRow(defined at design-time). And if you put the control removing
code here, it will be reflected in every page request(no matter the
GridView is performing databinding or not...). e.g.

=====================

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowIndex
mod 3 )= 0 Then

Dim btn As ImageButton =
e.Row.Cells(2).FindControl("testButton") as ImageButton

If Not btn Is Nothing Then
e.Row.Cells(2).Controls.Remove(btn)
End If

End If
End Sub

=======================
Though the above one can work. IMO, I also recommend you use
ImageButton.Visible= False to archive the same goal since it'll be much
simpler and you can use that code in "RowDataBound" event(i assume the
"CriteriaIsMet" relate to databinding data) because Visible property will
be persited in Control(page)'s viewstate and will be remain between page
postback.

Hope this help further clarify this. If you have any other questions on
this, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.


Oct 17 '06 #4
Thanks for your followup AG,

Yes, I agree that both approaches has restriction and drawback.

What's your current criteria for determine which row in the GridView that
need to remove the button? If the criteria data is not very complex, we can
store it in a hidden form element on the page so that we can access it in
RowCreated event rather than query it from inspecting GridView control
collection.

Anyway, if there is anything else we can help, please feel free to post
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 18 '06 #5
AG
Thanks Steven, I will play around with it. All I am really doing right now
is trying to get myself up to speed on 2.0. The books, tutorials, etc. don't
get into all these practical little details.

--

AG
Email: discuss at adhdata dot com

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:u9**************@TK2MSFTNGXA01.phx.gbl...
Thanks for your followup AG,

Yes, I agree that both approaches has restriction and drawback.

What's your current criteria for determine which row in the GridView that
need to remove the button? If the criteria data is not very complex, we
can
store it in a hidden form element on the page so that we can access it in
RowCreated event rather than query it from inspecting GridView control
collection.

Anyway, if there is anything else we can help, please feel free to post
here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Oct 18 '06 #6
Thanks for the followup AG,

Sure, glad to hear that you're on the way to go.

Also, for ASP.NET 2.0, you can find plenty of resources in the ASP.NET
official site:

http://www.asp.net/

As always, you're welcome to discuss any problem or question you met here.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Nalaka | last post: by
1 post views Thread by Tomas Oplt | last post: by
4 posts views Thread by Jim Katz | last post: by
reply views Thread by Yourself | last post: by
6 posts views Thread by CSharpguy | last post: by
1 post views Thread by =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.