Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

click event not firing

Status
Not open for further replies.

ryandale

IS-IT--Management
Dec 1, 2004
9
US
I'm having a problem dynamically loading controls. I have a page with a placeholder. There are 3 different controls ctl1, ctl2, ctl3 that I want to put in the placeholder (1 at a time, depending upon the value of a session variable called sessVar).

in ctl1 there is a submit button that when clicked it will change the value of sessVar. so i would like the corresponding control to load then.

here is the code to dynamically load the control:


switch (sessVar)
{
case 1:
PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlFilters.ascx"));
break;
case 2:
PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlResults.ascx"));
break;
case 3:
PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlPropDetails.ascx"));
break;
default:
sm.sSearchStage = 1;
PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlFilters.ascx"));
break;
}



the problem is that i cant figure out where to put the above code. if i put it in Page_Load() then the control is loaded before the click event in ctl1, so i have to refresh or click the button twice for ctl2 to load. if i put the above code in PreRender() then the button event does not even fire (which i don't understand).


i'm having the same problem as being mentioned here:


Thanks all,
Ryan
 
It's best to dynamically load controls on Init if possible.

How about this:

Code:
//On_Init
if( !Page.IsPostBack )
{
    LoadControl( (string)Session["Key"] );
}

//on button click
string controlName = "fill in";

Session["Key"] = controlName;
LoadControl( controlName );

//have a function
private void LoadControl( string controlName )
{
    PlaceHolder1.Controls.Clear();

    switch (controlName)
            {
                case "Filters":
                    PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlFilters.ascx"));
                    break;
                case "Results":
                    PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlResults.ascx"));
                    break;
                case "PropDetails":
                    PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlPropDetails.ascx"));
                    break;
                default:
                    sm.sSearchStage = 1;
                    PlaceHolder1.Controls.Add(LoadControl("Controls/PropertySearch/ctlFilters.ascx"));
                    break;
            }
}
 
well that is essentially the same thing. the page_init executes before my button_click event. and my button_click event is what modifies the variable i need to read in to decide which control to be displayed.....

do you need me to post all my code?
 
No.

To refactor, however, you probably want to have Init always add the control, and it may be better to set the Session variable in LoadControl().

i.e.
Code:
//in init
LoadControl( Session["Key"] as string );

//in button click
LoadControl( "derived name" );

//function
private void LoadControl( string controlName )
{
    if( controlName == null )
       return;

    Placeholder1.Controls.Clear();

    Session["Key"] = controlName;
 
    //load control
}

That simple arrangement should suit your situation, but if it doesn't for some reason, please drop a note.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top