Re: explanation of when need to repopulate control



Another problem:

while i'm waiting on your remark to my last post, i removed the sub
composite controls from my composite control so that there is only drop down
lists on it. During CreateChildcontrols, i load the main drop down using
databind. When the page is sent to browser for the first time, i have my
main drop down filled. I then select an item and its SelectedIndexChanged
fires and populates the send drop down list. Then when i select an item from
it, it posts back, and the event that gets called is the main(first)
dropdown list's SelectedIndexChanged event, which then re-populates the
second drop down list, then the control returns to the browser (The
SelectedIndexChanged event never fired for the 2nd dropdown's changed
event.???

thanks again

using System;

using System.Collections.Specialized;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using OperationsTeams.Business;

using OperationsTeams.Data;

using OperationsTeams.Reporting;

using OperationsTeams.Reporting.WebControls;

namespace OperationsTeams.Web.ReportControls

{

/// <summary>

/// Summary description for FiscalAgentHierarchy.

/// </summary>

public class FiscalAgentHierarchy : WebControl, IReportParameterControl,
INamingContainer

{

public FiscalAgentHierarchy()

{

Parameters.Add(new Parameter("@SchoolYear"));

Parameters.Add(new Parameter("@ReportingGroup"));

Parameters.Add(new Parameter("@FiscalAgentID"));

Parameters.Add(new Parameter("@FundingSourceID"));

Parameters.Add(new Parameter("@ProviderID"));

Parameters.Add(new Parameter("@SiteID"));

Parameters.Add(new Parameter("@ClassID"));

Parameters["@SchoolYear"].Value = 2006;

}


#region Events

protected override void OnLoad(EventArgs e)

{

base.OnLoad (e);


// The last state of controls from viewstate is now loaded (Before the
client data is processed)

}


protected override void OnPreRender(EventArgs e)

{

base.OnPreRender (e);

// The controls now have the client values from the the last postback

// ddlFundingSource.Visible = FundingSourceVisible;

// ddlProviders.Visible = ProviderVisible;

// ddlSites.Visible = SiteVisible;

// ddlClasses.Visible = ClassVisible;

// ddlSites.Items.Add(new ListItem("help","1"));

// ddlSites.Items.Add(new ListItem("me","2"));

// ddlSites.Visible = true;

}

private void rysReportingYearSelector_ReportingYearChanged(object sender,
EventArgs e)

{

// this.LoadFundingSource();

// this.LoadProviders();

}

private void fasFiscalAgent_FiscalAgentChanged(object sender, EventArgs e)

{

this.LoadFundingSource();

this.LoadProviders();

}

private void ddlFundingSource_SelectedIndexChanged(object sender, EventArgs
e)

{

LoadSites();

}

private void ddlProviders_SelectedIndexChanged(object sender, EventArgs e)

{

LoadSites();

}

private void ddlSites_SelectedIndexChanged(object sender, EventArgs e)

{

LoadClasses();

}

#endregion

protected override void CreateChildControls()

{

this.ddlFundingSource = new DropDownList();

this.ddlProviders = new DropDownList();

this.ddlSites = new DropDownList();

this.ddlClasses = new DropDownList();

ddlFundingSource.ID = FundingSourceControlId;

ddlProviders.ID = ProvidersControlId;

ddlSites.ID = SitesControlId;

ddlClasses.ID = ClassesControlId;


LoadProviders();

// start containing table

this.Controls.Add(WebHelper.MakeLiteral("<table cellpadding=0
cellspacing=0><tr><td>"));


this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>"));

// this.Controls.Add(rysReportingYearSelector);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

this.Controls.Add(WebHelper.MakeLiteral("</td></tr><tr><td>"));

this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>"));

// this.Controls.Add(fasFiscalAgent);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

this.Controls.Add(WebHelper.MakeLiteral("</td></tr><tr><td>"));

this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>Funding
Source</td><td>"));

this.Controls.Add(ddlFundingSource);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

this.Controls.Add(WebHelper.MakeLiteral("</td></tr><tr><td>"));

this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>Providers</td><td>")
);

this.Controls.Add(ddlProviders);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

this.Controls.Add(WebHelper.MakeLiteral("</td></tr><tr><td>"));

this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>Sites</td><td>"));

this.Controls.Add(ddlSites);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

this.Controls.Add(WebHelper.MakeLiteral("</td></tr><tr><td>"));

this.Controls.Add(WebHelper.MakeLiteral("<table><tr><td>Classes</td><td>"));

this.Controls.Add(ddlClasses);

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

// end containing table

this.Controls.Add(WebHelper.MakeLiteral("</td></tr></table>"));

ddlFundingSource.SelectedIndexChanged += new
EventHandler(ddlFundingSource_SelectedIndexChanged);

ddlProviders.SelectedIndexChanged += new
EventHandler(ddlProviders_SelectedIndexChanged);

ddlSites.SelectedIndexChanged += new
EventHandler(ddlSites_SelectedIndexChanged);

ddlFundingSource.AutoPostBack = ddlProviders.AutoPostBack =
ddlSites.AutoPostBack = true;


}

private void LoadFundingSource()

{

int fiscalAgentId = 1;//this.fasFiscalAgent.SelectedFiscalAgentId;


if(fiscalAgentId != int.MinValue)

{

FundingSourceVisible = true;

this.ddlFundingSource.DataSource = FiscalAgentFunding.Find(fiscalAgentId);

this.ddlFundingSource.DataTextField = "ShortDescription";

this.ddlFundingSource.DataValueField = "CodeId";

this.ddlFundingSource.DataBind();

if(this.ddlFundingSource.Items.Count > 1)

this.ddlFundingSource.Items.Insert(0, new ListItem(string.Empty,
string.Empty));

}

else

{

FundingSourceVisible = false;

}

}

private void LoadProviders()

{

int fiscalAgentId = 1;//fasFiscalAgent.SelectedFiscalAgentId;

if(fiscalAgentId != int.MinValue)

{

// ddlSites.Items.Add(new ListItem("help","1"));

// ddlSites.Items.Add(new ListItem("me","2"));



ProviderVisible = true;

// are these dates correct???????????????????????????

ddlProviders.DataSource = FiscalAgentProvider.Find(fiscalAgentId, new
DateTime(2005,7,1), new
DateTime(2006,6,30));//rysReportingYearSelector.ReportingYearStartDate,
rysReportingYearSelector.ReportingYearEndDate); //

ddlProviders.DataTextField = "ProviderName";

ddlProviders.DataValueField = "ProviderId";

ddlProviders.DataBind();

ddlProviders.Items.Insert(0, new ListItem(string.Empty, string.Empty));

}

else

{

this.ddlProviders.Items.Clear();

ProviderVisible = false;

}

// LoadSites();

}

private void LoadSites()

{

if(ddlProviders.SelectedValue != string.Empty)

{

// Load the Site search parameters.

SiteFindArgs siteFindArgs=new SiteFindArgs();

siteFindArgs.FiscalAgentId=1;
//Convert.ToInt32(fasFiscalAgent.SelectedFiscalAgentId);

SiteVisible = true;

ddlSites.DataSource = Business.Site.Find(siteFindArgs);

ddlSites.DataTextField = "Name";

ddlSites.DataValueField = "SiteId";

ddlSites.DataBind();

ddlSites.Items.Insert(0, new ListItem(string.Empty, string.Empty));

}

else

{

ddlSites.Items.Clear();

SiteVisible = false;

}

// LoadClasses();

}

private void LoadClasses()

{

if(ddlSites.SelectedValue != string.Empty)

{

AdultEdClassFindArgs adultEdClassFindArgs = new AdultEdClassFindArgs();

// adultEdClassFindArgs.FiscalAgentId =
Convert.ToInt32(fasFiscalAgent.SelectedFiscalAgentId);

// adultEdClassFindArgs.ReportingYearStartDate =
rysReportingYearSelector.ReportingYearStartDate;

// adultEdClassFindArgs.ReportingYearEndDate =
rysReportingYearSelector.ReportingYearEndDate;

// adultEdClassFindArgs.ProviderName = ddlProviders.SelectedValue;

// adultEdClassFindArgs.SiteName = ddlSites.SelectedValue;


ClassVisible = true;

ddlClasses.DataSource = AdultEdClass.Find(adultEdClassFindArgs);

ddlClasses.DataTextField = "Name";

ddlClasses.DataValueField = "ClassId";

ddlClasses.DataBind();

ddlClasses.Items.Insert(0, new ListItem(string.Empty, string.Empty));

}

else

{

ddlClasses.Items.Clear();

ClassVisible = false;

}

}



#region Public Properties

#region IReportParameterControl Members

public object ParameterValue

{

get{ return null; }

set{ /*do nothing */ }

}

public ParameterCollection Parameters

{

get{ return parameters; }

set{ parameters = value;}

}

#endregion

protected bool FundingSourceVisible

{

get

{

if(ViewState["FundingSourceVisible"] == null)

{

return false;

}

return (bool)ViewState["FundingSourceVisible"];

}

set

{

TrackViewState();

ViewState["FundingSourceVisible"] = value;

}

}

protected bool ProviderVisible

{

get

{

if(ViewState["ProviderVisible"] == null)

{

return false;

}

return (bool)ViewState["ProviderVisible"];

}

set

{

TrackViewState();

ViewState["ProviderVisible"] = value;

}

}

protected bool SiteVisible

{

get

{

if(ViewState["SiteVisible"] == null)

{

return false;

}

return (bool)ViewState["SiteVisible"];

}

set

{

TrackViewState();

ViewState["SiteVisible"] = value;

}

}

protected bool ClassVisible

{

get

{

if(ViewState["ClassVisible"] == null)

{

return false;

}

return (bool)ViewState["ClassVisible"];

}

set

{

TrackViewState();

ViewState["ClassVisible"] = value;

}

}

#endregion

#region Private Member Variables

private DropDownList ddlFundingSource;

private DropDownList ddlProviders;

private DropDownList ddlSites;

private DropDownList ddlClasses;

private ParameterCollection parameters = new ParameterCollection();

#endregion

#region Private Constants

private const string ReportingYearSelectorControlId =
"rysReportingYearSelector";

private const string FiscalAgentSelectorControlId =
"fasFiscalAgentSelector";

private const string FundingSourceControlId = "ddlFundingSource";

private const string FundingSourceLabelControlId = "lblFundingSource";

private const string ProvidersControlId = "ddlProviders";

private const string ProvidersLabelControlId = "lblProviders";

private const string SitesControlId = "ddlSites";

private const string SitesLabelControlId = "lblSites";

private const string ClassesControlId = "ddlClasses";

private const string ClassesLabelControlId = "lblClasses";

#endregion

}

}


"Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:2rQIZ7$lFHA.940@xxxxxxxxxxxxxxxxxxxxxxxx
> Hi TS,
>
> See you again :-), seems you're rushing in a asp.net project these days?
> For the question you mentioned in this post, here are some of my
> understanding and suggestions:
>
> 1. ASP.NET controls derived from Control will automatically maintain its
> ViewStates according to the asp.net web page's events sequence. So for
> composite control, those nested sub Controls' status (properties which be
> persistd in Viewsstate ) will be store and retrieve automatically.
>
> 2. However, there're some thing we need to care when building composite
> control:
> #remember to implement INamingContainer for controls which will have
nested
> sub controls. Otherwise, even handler mapping, ViewState loading will
occur
> unexpectedly.
>
> #Do remember to assign a explicit ID for each sub controls(same reason as
> #1). Also, please always try best to add subcontrols in the
> "CreateChildControls" method(just create control hierarchy) and put
> manipulating code in postback event or PreRender event.
>
> In addition, for your detaile scenario, I've just built a very simple demo
> control which have three dropdownlists and The "Top" one will display
> first(other twos invisible) and according to the top one's selection, the
> "Mid" dropdownlist will be pouplated and the same when the "mid"'s
> selection changed......
>
> Here's the control's code for your reference:
>
> =========================
> [DefaultProperty("Text"),
> ToolboxData("<{0}:MultiListControl runat=server></{0}:MultiListControl>")]
> public class MultiListControl : System.Web.UI.WebControls.WebControl,
> INamingContainer
> {
> private string text;
>
> private DropDownList lstTop;
> private DropDownList lstMid;
> private DropDownList lstBot;
>
>
>
>
>
> protected bool MidVisible
> {
> get{
> if(ViewState["MID_VISIBLE"] == null)
> {
> return false;
> }
>
> return (bool)ViewState["MID_VISIBLE"];
> }
>
> set{
> TrackViewState();
> ViewState["MID_VISIBLE"] = value;
> }
> }
>
> protected bool BotVisible
> {
> get
> {
> if(ViewState["BOT_VISIBLE"] == null)
> {
> return false;
> }
>
> return (bool)ViewState["BOT_VISIBLE"];
> }
>
> set
> {
> TrackViewState();
> ViewState["BOT_VISIBLE"] = value;
> }
> }
>
>
> [Bindable(true),
> Category("Appearance"),
> DefaultValue("")]
> public string Text
> {
> get
> {
> return text;
> }
>
> set
> {
> text = value;
> }
> }
>
>
> protected override void CreateChildControls()
> {
> Controls.Clear();
>
> Controls.Add(
> new LiteralControl(
> @"
> <table width='100%'>
> <tr><td>
> "));
>
> lstTop = new DropDownList();
> lstTop.ID = "lstTop";
>
> lstTop.Items.Add("----------");
> lstTop.Items.Add("Top_Item_1");
> lstTop.Items.Add("Top_Item_2");
> lstTop.Items.Add("Top_Item_3");
> lstTop.Items.Add("Top_Item_4");
>
> Controls.Add(lstTop);
>
> Controls.Add(
> new LiteralControl(
> @"<br/>"
> ));
>
> lstMid = new DropDownList();
> lstMid.ID = "lstMid";
>
> Controls.Add(lstMid);
>
> Controls.Add(
> new LiteralControl(
> @"<br/>"
> ));
>
>
> lstBot = new DropDownList();
> lstBot.ID = "lstBot";
>
> Controls.Add(lstBot);
>
>
> Controls.Add(
> new LiteralControl(
> @"
> </td></tr>
> </table>
> "
> ));
>
>
>
> lstTop.SelectedIndexChanged +=new EventHandler(lst_SelectedIndexChanged);
> lstMid.SelectedIndexChanged +=new EventHandler(lst_SelectedIndexChanged);
> lstBot.SelectedIndexChanged +=new EventHandler(lst_SelectedIndexChanged);
>
> lstTop.AutoPostBack = lstMid.AutoPostBack = lstBot.AutoPostBack = true;
>
>
> }
>
>
> protected override void OnPreRender(EventArgs e)
> {
> base.OnPreRender (e);
>
> lstMid.Visible = MidVisible;
> lstBot.Visible = BotVisible;
> }
>
>
>
>
> private void lst_SelectedIndexChanged(object sender, System.EventArgs e)
> {
>
> DropDownList lst = sender as DropDownList;
>
> switch(lst.ID)
> {
> case "lstTop":
>
> if(lst.SelectedIndex != 0)
> {
> lstMid.DataSource = GetSubItems(lst.SelectedValue);
> lstMid.DataTextField = "Text";
> lstMid.DataValueField= "Value";
> lstMid.DataBind();
>
> MidVisible = true;
> }
> else
> {
> MidVisible = BotVisible = false;
> }
>
> break;
> case "lstMid":
>
> lstBot.DataSource = GetSubItems(lst.SelectedValue);
> lstBot.DataTextField = "Text";
> lstBot.DataValueField= "Value";
> lstBot.DataBind();
>
> MidVisible = BotVisible = true;
>
> break;
> case "lstBot":
>
>
>
> break;
> }
>
> Page.Response.Write("<br>" + lst.ID + "_selectedindexchanged!" +
> lst.EnableViewState);
> }
>
> #region --Helper functions---
>
> public ListItemCollection GetSubItems(string parent)
> {
> ListItemCollection items = new ListItemCollection();
> int count = parent.Length;
>
> for(int i=0;i<count;++i)
> {
> items.Add(parent + "_Item_" + i);
> }
>
> return items;
> }
>
>
> #endregion
> }
>
> ========================
> Hope helps. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
> --------------------
> | From: "TS" <manofsteele1@xxxxxxxxxxxxx>
> | References: <uzUvXf6lFHA.1372@xxxxxxxxxxxxxxxxxxxx>
> | Subject: Re: explanation of when need to repopulate control
> | Date: Tue, 2 Aug 2005 18:32:23 -0500
> | Lines: 39
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> | Message-ID: <uRDywp7lFHA.3552@xxxxxxxxxxxxxxxxxxxx>
> | Newsgroups:
>
microsoft.public.dotnet.framework.aspnet.webcontrols,microsoft.public.dotnet
> framework.aspnet.buildingcontrols
> | NNTP-Posting-Host: 103nat100.tea.state.tx.us 198.214.103.100
> | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
> | Xref: TK2MSFTNGXA01.phx.gbl
> microsoft.public.dotnet.framework.aspnet.buildingcontrols:3979
> microsoft.public.dotnet.framework.aspnet.webcontrols:10243
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
> |
> | sorry, heres the scenario i'm trying to accomplish:
> | I have a composite control that contains 4 drop down lists. When the
page
> | loads initially, i want the first drop down filled and the rest
invisible.
> | When you select an item in this ddl, it posts back to the server and
based
> | on its value, it populates its immediate child's drop down list. So now
> the
> | top ddl has a value selected and the 2nd one just has its items
populated.
> | Then when the 2nd drop down list gets selected, it posts to the server
and
> | its value is used to populate(filter) the items for the 3rd drop down
> | list...and so on for each drop down list.
> |
> | Please tell me what i need to do to handle post back data and maintain
> state
> | from one postback to another while keeping the drop downlists filled and
> | their values persisted.
> |
> | thank you again!
> |
> |
> | "TS" <manofsteele1@xxxxxxxxxxxxx> wrote in message
> | news:uzUvXf6lFHA.1372@xxxxxxxxxxxxxxxxxxxxxxx
> | > I have a quesiton:
> | > if i have a composite control and on its intial page loading, i fill
my
> | (sub
> | > control) drop down list's items collection from the database and
return.
> | > When the user hits a button to cause postback, the control is going to
> get
> | > initialized, then does its items collection that i filled on the
initial
> | > page request get repopulated from viewstate? And on top of that, if
so,
> | does
> | > the list item that person selected in the drop down list again set
> itself
> | as
> | > the selected item in the list?
> | >
> | > OR do i have to re-load the items on every page request and then
> populate
> | > its value some other way???
> | >
> | > thanks a bunch
> | >
> | >
> |
> |
> |
>


.