neonlights
asked on
DropDownList1_SelectedIndexChanged and TreeView
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="Dr opDownList 1_Selected IndexChang ed" AutoPostBack="True" >
<asp:ListItem>2006</asp:Li stItem>
<asp:ListItem>2007</asp:Li stItem>
</asp:DropDownList>
<br />
<asp:TreeView ID="TreeView4" OnTreeNodePopulate="Popula tePayPerio ds" 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_SelectedInde xChanged(o bject 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.Expan d;
//newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod e);
//TreeNode newNodeChild = new TreeNode("\t" + c.PayPeriodStart + " to " + c.PayPeriodEnd, c.Id);
//newNodeChild.SelectActio n = TreeNodeSelectAction.Expan d;
//newNodeChild.PopulateOnD emand = true;
//newNode.ChildNodes.Add(n ewNodeChil d);
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan d;
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod e);
}
}
public PayPeriodList GetPayPeriods()
{
//string TmpYear = DateTime.Now.Year.ToString ();
TmpYear = DropDownList1.Text;
PayPeriodList payperiods = new PayPeriodList();
OleDbConnection DbConn = new OleDbConnection(Configurat ionManager .Connectio nStrings[" AM"].Conne ctionStrin g);
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"].ToSt ring(),
dataTbl.Rows[i]["PayMonth" ].ToString (),
Convert.ToDateTime(dataTbl .Rows[i][" PayDate"]) ,
Convert.ToDateTime(dataTbl .Rows[i][" PayPeriodS tart"]),
Convert.ToDateTime(dataTbl .Rows[i][" PayPeriodE nd"])));
}
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(PPayPer iodStart). ToString(" MMM/dd/yyy y");
this.PayPeriodEnd = Convert.ToDateTime(PPayPer iodEnd).To String("dd /yyyy");
this.PayDate = Convert.ToDateTime(PPayDat e).ToStrin g("dd/yyyy ");
}
}
public class PayPeriodList : ArrayList
{
public new PayPeriod this[int i]
{
get
{
return (PayPeriod)base[i];
}
set
{
base[i] = value;
}
}
}
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="Dr
<asp:ListItem>2006</asp:Li
<asp:ListItem>2007</asp:Li
</asp:DropDownList>
<br />
<asp:TreeView ID="TreeView4" OnTreeNodePopulate="Popula
<Nodes>
<asp:TreeNode Text="Pay Periods" SelectAction="Expand" PopulateOnDemand="True" Value="Pay Periods"/>
</Nodes>
</asp:TreeView>
--------------------------
in the form.aspx.cs
--------------------------
protected void DropDownList1_SelectedInde
{
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.Expan
//newNode.PopulateOnDemand
node.ChildNodes.Add(newNod
//TreeNode newNodeChild = new TreeNode("\t" + c.PayPeriodStart + " to " + c.PayPeriodEnd, c.Id);
//newNodeChild.SelectActio
//newNodeChild.PopulateOnD
//newNode.ChildNodes.Add(n
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod
}
}
public PayPeriodList GetPayPeriods()
{
//string TmpYear = DateTime.Now.Year.ToString
TmpYear = DropDownList1.Text;
PayPeriodList payperiods = new PayPeriodList();
OleDbConnection DbConn = new OleDbConnection(Configurat
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"].ToSt
dataTbl.Rows[i]["PayMonth"
Convert.ToDateTime(dataTbl
Convert.ToDateTime(dataTbl
Convert.ToDateTime(dataTbl
}
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(PPayPer
this.PayPeriodEnd = Convert.ToDateTime(PPayPer
this.PayDate = Convert.ToDateTime(PPayDat
}
}
public class PayPeriodList : ArrayList
{
public new PayPeriod this[int i]
{
get
{
return (PayPeriod)base[i];
}
set
{
base[i] = value;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ???
I can either use:
void GetPayPeriods1(TreeNode node)
so, if I want to call this GetPayPeriods1(???) what will is ???
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_SelectedInde xChanged(o bject 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.N odes[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.Expan d;
node.ChildNodes.Add(newNod e);
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan d;
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod e);
}
}
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_SelectedInde
{
TreeView4.ExpandDepth = 0;
PoupuLateTreeView4();
}
public void PoupuLateTreeView4()
{
TreeView4.Nodes.Clear();
TreeView4.Nodes.Add("Pay Periods"); --- will be my root node
GetPayPeriods1(TreeView4.N
}
--------- 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.Expan
node.ChildNodes.Add(newNod
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod
}
}
where is your GetPayPeriods() code
show me that code
show me that code
On what parameters you are fetching pay periods form db? using Year if I am not missing ?Where you are passing this value.
ASKER
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="Popula tePayPerio ds" 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="Dr opDownList 1_Selected IndexChang ed" AutoPostBack="True" >
<asp:ListItem>2006</asp:Li stItem>
<asp:ListItem>2007</asp:Li stItem>
</asp:DropDownList>
<asp:TreeView ID="TreeView4" OnTreeNodePopulate="Popula tePayPerio ds" 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="Popula tePayPerio ds" 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.Expan d;
newNode.PopulateOnDemand = false;
node.ChildNodes.Add(newNod e);
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan d;
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod e);
}
}
-------------------------- ------CODE GetPayPeriods------------- ---------- ---------
public PayPeriodList GetPayPeriods()
{
//string TmpYear = DateTime.Now.Year.ToString ();
string TmpYear = DropDownList1.Text;
PayPeriodList payperiods = new PayPeriodList();
OleDbConnection DbConn = new OleDbConnection(Configurat ionManager .Connectio nStrings[" AM"].Conne ctionStrin g);
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"].ToSt ring(),
dataTbl.Rows[i]["PayMonth" ].ToString (),
Convert.ToDateTime(dataTbl .Rows[i][" PayDate"]) ,
Convert.ToDateTime(dataTbl .Rows[i][" PayPeriodS tart"]),
Convert.ToDateTime(dataTbl .Rows[i][" PayPeriodE nd"])));
}
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(PPayPer iodStart). ToString(" MMM/dd/yyy y");
this.PayPeriodEnd = Convert.ToDateTime(PPayPer iodEnd).To String("dd /yyyy");
this.PayDate = Convert.ToDateTime(PPayDat e).ToStrin g("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_SelectedInde xChanged(o bject sender, EventArgs e)
{
TreeView4.ExpandDepth = 0;
PoupuLateTreeView4();
}
public void PoupuLateTreeView4()
{
GetPayPeriods1(TreeView4.N odes[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.
Here we go again - from the beggining...
In my form.aspx - I had a treeview. and I used OnTreeNodePopulate="Popula
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="Dr
<asp:ListItem>2006</asp:Li
<asp:ListItem>2007</asp:Li
</asp:DropDownList>
<asp:TreeView ID="TreeView4" OnTreeNodePopulate="Popula
<Nodes>
<asp:TreeNode Text="Pay Periods" SelectAction="Expand" PopulateOnDemand="True" Value="Pay Periods"/>
</Nodes>
</asp:TreeView>
--------------------- CODE --------------------------
and if you notice, I have OnTreeNodePopulate="Popula
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.Expan
newNode.PopulateOnDemand = false;
node.ChildNodes.Add(newNod
}
}
else
{
TreeNode newNode = new TreeNode("No Record Found", "0");
newNode.SelectAction = TreeNodeSelectAction.Expan
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNod
}
}
--------------------------
public PayPeriodList GetPayPeriods()
{
//string TmpYear = DateTime.Now.Year.ToString
string TmpYear = DropDownList1.Text;
PayPeriodList payperiods = new PayPeriodList();
OleDbConnection DbConn = new OleDbConnection(Configurat
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"].ToSt
dataTbl.Rows[i]["PayMonth"
Convert.ToDateTime(dataTbl
Convert.ToDateTime(dataTbl
Convert.ToDateTime(dataTbl
}
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(PPayPer
this.PayPeriodEnd = Convert.ToDateTime(PPayPer
this.PayDate = Convert.ToDateTime(PPayDat
}
}
--------------------------
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_SelectedInde
{
TreeView4.ExpandDepth = 0;
PoupuLateTreeView4();
}
public void PoupuLateTreeView4()
{
GetPayPeriods1(TreeView4.N
}
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.
ASKER
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;
....
eg:
public PayPeriodList GetPayPeriods()
{
//string TmpYear = DateTime.Now.Year.ToString
string TmpYear = DropDownList1.Text;
....
Add this to selected index change event of your drop down
TreeView4.Nodes.Clear();
TreeView4.Nodes.Clear();
ASKER
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
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
ASKER
what will be the PoupuLateMyTreeFromDB(Year
Thanks