Please try to use ItemCreated event and ListViewItemEventArgs.
Here is some code: (sorry its VB.NET)
Protected Sub MowerList_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles MowerList.ItemCreated
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim btn = DirectCast(e.Item.FindControl("EditButton"), Button)
btn.Visible = True
End If
End Sub
Thank you Decker Dong, how would I go about applying this function to a page_load event? What is happening is, if the user has an administrator session variable, to show this EditButton in the ListView. Here, you have this buttong being defined when the
ListView is created.
Thank you Decker Dong, how would I go about applying this function to a page_load event? What is happening is, if the user has an administrator session variable, to show this EditButton in the ListView. Here, you have this buttong being defined when the
ListView is created.
Hello,
As far as I see, I think you can just check whether the session is null or not (suppose this means whether you've successfully logged in or not), And handle the event of ItemEditing if you wanna control whether the Edit button is visible for you or not.
bangtheory
Member
52 Points
58 Posts
Cannot Access Button in ListView
Dec 06, 2012 10:21 PM|LINK
I have a ListView and I stashed a custom button inside of it. I have it's current view state set to false. I want it show based on a session variable.
Here is the front-end
<ItemTemplate> <div class="product-image"><asp:Image ID="Thumbimage" runat="server" ImageUrl='<%# Eval("ThumbImage") %>' /></div><!-- end product-image --> <div class="product-info"> <ul> <li class="product-title"> <asp:Label ID="ManufacturerLabel" runat="server" Text='<%# Eval("Manufacturer") %>' /></li> <li class="product-desc"> <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' /></li> <li style="font-weight:bold;" class="font13 green"> <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price", "{0:c}") %>' /></li> <li class="product-price"><asp:Label ID="SaleLabel" runat="server" Text='<%# Eval("SalePrice", "{0:c}") %>' /></li> <li class="btn-container"> <asp:Button ID="ViewButton" runat="server" CssClass="btn" Text="View Item" CommandName="Select" OnClick="HideDataPager" /> <!-- this is the button I am trying to access --><asp:Button ID="EditButton" runat="server" CssClass="btn" Visible="false" Text="Edit Items" onclick="EditButton_Click"/> </li> </ul> </div><!-- end product-info -->I tried to do this in the code behind. This didn't work
Button editBtn = (Button)MowerList.FindControl("EditButton"); editBtn.Visible = true;kyk_nk
Member
2 Points
1 Post
Re: Cannot Access Button in ListView
Dec 07, 2012 01:15 AM|LINK
Hi
Please try to use ItemCreated event and ListViewItemEventArgs.
Here is some code: (sorry its VB.NET)
Protected Sub MowerList_ItemCreated(sender As Object, e As ListViewItemEventArgs) Handles MowerList.ItemCreated If e.Item.ItemType = ListViewItemType.DataItem Then Dim btn = DirectCast(e.Item.FindControl("EditButton"), Button) btn.Visible = True End If End Subbangtheory
Member
52 Points
58 Posts
Re: Cannot Access Button in ListView
Dec 07, 2012 02:48 AM|LINK
uuh... yeah I don't know what that translates into for C#
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Cannot Access Button in ListView
Dec 07, 2012 07:30 AM|LINK
protected void MowerList_ItemCreated(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { dynamic btn = (Button)e.Item.FindControl("EditButton"); btn.Visible = true; } }bangtheory
Member
52 Points
58 Posts
Re: Cannot Access Button in ListView
Dec 07, 2012 12:22 PM|LINK
Thank you Decker Dong, how would I go about applying this function to a page_load event? What is happening is, if the user has an administrator session variable, to show this EditButton in the ListView. Here, you have this buttong being defined when the ListView is created.
bangtheory
Member
52 Points
58 Posts
Re: Cannot Access Button in ListView
Dec 07, 2012 12:29 PM|LINK
This is not working.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Cannot Access Button in ListView
Dec 08, 2012 01:22 AM|LINK
Hello,
As far as I see, I think you can just check whether the session is null or not (suppose this means whether you've successfully logged in or not), And handle the event of ItemEditing if you wanna control whether the Edit button is visible for you or not.
void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e) { Button btn = (Button)YourListItem.Items[e.NewEditIndex].FindControl("EditButton"); btn.Visible = Session["loggin"]!=null; }