locked
Cannot Access Button in ListView RRS feed

  • Question

  • User1356352994 posted

    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;



    Thursday, December 6, 2012 6:21 PM

Answers

  • User3866881 posted

    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.

    void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e)
      {
           Button btn = (Button)YourListItem.Items[e.NewEditIndex].FindControl("EditButton");
           btn.Visible = Session["loggin"]!=null;
      }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2012 9:22 PM

All replies

  • User1178673432 posted

    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 Sub



    Thursday, December 6, 2012 9:15 PM
  • User1356352994 posted

    uuh... yeah I don't know what that translates into for C#

    Thursday, December 6, 2012 10:48 PM
  • User3866881 posted

    uuh... yeah I don't know what that translates into for C#

    protected void MowerList_ItemCreated(object sender, ListViewItemEventArgs e)
    {
    	if (e.Item.ItemType == ListViewItemType.DataItem) {
    		dynamic btn = (Button)e.Item.FindControl("EditButton");
    		btn.Visible = true;
    	}
    
    }
    Friday, December 7, 2012 3:30 AM
  • User1356352994 posted

    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.

    Friday, December 7, 2012 8:22 AM
  • User1356352994 posted

    This is not working.

    Friday, December 7, 2012 8:29 AM
  • User3866881 posted

    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.

    void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e)
      {
           Button btn = (Button)YourListItem.Items[e.NewEditIndex].FindControl("EditButton");
           btn.Visible = Session["loggin"]!=null;
      }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 7, 2012 9:22 PM