Solved

DropDownList1_SelectedIndexChanged and TreeView

Posted on 2007-12-05
12
1,472 Views
Last Modified: 2013-11-26
Hi..

I have a DropDownList1 and a treeview.
dropdown has years 2006, 2007 values - and what I am trying to achieve is that whenever dropdown value changes, reloading the treeview.

So far, it is working with a small bug.

when the page loads, everything is looking good. Dropdown value is 2006. then, if I change the value on the dropdown to 2007, my treeview reloads for 2007 data - GOOD.

... but, If I change the dropdown value again to 2006, treeview does not do nothing and keep the 2007 data.... why is that?

I am wondering if I am not setting "PopulateOnDemand = true" correctly...

Thanks ...

Here are my code:

in my form.aspx:

                    <asp:DropDownList ID="DropDownList1" runat="server" Width="80px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" >
                        <asp:ListItem>2006</asp:ListItem>
                        <asp:ListItem>2007</asp:ListItem>
                    </asp:DropDownList>&nbsp;
                    <br />
                    <asp:TreeView ID="TreeView4" OnTreeNodePopulate="PopulatePayPeriods" ExpandDepth="0" runat="server" NodeWrap="True">
                        <Nodes>
                            <asp:TreeNode Text="Pay Periods" SelectAction="Expand" PopulateOnDemand="True" Value="Pay Periods"/>
                        </Nodes>
                    </asp:TreeView>
--------------------------------------
in the form.aspx.cs
--------------------------------------
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TmpYear = DropDownList1.Text;
            TreeView4.ExpandDepth = 0;
        }

        public void PopulatePayPeriods(Object source, TreeNodeEventArgs e)
        {
            TmpYear = DropDownList1.Text;
            switch (e.Node.Depth)
            {
                case 0:
                    GetPayPeriods1(e.Node);
                    break;
            }
        }
        void GetPayPeriods1(TreeNode node)
        {
            PayPeriodList payperiods = GetPayPeriods();
            if (null != payperiods)
            {
                foreach (PayPeriod c in payperiods)
                {

                    TreeNode newNode = new TreeNode(c.PayMonth + " - " + c.PayDate, c.Id);
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    //newNode.PopulateOnDemand = true;
                    node.ChildNodes.Add(newNode);

                    //TreeNode newNodeChild = new TreeNode("\t" + c.PayPeriodStart + " to " + c.PayPeriodEnd, c.Id);
                    //newNodeChild.SelectAction = TreeNodeSelectAction.Expand;
                    //newNodeChild.PopulateOnDemand = true;
                    //newNode.ChildNodes.Add(newNodeChild);
                }
            }
            else
            {
                TreeNode newNode = new TreeNode("No Record Found", "0");
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.PopulateOnDemand = true;
                node.ChildNodes.Add(newNode);
            }
        }
        public PayPeriodList GetPayPeriods()
        {
            //string TmpYear = DateTime.Now.Year.ToString();
            TmpYear = DropDownList1.Text;
            PayPeriodList payperiods = new PayPeriodList();

            OleDbConnection DbConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["AM"].ConnectionString);
            DbConn.Open();
            DataSet Dt = new DataSet();
            OleDbDataAdapter Da = new OleDbDataAdapter("Select ID, PayMonth, PayDate, PayPeriodStart, PayPeriodEnd from TblPayPeriods Where PayYear = '" + TmpYear + "' Order By  PayPeriodSeq", DbConn);
            Da.Fill(Dt);
            DataTable dataTbl = Dt.Tables[0];
            int i;

            if (dataTbl.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                for (i = 0; i <= dataTbl.Rows.Count - 1; i++)
                {
                    payperiods.Add(new PayPeriod(
                        dataTbl.Rows[i]["ID"].ToString(),
                        dataTbl.Rows[i]["PayMonth"].ToString(),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayDate"]),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayPeriodStart"]),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayPeriodEnd"])));
                }
                return payperiods;
            }
        }
        public class PayPeriod
        {
            public string Id;
            public string PayMonth;
            public string PayPeriodStart;
            public string PayPeriodEnd;
            public string PayDate;

            public PayPeriod(String id, String PPayMonth, DateTime PPayPeriodStart, DateTime PPayPeriodEnd, DateTime PPayDate)
            {
                this.Id = id;
                this.PayMonth = PPayMonth;
                this.PayPeriodStart = Convert.ToDateTime(PPayPeriodStart).ToString("MMM/dd/yyyy");
                this.PayPeriodEnd = Convert.ToDateTime(PPayPeriodEnd).ToString("dd/yyyy");
                this.PayDate = Convert.ToDateTime(PPayDate).ToString("dd/yyyy");
            }
        }

        public class PayPeriodList : ArrayList
        {

            public new PayPeriod this[int i]
            {
                get
                {
                    return (PayPeriod)base[i];
                }
                set
                {
                    base[i] = value;
                }
            }
        }

