| | |
Button event lost..
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
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);
}
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);
}
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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).
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).
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Button click event only working the second time (C#)
- A simple question : How to enable/disable a button at runtime (C#)
- Reset and Calculation Buttons (Java)
- event code not working (Java)
- how to call javabean from jsp page upon onChange event (JSP)
- Gettig delphi to wait for a button press before continuing (Pascal and Delphi)
Other Threads in the ASP.NET Forum
- Previous Thread: Need help in project
- Next Thread: Inserting Images to a Datagrid
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox child class compatible complex content contenttype control countryselector courier database datagrid datagridview datalist deployment development dgv dialog dropdown dropdownmenu dynamic dynamically edit editing embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv folder form gridview gudi iis image javascript languages list maps menu mobile mssql nameisnotdeclared novell opera order parent problem ratings redirect refer registration relationaldatabases reportemail response.redirect rows search security select serializesmo.table sessionvariables silverlight smoobjects software sql ssl tracking treeview typeof validatedate validation vb.net vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming wizard xsl





