Groups | Blog | Home
all groups > asp.net building controls > august 2005 >

asp.net building controls : explanation of when need to repopulate control


TS
8/2/2005 4:19:16 PM
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

TS
8/2/2005 5:00:24 PM
My control's sub control (dropdownlist) doesn't have its items collection
re-populated from viewstate. I also have a textbox that doesn't have its
value persisted either (Well sort of. In the load event, i access the
textbox and its value is blank, but when the control comes back to the user,
the value i entered in that textbox is there!!!???)

sorry for the large amount of code, but here it goes, again.
using System;

using System.Collections.Specialized;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Text;

using Operations.Teams.Business;

using Operations.Teams.Data;

using Operations.Teams.Reporting;

using Operations.Teams.Reporting.WebControls;

namespace Operations.Teams.Web.ReportControls

{

/// <summary>

/// Summary description for FiscalAgentHierarchy.

/// </summary>

public class FiscalAgentHierarchy : Control, 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 OnInit(EventArgs e)

{

EnsureChildControls();

if(this.fasFiscalAgent.SelectedFiscalAgentName != string.Empty)

{

if(!Page.IsPostBack)

{

LoadFundingSource();

LoadProviders();

}

}

base.OnInit (e);

}

protected override void OnLoad(EventArgs e)

{

// The controls should now be in the control tree and its client values
should be in it???

base.OnLoad (e);

}



private void rysReportingYearSelector_ReportingYearChanged(object sender,
EventArgs e)

{

this.SchoolYear = rysReportingYearSelector.SchoolYear;

this.ReportingYearGroupID = rysReportingYearSelector.ReportYearGroupId;

this.LoadFundingSource();

this.LoadProviders();

}

private void fasFiscalAgent_FiscalAgentChanged(object sender, EventArgs e)

{

this.FiscalAgentID = fasFiscalAgent.SelectedFiscalAgentId;

this.LoadFundingSource();

this.LoadProviders();

}

private void ddlProviders_SelectedIndexChanged(object sender, EventArgs e)

{

if(ddlProviders.SelectedValue != string.Empty)

this.ProviderID = Convert.ToInt32(ddlProviders.SelectedValue);

LoadSites();

}

private void ddlSites_SelectedIndexChanged(object sender, EventArgs e)

{

if(ddlSites.SelectedValue != string.Empty)

this.SiteID = Convert.ToInt32(ddlSites.SelectedValue);

LoadClasses();

}

#endregion

TextBox box = new TextBox();

protected override void CreateChildControls()

{

box.ID = "box";

if(!Page.IsPostBack)

box.Text = "hello";

Controls.Add(box);



rysReportingYearSelector = new ReportingYearReportSelector();

fasFiscalAgent = new FiscalAgentReportSelector();

this.ddlFundingSource = new DropDownList();

this.ddlProviders = new DropDownList();

this.ddlSites = new DropDownList();

this.ddlClasses = new DropDownList();

rysReportingYearSelector.ID = ReportingYearSelectorControlId;

fasFiscalAgent.ID = FiscalAgentSelectorControlId;

ddlFundingSource.ID = FundingSourceControlId;

ddlProviders.ID = ProvidersControlId;

ddlSites.ID = SitesControlId;

ddlClasses.ID = ClassesControlId;

// Assign these controls' Page property because when they try to access Page
they get null reference - apparently they aren't in the control tree at that
point

rysReportingYearSelector.Page = this.Page;

fasFiscalAgent.Page = this.Page;


rysReportingYearSelector.ReportingYearChanged += new
EventHandler(rysReportingYearSelector_ReportingYearChanged);

// fasFiscalAgent.FiscalAgentChanged += new
EventHandler(fasFiscalAgent_FiscalAgentChanged);

ddlProviders.SelectedIndexChanged += new
EventHandler(ddlProviders_SelectedIndexChanged);

ddlSites.SelectedIndexChanged += new
EventHandler(ddlSites_SelectedIndexChanged);

fasFiscalAgent.IsSubmitOnChange = true;

ddlProviders.AutoPostBack = true;

ddlSites.AutoPostBack = true;


this.SchoolYear = rysReportingYearSelector.SchoolYear;

// check if can populate funding sources and providers (because fiscalAgent
has been saved in session)

// if(this.fasFiscalAgent.SelectedFiscalAgentName != string.Empty)

// {

// if(!Page.IsPostBack)

// {

// LoadFundingSource();

// LoadProviders();

// }

// }

// else

// {

// ddlFundingSource.Visible = false;

// ddlProviders.Visible = false;

// }

// Initially set to false until their direct parent's selection has been
made (all other controls will have data on page load)

ddlSites.Visible = false;

ddlClasses.Visible = false;

// 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>"));

}

private void LoadFundingSource()

{

int fiscalAgentId = this.fasFiscalAgent.SelectedFiscalAgentId;


if(fiscalAgentId != int.MinValue)

{

this.ddlFundingSource.Visible = true;

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

this.ddlFundingSource.DataTextField = "ShortDescription";

this.ddlFundingSource.DataValueField = "CodeId";

this.ddlFundingSource.DataBind();

TS
8/2/2005 6:32:23 PM
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!


[quoted text, click to view]

AddThis Social Bookmark Button