This is a migrated thread and some comments may be shown as answers.

Trying to hide ImageButton

9 Answers 231 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Dwayne Starkey
Top achievements
Rank 1
Dwayne Starkey asked on 05 Jan 2010, 05:39 PM
I am trying to hide the print button if the row meets a certain ID. I have this code below in my loop and I cannot seem to get this to work. It keeps giving me an error stating the Object is not set to an instance of an object. 
What is the correct way of getting the control and hiding it within the Rad listview?

 Hope someone can help thanks.

If

 

GetCartItems.Rows(i).Item("ID") = "DZ" Then

 

 

 

Dim PH As PlaceHolder = CType(RadListCart.FindControl("PlaceHolder1"), PlaceHolder)

 

 

 

Dim Imageb As ImageButton = CType(PH.FindControl("PrintButton"), ImageButton)

 

Imageb.Visible =

 

True

 

 

 

End If

 

 

9 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 05 Jan 2010, 06:04 PM
Hello Dwayne,

If the place holder resides in ItemTemplate/AlternatingItemTemplate of RadListView, I think that you should modify the following line:

Dim PH As PlaceHolder = CType(RadListCart.FindControl("PlaceHolder1"), PlaceHolder)

as shown below:

Dim PH As PlaceHolder = CType(RadListCart.Items(i).FindControl("PlaceHolder1"), PlaceHolder)

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dwayne Starkey
Top achievements
Rank 1
answered on 05 Jan 2010, 06:12 PM
Thanks for the feedback...

My placeholder resides in th 

LayoutTemplate

I am now getting index was out of range on Dim PH As PlaceHolder = CType(RadListCart.Items(i).FindControl("PlaceHolder1"), PlaceHolder)
I am also trying ot perform this within the Need Datasource and not ItemDatbound, Is this an issue?
 Should the placeholder be in the ItemTemplate? Is there another way to get this to work?
0
Sebastian
Telerik team
answered on 06 Jan 2010, 09:24 AM
Hello Dwayne,

Thank you for the clarification - in case your place holder resides inside the LayoutTemplate, your initial code implementation should be correct. Unfortunately I cannot say for certain what might be the exact cause of the 'Object reference not set to an instance of an object' exception.

Can you please post the markup of the ListView control in this thread (using the 'Format Code Block' dialog) and the code-behind logic for further review? Also note that in case the asp PlaceHolder resides in another container in the listview's LayoutTemplate, you will need to invoke the FindControl(id) method for the container first and then its FindControl method for the place holder.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dwayne Starkey
Top achievements
Rank 1
answered on 06 Jan 2010, 12:01 PM
Form what you are saying is I need to find :
ID="HierarchyPanel" ?
Then find
Placeholder and then find
Imagebutton.....

The code is below: Thanks

 

