1
Reply

Collpasing and Expanding items in Datalist

Jaco Bosch

Jaco Bosch

Aug 27 2012 11:23 AM
1.5k

Good day

I have created a website in ASP.net using Datalists.  I am binding the data from a database using a datalist.  I am trying to expand and collapse the items in the datalist.  When I click on the first item in the datalist, it expands and when I click on the first item again, then it collapses.  That is working 100%.

The problem that I have is, when I click on the first item in the datalist, and I click on the second item in the datalist, the first item in the datalist stays open..

When I want to collapse the first item in the datalist, after opening the second item in the datalist, by clicking again on the first item in the datalist, both items are closing.. What can I add or change in my coding, so that when I open another item in the datalist, the previous one that I opened, collapse automatically?

Here is my code

protected void Page_Load(object sender, EventArgs e)
    {
      
            if (!IsPostBack)
            {
                Session["SystemName"] = "";
                loadSystemnames();
            }
     }
//Loading the names in the datalist
    public void loadSystemnames()
    {
        clsTsogoSafe.Instance.Decrypt();
        clsDBCalls objDBcalls = new clsDBCalls();
      
        dtSystems = objDBcalls.dtSystems();
        dtlSystems.DataSource = dtSystems;
        dtlSystems.DataBind();
    }

//Clicking on an item in the datalist will expand or close an item in the datalist
    public void lnkSystemnames_Click(object sender, DataListCommandEventArgs e)
    {

        if (IsPostBack)
        {
            clsDBCalls objDBcalls = new clsDBCalls();

            DataTable dtfullDesc = new DataTable();
            int iDTListID = e.Item.ItemIndex;
    
            LinkButton strlblSystem = e.Item.FindControl("lnkSystem") as LinkButton;
            string strValue = strlblSystem.Text;

            if (Session["SystemName"].ToString() != strValue)
            {

                dtfullDesc = objDBcalls.dtSystemDescription(strValue);
                DataList dtSystemdescription = e.Item.FindControl("dtSystemDescription") as DataList;

                Session["SystemName"] = strValue;
                dtSystemdescription.DataSource = dtfullDesc;
                dtSystemdescription.DataBind();
            }
            else
            {
                Session["SystemName"] = "";
                loadSystemnames();
            }
        }
    }

Thanks for the help


Answers (1)