Disappearing usercontrol

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Merwitz

    Disappearing usercontrol

    I am using usercontrols to control the contant in my website.
    Currently, depending on a parameter passed in the querystring, the
    appropriate usercontrol will be loaded.

    i.e.
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    if(!Page.IsPost Back)
    {
    Switch(Request. Params["P"])
    {
    case "1" :
    Control uc = LoadControl("/controls/control1.ascx") ;
    pnlContent.Cont rols.Add(uc);
    break;
    case "2" :
    Control uc = LoadControl("/controls/control2.ascx") ;
    pnlContent.Cont rols.Add(uc);
    break;
    }
    }
    }

    But now, when one of these control calls an internal method, the page posts
    back and the control disappears.
    I understand this i because the control is loaded through the code and is
    not loaded again in the postback.

    I have solved this problem by keeping the control in a session during post
    backs, but am not completely sure if this is the right way to do things.

    I have also tried to viewstate the control, but get the error:
    The type 'ASP.control1_a scx' must be marked as Serializable or have a
    TypeConverter other than ReferenceConver ter to be put in viewstate.
    I have further tried to mark the control as Serializable which didn't seem
    to help.

    Can anyone suggest a better way of doing this, or a solution

    Thanks

    Grant

    P.S. the control is not called control1.ascx, i just renamed it for this
    example.


  • Karl Seguin

    #2
    Re: Disappearing usercontrol

    Typically you store the name of the control in the session or viewstate and
    reuse that... something like:


    string controlName = null;

    if (!Page.IsPostBa ck){
    switch(){
    case "1":
    controlName = "/controls/control1.ascx";
    break;
    case "2"
    controlName = "/controls/control2.ascx";
    break;
    }
    pnlContent.Cont rols.Add(Page.L oadControl(cont rolName));
    ViewState.Add(" lastControl", controlName);
    }ELSE{
    controlName = (string)ViewSta te["lastContro l"];
    if (controlName != null){
    pnlContent.Cont rols.Add(Page.L oadControl(cont rolName));
    }
    }


    Denis Bauer has an PlaceHolderCont rol which takes care of this, check it
    out:

    (free)

    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Grant Merwitz" <grant@workshar e.com> wrote in message
    news:OizGh5rNFH A.3988@tk2msftn gp13.phx.gbl...[color=blue]
    > I am using usercontrols to control the contant in my website.
    > Currently, depending on a parameter passed in the querystring, the
    > appropriate usercontrol will be loaded.
    >
    > i.e.
    > private void Page_Load(objec t sender, System.EventArg s e)
    > {
    > if(!Page.IsPost Back)
    > {
    > Switch(Request. Params["P"])
    > {
    > case "1" :
    > Control uc = LoadControl("/controls/control1.ascx") ;
    > pnlContent.Cont rols.Add(uc);
    > break;
    > case "2" :
    > Control uc = LoadControl("/controls/control2.ascx") ;
    > pnlContent.Cont rols.Add(uc);
    > break;
    > }
    > }
    > }
    >
    > But now, when one of these control calls an internal method, the page[/color]
    posts[color=blue]
    > back and the control disappears.
    > I understand this i because the control is loaded through the code and is
    > not loaded again in the postback.
    >
    > I have solved this problem by keeping the control in a session during post
    > backs, but am not completely sure if this is the right way to do things.
    >
    > I have also tried to viewstate the control, but get the error:
    > The type 'ASP.control1_a scx' must be marked as Serializable or have a
    > TypeConverter other than ReferenceConver ter to be put in viewstate.
    > I have further tried to mark the control as Serializable which didn't seem
    > to help.
    >
    > Can anyone suggest a better way of doing this, or a solution
    >
    > Thanks
    >
    > Grant
    >
    > P.S. the control is not called control1.ascx, i just renamed it for this
    > example.
    >
    >[/color]


    Comment

    • Grant Merwitz

      #3
      Re: Disappearing usercontrol

      Brilliant, Thank You

      So even if i'm reloading the control, the post back still holds strong.
      I was under the impresson that if i had to reload the control, it would load
      as if it were loading for the first time.
      Hence why i tried to viewstate the whole control.

      So i guess it all goes down to understanding how the .net page loads, and
      that it will load the control, before attempting to do the postback within
      the control.

      Very interesting and thanks for your help


      "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
      wrote in message news:OJUdvDsNFH A.2748@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Typically you store the name of the control in the session or viewstate
      > and
      > reuse that... something like:
      >
      >
      > string controlName = null;
      >
      > if (!Page.IsPostBa ck){
      > switch(){
      > case "1":
      > controlName = "/controls/control1.ascx";
      > break;
      > case "2"
      > controlName = "/controls/control2.ascx";
      > break;
      > }
      > pnlContent.Cont rols.Add(Page.L oadControl(cont rolName));
      > ViewState.Add(" lastControl", controlName);
      > }ELSE{
      > controlName = (string)ViewSta te["lastContro l"];
      > if (controlName != null){
      > pnlContent.Cont rols.Add(Page.L oadControl(cont rolName));
      > }
      > }
      >
      >
      > Denis Bauer has an PlaceHolderCont rol which takes care of this, check it
      > out:
      > http://www.denisbauer.com/ASPNETCont...aceholder.aspx
      > (free)
      >
      > Karl
      > --
      > MY ASP.Net tutorials
      > http://www.openmymind.net/ - New and Improved (yes, the popup is
      > annoying)
      > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
      > come!)
      > "Grant Merwitz" <grant@workshar e.com> wrote in message
      > news:OizGh5rNFH A.3988@tk2msftn gp13.phx.gbl...[color=green]
      >> I am using usercontrols to control the contant in my website.
      >> Currently, depending on a parameter passed in the querystring, the
      >> appropriate usercontrol will be loaded.
      >>
      >> i.e.
      >> private void Page_Load(objec t sender, System.EventArg s e)
      >> {
      >> if(!Page.IsPost Back)
      >> {
      >> Switch(Request. Params["P"])
      >> {
      >> case "1" :
      >> Control uc = LoadControl("/controls/control1.ascx") ;
      >> pnlContent.Cont rols.Add(uc);
      >> break;
      >> case "2" :
      >> Control uc = LoadControl("/controls/control2.ascx") ;
      >> pnlContent.Cont rols.Add(uc);
      >> break;
      >> }
      >> }
      >> }
      >>
      >> But now, when one of these control calls an internal method, the page[/color]
      > posts[color=green]
      >> back and the control disappears.
      >> I understand this i because the control is loaded through the code and is
      >> not loaded again in the postback.
      >>
      >> I have solved this problem by keeping the control in a session during
      >> post
      >> backs, but am not completely sure if this is the right way to do things.
      >>
      >> I have also tried to viewstate the control, but get the error:
      >> The type 'ASP.control1_a scx' must be marked as Serializable or have a
      >> TypeConverter other than ReferenceConver ter to be put in viewstate.
      >> I have further tried to mark the control as Serializable which didn't
      >> seem
      >> to help.
      >>
      >> Can anyone suggest a better way of doing this, or a solution
      >>
      >> Thanks
      >>
      >> Grant
      >>
      >> P.S. the control is not called control1.ascx, i just renamed it for this
      >> example.
      >>
      >>[/color]
      >
      >[/color]


      Comment

      Working...