0
Comment
Question by:neonlights
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 12

Accepted Solution

by:
needo_jee earned 250 total points
ID: 20414611
why you dont call your treeview update here inside index change event?

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TmpYear = DropDownList1.Text;

            TreeView4.ExpandDepth = 0;
PoupuLateMyTreeFromDB(YearValue) ;//DropDownList1.Text
        }
0
 

Author Comment

by:neonlights
ID: 20414641
Thanks needo jee..

what will be the PoupuLateMyTreeFromDB(YearValue)?
Thanks
0
 

Author Comment

by:neonlights
ID: 20414718
because, if you look at my code above:

I can either use:
       void GetPayPeriods1(TreeNode node)
so, if I want to call this GetPayPeriods1(???) what will is ???

0
 
LVL 14

Assisted Solution

by:chinu1310
chinu1310 earned 150 total points
ID: 20414777
Hi,

PoupuLateMyTreeFromDB . This will be your function which will fetch the data from database using the passed input parameter which is Year and bind that result to your tree view control.
Whrere year is getting the value from "  DropDownList1.Text "

Hope it helps.

0
 
LVL 10

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 100 total points
ID: 20414815
Needo_jee is right
you have to get effect on the selectedchange event of dropdown button
so just change your logic and do in that way
0
 

Author Comment

by:neonlights
ID: 20414878
Hi both.. I am sorry I do not understand.. I am new to asp.net..

where is PoupuLateMyTreeFromDB coming from? sorry, not following you guys.

you see, I have this GetPayPeriods1(TreeNode node)

How about if I psss a root node to this procedure?
In my case, it will be root node - do you guys know how would I do that?

So far I have:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TreeView4.ExpandDepth = 0;
            PoupuLateTreeView4();
        }
        public void PoupuLateTreeView4()
        {
            TreeView4.Nodes.Clear();
            TreeView4.Nodes.Add("Pay Periods"); --- will be my root node
            GetPayPeriods1(TreeView4.Nodes[0]);
        }

--------- code ------------

void GetPayPeriods1(TreeNode node)
        {
            PayPeriodList payperiods = GetPayPeriods();
            if (null != payperiods)
            {
                foreach (PayPeriod c in payperiods)
                {

                    TreeNode newNode = new TreeNode(c.PayMonth + " - " + c.PayDate, c.Id);
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(newNode);
                }
            }
            else
            {
                TreeNode newNode = new TreeNode("No Record Found", "0");
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.PopulateOnDemand = true;
                node.ChildNodes.Add(newNode);
            }
        }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:Alpesh Patel
ID: 20414954
where is your GetPayPeriods() code
show me that code
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 20414957
On what parameters you are fetching pay periods form db? using Year if I am not missing ?Where you are passing this value.

0
 

Author Comment

by:neonlights
ID: 20415253
I guess I am cofusing you guys.. .

Here we go again - from the beggining...
In my form.aspx - I had a treeview. and I used OnTreeNodePopulate="PopulatePayPeriods"  and PopulateOnDemand="True" to populate the treeview.

But, then, I needed to add a dropdown to select a year. This is where I am having tough time.
I did not know at the begging that I have to use dropdown list to keep the year.

-----------CODE ----------------------------------------------------
<asp:DropDownList ID="DropDownList1" runat="server" Width="80px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" >
                        <asp:ListItem>2006</asp:ListItem>
                        <asp:ListItem>2007</asp:ListItem>
                    </asp:DropDownList>&nbsp;

                    <asp:TreeView ID="TreeView4" OnTreeNodePopulate="PopulatePayPeriods" ExpandDepth="0" runat="server" NodeWrap="True">
                        <Nodes>
                            <asp:TreeNode Text="Pay Periods" SelectAction="Expand" PopulateOnDemand="True" Value="Pay Periods"/>
                        </Nodes>
                    </asp:TreeView>
--------------------- CODE ---------------------------------------------

and if you notice, I have OnTreeNodePopulate="PopulatePayPeriods"  and PopulateOnDemand="True"
Now, PopulatePayPeriods is:

