Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Posted
Updated 27-Aug-12 4:46am
v2

1 solution

C#
public void lnkSystemnames_Click(object sender, DataListCommandEventArgs e)
   {

       if (IsPostBack)
       {
//here use for loop on datalist row to collapse row if it is open it will collpase all
//row

//your code as it is so first you are collapse all row then your code to open only //clicked row
 
Share this answer
 
Comments
JacoBosch 27-Aug-12 8:48am    
Hi pradiprenushe, thanks for the reply.. How can I check whether the row was opened, because at the momemnt I am only checking whether the item was clicked.
pradiprenushe 27-Aug-12 8:51am    
What are you using for collapsing?
If you are using ajax collapsible panel then refer this
http://chandradev819.wordpress.com/2010/11/03/how-to-make-collapable-panel-in-datalist-using-ajax/
http://www.vbforums.com/showthread.php?637327-RESOLVED-AJAX-Collapsible-Panel-inside-Repeater-strangeness
JacoBosch 27-Aug-12 9:00am    
When I am collapsing the item in the datalist, I used a session variable, and rebinded the data.. I am not using the ajax collapsible panel.
pradiprenushe 27-Aug-12 9:03am    
use some hidden field to store row info which is open. while in for loop check rowid with that hidden field
JacoBosch 27-Aug-12 10:39am    
Hi pradiprenushe, I have tried it, but it did not work.. Do you have any other idea how this problem can be solved?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900