473,704 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_RowDataBoun d(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles gvUsers.RowData Bound
If e.Row.RowType = DataControlRowT ype.DataRow Then
If CriteriaIsMet Then
Dim ibtN As ImageButton =
CType(e.Row.Cel ls(1).FindContr ol("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 11127
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**********@n ewsgroups.nospa mwrote in message
news:ut******** ******@TK2MSFTN GP04.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_RowDataBoun d(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewRowEventArg s) Handles
gvUsers.RowData Bound
If e.Row.RowType = DataControlRowT ype.DataRow Then
If CriteriaIsMet Then
Dim ibtN As ImageButton =
CType(e.Row.Cel ls(1).FindContr ol("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 "RowDataBou nd" 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
"RowDataBou nd" 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(def ined 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_RowCr eated(ByVal sender As Object, ByVal e As
GridViewRowEven tArgs)
If e.Row.RowType = DataControlRowT ype.DataRow And (e.Row.RowIndex
mod 3 )= 0 Then

Dim btn As ImageButton =
e.Row.Cells(2). FindControl("te stButton") 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.Vis ible= False to archive the same goal since it'll be much
simpler and you can use that code in "RowDataBou nd" event(i assume the
"CriteriaIs Met" 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.comw rote in message
news:u2******** ******@TK2MSFTN GXA01.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 "RowDataBou nd"
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
"RowDataBou nd" 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(def ined 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_RowCr eated(ByVal sender As Object, ByVal e As
GridViewRowEven tArgs)
If e.Row.RowType = DataControlRowT ype.DataRow And (e.Row.RowIndex
mod 3 )= 0 Then

Dim btn As ImageButton =
e.Row.Cells(2). FindControl("te stButton") 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.Vis ible= False to archive the same goal since it'll be much
simpler and you can use that code in "RowDataBou nd" event(i assume the
"CriteriaIs Met" 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.comw rote in message
news:u9******** ******@TK2MSFTN GXA01.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3035
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the current_row_key of Grid1... to the objectDataSource2 parameter? (so that the second grid, gets only the information to do with current row of grid1)
1
1805
by: Tomas Oplt | last post by:
Visual Studio2002 Framework 1.0 I have userControl which contain some controls If user remove at runtime remove control from the userControl especially ComboBox, it is not possible to close the form ! Form does not react on clicking on the right upper corner. Is there some solution for this to avoid this behavior. I tryed to remove focus from the control before removing but it helps only is some cases.
4
13486
by: Jim Katz | last post by:
I have an application that updates a strongly typed data set at run time. I'd like to dynamically create a table that connects to a run time data table. For displaying the data, this works well. I just set the GridView.DataSource once, and call DataBind(); I'd like to drive the application from the GridView control, by including command buttons that allow editing of the data. Starting out simple, I have a DataTable with a boolean...
0
1037
by: Yourself | last post by:
I have the following .aspx code: <%@ Page Language="vb" AutoEventWireup="false" Explicit="True" Inherits="Whatever.DefaultPage" CodeFile="Default.aspx.vb" %> <%@ Register TagPrefix="whatever" TagName="something" Src="~/Whatever/Whatever.ascx" %> <asp:placeholder id="fred" runat="server"></asp:placeholder> <whatever:something id="something" runat="server" /> the Default.aspx.vb code adds some controls to the 'fred' placeholder in the
0
1654
by: John Smith | last post by:
If anyone can help, I would very muchly appreciate it. I have a main page that uses the .net 2.0 menu control with the multiview controls as the menu choices. This works fine. One of the menu choices dynamically loads a user control with a gridview (datagrid) control on it. That works fine. The problem arises when the user clicks on a different page of the gridview control---this is a paged gridview. When that happens, the gridview...
6
3468
by: CSharpguy | last post by:
In my gridview I have 2 -3 template fields which are hyperlinks. I allow the user to print this grid. When the grid prints it also prints the links, how can I take the user to a print preview page and not show the templated fields, (hyperlinks) and also not print those fields?
1
3504
by: =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= | last post by:
Can anyone tell me how to programmatically remove a control from a tab page from VB .Net? The control was added using: Me.TabPage1.Controls.Add(pb) but I don't know what its index number is to remove it using: Me.TabPage1.Controls.RemoveAt(index) and Me.TabPage1.Controls.Remove(pb) doesn't work.
0
1318
by: ganeshvkl | last post by:
hi my requirement is create control arrays at run time that i achived but i couldn't remove control array object at run time visual Basic 6.0 i attached this code Private Sub Command1_Click() Dim i As Integer For i = 1 To 3 Load Text1(i) Set Text1(i).Container = SSTab1 Text1(i).Visible = True
0
1235
by: ramuksasi | last post by:
Hi, I've an asp.net .ascx form and a gridview on that form.I've placed that form on an .aspx. Now while I'm trying to import data from gridview to excel it is showing the error "Control 'GridView' must be placed inside a form tag with runat=server" see my code: <form action="readdata.aspx" runat="server"> <table> <tr> <td style="width: 100px"> <asp:Label ID="lblSearchBy"...
0
8767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9026
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5931
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4701
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2483
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.