0

i am getting desperate with this one. I receive a tree data structure from a webservice and try to propose a browser and a search functionality. To browse the structure I use an updatepanel containing a listview including linkbuttons leading to the sublevels of the structure (similar to a folder explorer view). The problem is that the linkbutton only work for the first sub-level, when in a subfolder the link button does not fire the corresponding function and only reload the page.

Example:

Structure:

Root
- Folder 1
  - Folder A
  - Folder B
    - Folder b1
    - Folder b2
    - Folder b3
  - Folder c
  - Folder D 
- Folder 2
- Folder 3

Page Load:

- Folder 1
- Folder 2
- Folder 3

Click on Folder 1:

- Folder A
- Folder B
- Folder c
- Folder D 

Click on Folder B reload the page with the root structure instead of loading:

- Folder b1
- Folder b2
- Folder b3

The apsx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="TMSD4CJ._default" ClientIDMode="AutoID"%>
<%@ Register TagPrefix="sb" TagName="SearchBox" Src="~/Controls/SearchBox.ascx" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta charset="utf-8" />
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
                    <asp:ScriptManager ID="SM" runat="server" EnablePartialRendering="true" /> 
                    <asp:UpdatePanel ID="upnl" runat="server" UpdateMode="Always">

                    <ContentTemplate>
                        <asp:ListView ID="lstView_entries" runat="server" OnItemDataBound="lstView_entries_ItemDataBound">
                            <LayoutTemplate>
                                <table class="table table-hover">
                                    <tr id="ItemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </LayoutTemplate>         
                            <ItemTemplate>
                                <tr>
                                    <td><asp:LinkButton runat="server" ID="lbtn_entryName"  OnCommand="lbtn_entryName_Click" CommandArgument=""></asp:LinkButton></td>
                                </tr>
                            </ItemTemplate>
                            </asp:ListView>
                        </ContentTemplate>       
                        </asp:UpdatePanel>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

The CS page:

protected void lstView_entries_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        LinkButton lbtnEntryName = (LinkButton)e.Item.FindControl("lbtn_entryName");
        var itemType = e.Item.DataItem.GetType();
        if(itemType == typeof(Catalog))
        {
            Catalog currentItem = e.Item.DataItem as Catalog;
            lbtnEntryName.Text = currentItem.Name;
            lbtnEntryName.ToolTip = currentItem.Id;
            lbtnEntryName.CommandArgument = currentItem.Id;
            System.Diagnostics.Debug.WriteLine("Set Name: " + currentItem.Name + " Set ID: " + currentItem.Id);
        }

        SM.RegisterAsyncPostBackControl(lbtnEntryName);
    }
}

protected void lbtn_entryName_Click(object sender, CommandEventArgs e)
{
    Catalog rootCat = (Catalog)ViewState["catalogueEntries"];
    List<object> catalogueEntries = new List<object>();
    Catalog catFound = searchCatalogByID(rootCat, e.CommandArgument.ToString());

    lstView_entries.DataSource = catalogueEntries;
    lstView_entries.DataBind();
}

I read many similar questions of the forum (here, here) but did not get it to work. I would highly appreciate a second pair of eyes :-)

Community
  • 1
  • 1
Salim
  • 495
  • 3
  • 20

1 Answers1

0

Well I finally get it to work but do not really understand why and if it is clean code. If I create a function with the content of lbtn_entryName_Click add a check for postback and call the function from that point it work well.

protected void Page_Load(object sender, EventArgs e)
{
  if(Page.IsPostBack)
  {
    LinkButton lbtn = sender as LinkButton;
    searchCatAndUpdateview(lbtn.CommandArgument);
  }
}

protected void searchCatAndUpdateview(string catID)
{
  Catalog rootCat = (Catalog)ViewState["catalogueEntries"];
  List<object> catalogueEntries = new List<object>();
  Catalog catFound = searchCatalogByID(rootCat, catID);
  lstView_entries.DataSource = catalogueEntries;
  lstView_entries.DataBind();
}

Could anyone confirm if it is an ok manner and potentially clear our the why lbtn_entryName_Click only works once.

Salim
  • 495
  • 3
  • 20