Remove Control from Gridview Row

A

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.
 
E

Eliyahu Goldin

Yes, ibtN.Visible = False will be just fine.Actually, it will achieve your
goal. The button will be removed from the resulting html.
 
S

Steven Cheng[MSFT]

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.
 
A

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.
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

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.
 
S

Steven Cheng[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top