Hello ,

I read artical listed on link
http://www.tgreer.com/aspnet_html_04.html

What I am doing is.

on aspx page
1)There are two dropdown list on my .aspx page
2) One ddl ( drop down list) is populated on change of another one
3) One place holder to accomodate dynamic controls

On change of runtime populated ddl i am setting one session value to load which dynamic set of controls. This set of controls i read from XML and match with XSLT and display it.

dynamic set of user control's load mechenism reside in ascx ( User control) file.

There is one button i am adding runtime on the page load of ascx ( User control)

Every thing work fine first time and i get all the post back values on button click as well. when i change option in ddl . It load dynamic set of diffrenet controls fine as well.but button click lost. Intresting to know is if i click second time on button it works fine.

Code:

Code on .aspx page
==============
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if ( ViewState["mediaType"] != null)
{
if( ViewState["mediaType"].ToString() != "")
{
plControls.Controls.Clear();
plControls.Controls.Add(LoadControl("ucMediaXMLPar se.ascx"));

}
}
}

// On this index change i am loading another set of controls using XML
#region "Media Type Index Change"
private void cmbmedia_SelectedIndexChanged(object sender, EventArgs e)
{
if(cmbmedia.SelectedValue != null && cmbmedia.SelectedValue !="")
{
Session.Add ("mediaType",cmbmedia.SelectedValue.ToString()) ;
ViewState["mediaType"] = int.Parse(cmbmedia.SelectedValue);
plControls.Controls.Clear();
plControls.Controls.Add(LoadControl("ucMediaXMLPar se.ascx"));
plControls.Visible = true;
}
}
#endregion

On ascx ( user control)
===================
comments:
CreateControlsUsingXSLT method will match template with xml file and make controls
====================

#region "Page Load"
private void Page_Load(object sender, System.EventArgs e)
{
if ( Session["mediaType"] != null)
{
if( Session["mediaType"].ToString() != "")
{
Controls.Clear();
CreateControlsUsingXSLT(int.Parse(Session
["mediaType"].ToString()));

// this button click event has been lost
Button submitButton = new Button();
submitButton.Text= "Submit Me";
submitButton.ID= "btnSubmit";
submitButton.Click +=new EventHandler
(submitButton_Click);
Controls.Add(submitButton);
}
}
}
#endregion

private void CreateControlsUsingXSLT(int mediatype)
{
// mach with template and parse
// parse the controls and add it to the page
System.Web.UI.Control ctrlParsedControls = ParseControl(result);
Controls.Add(ctrlParsedControls);
}

It is because everytime you reload all the controls everytime - every postback.
Remember the pageload etc is fired everytime you do something and is fired before the button click event, or selected change etc. so be very careful where you want things to run (ie choose your events - page load, init, loadviewstate etc - wisely) and also decide whether you want it to run every time or just on postback or not (use the Request.IsPostBack to check for this). You can always move parts or all of your function out of the event (i would not code in events either, just call them from events - thats good programming practice for web and windows apps) so you have full control.
I am not 100% sure what you want to do when so cant redo your code for you, but can give you pointers. Use the IsPostback and check the events you are using and whether it is the right one. 90% of unexpected results in apps are usually because the wrong event is chosen. Events are so similar so you have to understand them all to choose the right one (a simple example is the mousedown, mouseup and mouseclick - putting the same code in different events can have different results).

Thanks F1 fan,

My problem was solved when i have transfer all the things to only one user controls. I mean i have transfered my both combos to usercontrol.

Steps i have taken care
1) put one place holder on aspx page to handle user control
2) transered both combos to usercontrol ascx
3) put one place holder on ascx (user control ) to hold the dynamically generated controls through xml and xslt
4) put on submit buttion on ascx and hide and display as per need.
5) transfer loadviewstate overridden function to ascx as well so on my aspx page there is only one place holder and on page load event of aspx i am loading user control.

any way thanks for your help.

Hint

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.