473,788 Members | 2,820 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 11136
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
3041
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
1810
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
13496
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
1040
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
1658
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
3475
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
3507
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
1320
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
1241
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
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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
6750
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
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
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.