('<

 

telerik:RadListView ID="RadListCart" runat="server" ItemPlaceholderID="PlaceHolder1" DataKeyNames="cID">

 

 

 

<LayoutTemplate>

 

 

 

<fieldset id="RadListCart">

 

 

 

<legend>Order Details</legend>

 

 

 

<asp:Panel ID="HierarchyPanel" runat="server" CssClass="wrapper">

 

 

 

<table id="products" cellpadding="3" cellspacing="3" width="90%">

 

 

 

<thead>

 

 

 

<tr>

 

 

 

<th class="expand">

 

 

 

</th>

 

 

 

<th>

 

 

D

 

</th>

 

 

 

<th>

 

 

Doc ID

 

</th>

 

 

 

<th>

 

 

Stock Number

 

</th>

 

 

 

<th>

 

 

Item Description

 

</th>

 

 

 

<th>

 

 

Quantity

 

</th>

 

 

 

<th>

 

 

Unit Issue

 

</th>

 

 

 

<th>

 

 

Unit Price

 

</th>

 

 

 

<th>

 

 

Total Price

 

</th>

 

 

 

 

<th>

 

 

2765

 

</th>

 

 

 

<th>

 

 

2407

 

</th>

 

 

 

</tr>

 

 

 

</thead>

 

 

 

 

 

 

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

 

 

</table>

 

 

 

</asp:Panel>

 

 

 

</fieldset>

 

 

 

</LayoutTemplate>

 

 

 

<EmptyDataTemplate>

 

 

 

<fieldset style="width: 100%">

 

 

 

<legend>Cart</legend>No items in your Cart.

 

 

 

</fieldset>

 

 

 

</EmptyDataTemplate>

 

 

 

<ItemTemplate>

 

 

 

<tr>

 

 

 

<td>

 

 

 

<asp:ImageButton ID="btndelete" runat="server" CommandName="Delete" Width="16" Height="16" ImageUrl="~/CMSC-Images/Icons/delete_bin_small.gif" />

 

 

 

<asp:Button ID="Button1" runat="server" Text="edit" CommandName="Edit" />

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("D")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("DocID")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("StockNumber")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("Nomenclature")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#String.Format("{0}", Eval("Qty").ToString())%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("UI")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

# Eval("UnitPrice", "{0:C}") %>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("TotalPrice", "{0:C}")%>

 

 

 

</td>

 

 

 

<td>

 

 

 

<asp:ImageButton ID="PrintButton1" runat="server" ImageUrl="~/CMSC-Images/Icons/printer1.png"

 

 

 

CommandName="Print1" Width="20" Height="20" AlternateText="Print List" />

 

 

 

</td>

 

 

 

<td>

 

 

 

<asp:ImageButton ID="PrintButton2" runat="server" ImageUrl="~/CMSC-Images/Icons/printer1.png"

 

 

 

CommandName="Print2" Width="20" Height="20" AlternateText="Print List" Visible="false" />

 

 

 

</td>

 

 

 

</tr>

 

 

 

</ItemTemplate>

 

 

 

<EditItemTemplate >

 

 

 

<tr>

 

 

 

<td>

 

 

 

<asp:ImageButton ID="UpdateButton" runat="server" ImageUrl="~/CMSC-Images/Icons/Save_Round-1.jpg"

 

 

 

CommandName="Update" Width="20" Height="20" AlternateText="Save New Quantity" />

 

 

 

</td>

 

 

 

<td>

 

 

 

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("D")%>' Width="120px"></asp:TextBox>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("DocID")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("StockNumber")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("Nomenclature")%>

 

 

 

</td>

 

 

 

<td>

 

 

 

<asp:TextBox ID="TextBoxQty" runat="server" Text='<%# Bind("Qty")%>' Width="120px"></asp:TextBox>

 

 

 

 

</td>

 

 

 

<td>

 

 

<%

#Eval("UI")%>

 

 

 

</td>

 

 

 

<td>

 

 

<%

# Eval("UnitPrice", "{0:C}") %>

 

 

 

</td>

 

 

 

<td>

 

 

 

</td>

 

 

 

<td>

 

 

 

</td>

 

 

 

</tr>

 

 

 

</EditItemTemplate>

 

</

 

 

telerik:RadListView>')

 

 

 

 

 

 

 

 

('Protected Sub RadListCart_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListViewNeedDataSourceEventArgs) Handles RadListCart.NeedDataSource

Dim GetCartItems As DataTable = getCt.GetCartDetails(Session("RoleID"))

 

If GetCartItems.Rows.Count > 0 Then

 

For i As Integer = 0 To GetCartItems.Rows.Count - 1

If GetCartItems.Rows(i).Item("DocID") = "DZ" Then
Dim PH As PlaceHolder = CType(RadListCart.Items(i).FindControl("PlaceHolder1"), PlaceHolder)

Dim Imageb As ImageButton = CType(RadListCart.Items.Item(i).FindControl("PrintButton2"), ImageButton)

Imageb.Visible = True
End If

Next

RadListCart.DataSource = GetCartItems

else
RadListCart.DataSource = GetCartItems

end sub')

 

 

 

 

 

 

 

0
Sebastian
Telerik team
answered on 06 Jan 2010, 12:39 PM
Hello Dwayne,

Thank you for the code snippets.

To find the place holder in the LayoutTemplate which hosts RadListView's items, you can use the following code fragment:

Dim PH As PlaceHolder = CType(RadListCart.FindControl("HierarchyPanel").FindControl("PlaceHolder1"), PlaceHolder)

To get reference to the second image button inside the listview's ItemTemplate, you can call the FindControl method for the corresponding RadListViewDataItem directly, i.e.:

Dim Imageb As ImageButton = CType(RadListCart.Items(i).FindControl("PrintButton2"), ImageButton)

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Dwayne Starkey
Top achievements
Rank 1
answered on 06 Jan 2010, 02:15 PM
Sebastian ,
Thanks for the feedback.

I am still getting the 'Object reference not set to an instance of an object.'

on 
Dim PH As PlaceHolder = CType(RadListCart.FindControl("HierarchyPanel").FindControl("PlaceHolder1"), PlaceHolder)

Anyother suggestions would be appreaciated.


Thanks for your time.
0
Sebastian
Telerik team
answered on 08 Jan 2010, 04:43 PM

Hello Dwayne,

Can you please check using the VS debugger whether the control with id HierarchyPanel or PlaceHolder1 is present in the relevant Controls collections when you invoke the FindControl methods? You may also invoke the same code slice inside the PreRender handler of the listview to see whether this makes a difference.

If this does not help, please isolate a working subset of your project and send it to a regular support ticket. We will test it locally and will get back to you with more info on the subject.

Best regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Accepted
Dyt Iscus
Top achievements
Rank 1
answered on 11 Jan 2010, 03:35 PM
The problem seems quite easy, you are asking the items collection of the listview wich isn't databound yet (you are in the event needsdatasource) so the collection is empty.
The object not set... is triggered by the index i in the line:
Dim Imageb As ImageButton =   
CType(RadListCart.Items(i).FindControl("PrintButton2"), ImageButton) 
This is the index from you table loop...

I recommend using the ItemCreatedEvent. Therefore to make your life more easy set the value you want to evaluate in the datakeys property avoiding the dataitem problems(only filled when control is created).

telerik:RadListView ID="RadListCart" runat="server" ItemPlaceholderID="PlaceHolder1" DataKeyNames="cID,DocID"

and the ItemCreated event
        void RadListCart_ItemCreated(object sender, RadListViewItemEventArgs e)  
        {  
            if (e.Item is RadListViewDataItem)  
            {  
                RadListViewDataItem li = (RadListViewDataItem)e.Item;  
                if (e.Item.OwnerListView.DataKeyValues[li.DisplayIndex]["DocID"].ToString() == "DZ")  
                {  
                    ((Button)e.Item.FindControl("MyButton")).Visible = false;  
                }  
            }  
        }  
 

Not absolutly sure about the DisplayIndex (the index of the item on the screen not in the collection) but its the only index available and not tested this code.

HTH
Carol
0
Dwayne Starkey
Top achievements
Rank 1
answered on 11 Jan 2010, 03:42 PM

Thanks for the reply.

I found that once I re-looked at it,  I was making it harder than what it needed to be. I ended up using the ItemDataBound Event and used the following code.

 

 

Dim lvitem As RadListViewDataItem = DirectCast(e.Item(), RadListViewDataItem)

 

 

If lvitem.DataItem("ID") = "DZ" Then

 

 

 

 

e.Item().FindControl(

"Print2").Visible = True

 

 

 

 

e.Item().FindControl(

"Print1").Visible = False

 

 

 

 

 

Else

 

 

 

 

e.Item().FindControl(

"Print2").Visible = False

 

 

 

 

e.Item().FindControl(

"Print1").Visible = True

 

 

 

 

 

End If

 

 

Tags
ListView
Asked by
Dwayne Starkey
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Dwayne Starkey
Top achievements
Rank 1
Dyt Iscus
Top achievements
Rank 1
Share this question
or