---------------- CODE PopulatePayPeriods-------------------------------------------------
public void PopulatePayPeriods(Object source, TreeNodeEventArgs e)
        {
            string TmpYear = DropDownList1.Text;
            switch (e.Node.Depth)
            {
                case 0:
                    GetPayPeriods1(e.Node);
                    break;
            }
        }
---------------- CODE GetPayPeriods1-------------------------------------------------

        void GetPayPeriods1(TreeNode node)
        {
            PayPeriodList payperiods = GetPayPeriods();
            if (null != payperiods)
            {
                foreach (PayPeriod c in payperiods)
                {

                    TreeNode newNode = new TreeNode(c.PayMonth + " - " + c.PayDate, c.Id);
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    newNode.PopulateOnDemand = false;
                    node.ChildNodes.Add(newNode);
                }
            }
            else
            {
                TreeNode newNode = new TreeNode("No Record Found", "0");
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.PopulateOnDemand = true;
                node.ChildNodes.Add(newNode);
            }
        }
--------------------------------CODE GetPayPeriods--------------------------------
        public PayPeriodList GetPayPeriods()
        {
            //string TmpYear = DateTime.Now.Year.ToString();
            string TmpYear = DropDownList1.Text;
            PayPeriodList payperiods = new PayPeriodList();

            OleDbConnection DbConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["AM"].ConnectionString);
            DbConn.Open();
            DataSet Dt = new DataSet();
            OleDbDataAdapter Da = new OleDbDataAdapter("Select ID, PayMonth, PayDate, PayPeriodStart, PayPeriodEnd from TblPayPeriods Where PayYear = '" + TmpYear + "' Order By  PayPeriodSeq", DbConn);
            Da.Fill(Dt);
            DataTable dataTbl = Dt.Tables[0];
            int i;

            if (dataTbl.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                for (i = 0; i <= dataTbl.Rows.Count - 1; i++)
                {
                    payperiods.Add(new PayPeriod(
                        dataTbl.Rows[i]["ID"].ToString(),
                        dataTbl.Rows[i]["PayMonth"].ToString(),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayDate"]),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayPeriodStart"]),
                        Convert.ToDateTime(dataTbl.Rows[i]["PayPeriodEnd"])));
                }
                return payperiods;
            }
        }
--------------------------------CODE Class PayPeriod--------------------------------
        public class PayPeriod
        {
            public string Id;
            public string PayMonth;
            public string PayPeriodStart;
            public string PayPeriodEnd;
            public string PayDate;

            public PayPeriod(String id, String PPayMonth, DateTime PPayPeriodStart, DateTime PPayPeriodEnd, DateTime PPayDate)
            {
                this.Id = id;
                this.PayMonth = PPayMonth;
                this.PayPeriodStart = Convert.ToDateTime(PPayPeriodStart).ToString("MMM/dd/yyyy");
                this.PayPeriodEnd = Convert.ToDateTime(PPayPeriodEnd).ToString("dd/yyyy");
                this.PayDate = Convert.ToDateTime(PPayDate).ToString("dd/yyyy");
            }
        }

--------------------------------CODE Class PayPeriodList--------------------------------

        public class PayPeriodList : ArrayList
        {

            public new PayPeriod this[int i]
            {
                get
                {
                    return (PayPeriod)base[i];
                }
                set
                {
                    base[i] = value;
                }
            }
        }

------------------- WHERE I AM NOW--------------------------------

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TreeView4.ExpandDepth = 0;
            PoupuLateTreeView4();
        }

        public void PoupuLateTreeView4()
        {
            GetPayPeriods1(TreeView4.Nodes[0]);
        }

It is working - except that previous nodes are not clearing .. and everytime I change the value in the dropdown, it is keep filling my treeview.. without clearing my treeview.
0
 

Author Comment

by:neonlights
ID: 20415263
I am not passing dropdown year value - since I can get that value from:
eg:

public PayPeriodList GetPayPeriods()
        {
            //string TmpYear = DateTime.Now.Year.ToString();
            string TmpYear = DropDownList1.Text;
....


0
 
LVL 14

Expert Comment

by:chinu1310
ID: 20415284
Add this to selected index change event of your drop down

TreeView4.Nodes.Clear();
0
 

Author Comment

by:neonlights
ID: 20420190
hi,

Thanks for all your answers and suggestions.. do not know why I got lost in this one.. it was very easy.. sorry for the confusion.

thanks all
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now