creating dynamic controls out of static controls

Discussion in 'ASP .Net' started by Tarscher, Feb 12, 2007.

  1. Tarscher

    Tarscher Guest

    hi all,

    I have this seemingly simple problem. I have lost a lot of time on it
    though.

    When a user selects a value from a dropdownlist (static control) a
    dynamic control is generated. I have to create the dynamic controls
    in the OnInit stage of the lifecycle. Since data from static controls
    is not yet available in the OnInit stage I can't know what dynamic
    control I have to create.

    How can I overcome this problem? Since this question has probably
    already been answered a good tutorial will also do of course. I have
    been looking on the net but no success.

    Thanks in advance
    Stijn
     
    Tarscher, Feb 12, 2007
    #1
    1. Advertising

  2. When you create controls first time, do it in Page_Load event or later. When
    you need to re-create them on postbacks, do it in PreInit event.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Tarscher" <> wrote in message
    news:...
    > hi all,
    >
    > I have this seemingly simple problem. I have lost a lot of time on it
    > though.
    >
    > When a user selects a value from a dropdownlist (static control) a
    > dynamic control is generated. I have to create the dynamic controls
    > in the OnInit stage of the lifecycle. Since data from static controls
    > is not yet available in the OnInit stage I can't know what dynamic
    > control I have to create.
    >
    > How can I overcome this problem? Since this question has probably
    > already been answered a good tutorial will also do of course. I have
    > been looking on the net but no success.
    >
    > Thanks in advance
    > Stijn
    >
     
    Eliyahu Goldin, Feb 12, 2007
    #2
    1. Advertising

  3. Tarscher

    Tarscher Guest

    Thanks Eliyah for the quick answer. Some moet questions tough.

    You say I have to recreate the controls on the PreInit even. But how
    can I know the state of the drop down list (a static control) in the
    PreInit stage? I can thus not create my dynamic controls since I don't
    know what's in the drop down list.

    Can you please explain me what I do when the dropdownlist
    SelectedIndexChanged had fired?

    Thanks
    Stijn

    On 12 feb, 15:34, "Eliyahu Goldin"
    <> wrote:
    > When you create controls first time, do it in Page_Load event or later. When
    > you need to re-create them on postbacks, do it in PreInit event.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > "Tarscher" <> wrote in message
    >
    > news:...
    >
    > > hi all,

    >
    > > I have this seemingly simple problem. I have lost a lot of time on it
    > > though.

    >
    > > When a user selects a value from a dropdownlist (static control) a
    > > dynamic control is generated. I have to create the dynamic controls
    > > in the OnInit stage of the lifecycle. Since data from static controls
    > > is not yet available in the OnInit stage I can't know what dynamic
    > > control I have to create.

    >
    > > How can I overcome this problem? Since this question has probably
    > > already been answered a good tutorial will also do of course. I have
    > > been looking on the net but no success.

    >
    > > Thanks in advance
    > > Stijn
     
    Tarscher, Feb 12, 2007
    #3
  4. Stijn,

    You need to re-create dynamic controls only if you are in a postback and the
    controls need to pickup their properties that arrive to the server in the
    same postback. In your case, you seem to create controls every postback from
    scratch depending on the selected ddl item. If it is the case, I don't see
    why can't you wait with creating controls till the stage where you already
    know what to create.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Tarscher" <> wrote in message
    news:...
    > Thanks Eliyah for the quick answer. Some moet questions tough.
    >
    > You say I have to recreate the controls on the PreInit even. But how
    > can I know the state of the drop down list (a static control) in the
    > PreInit stage? I can thus not create my dynamic controls since I don't
    > know what's in the drop down list.
    >
    > Can you please explain me what I do when the dropdownlist
    > SelectedIndexChanged had fired?
    >
    > Thanks
    > Stijn
    >
    > On 12 feb, 15:34, "Eliyahu Goldin"
    > <> wrote:
    >> When you create controls first time, do it in Page_Load event or later.
    >> When
    >> you need to re-create them on postbacks, do it in PreInit event.
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer & Consultant
    >> Microsoft MVP
    >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >>
    >> "Tarscher" <> wrote in message
    >>
    >> news:...
    >>
    >> > hi all,

    >>
    >> > I have this seemingly simple problem. I have lost a lot of time on it
    >> > though.

    >>
    >> > When a user selects a value from a dropdownlist (static control) a
    >> > dynamic control is generated. I have to create the dynamic controls
    >> > in the OnInit stage of the lifecycle. Since data from static controls
    >> > is not yet available in the OnInit stage I can't know what dynamic
    >> > control I have to create.

    >>
    >> > How can I overcome this problem? Since this question has probably
    >> > already been answered a good tutorial will also do of course. I have
    >> > been looking on the net but no success.

    >>
    >> > Thanks in advance
    >> > Stijn

    >
    >
     
    Eliyahu Goldin, Feb 12, 2007
    #4
  5. Tarscher

    Tarscher Guest

    Eliyahu, what i try:

    - On the SelectedIndexChanged event of the dropdownlist I read out the
    DDL data and create the appropriate controls
    protected void KeywordDropDownList_SelectedIndexChanged(object
    sender, EventArgs e)
    {
    string selectedKeyword =
    KeywordDropDownList.SelectedValue.ToString();
    DrawKeywordEditor(selectedKeyword);
    }
    - I then try to recreate the control in the Preinit stage but this
    seems to fail.
    base.OnPreInit(e);
    IKeywordEditor myIKE = EditorHolder.FindControl("KeywordEditor") as
    IKeywordEditor;
    The EditorHolder is not known in this page (it is in the init stage).
    When I recreate it in the Init stage I always get a null returned. The
    KeywordEditor control is thus not found.

    I'm probably missing something crucial here.

    Stijn


    On 12 feb, 16:51, "Eliyahu Goldin"
    <> wrote:
    > Stijn,
    >
    > You need to re-create dynamic controls only if you are in a postback and the
    > controls need to pickup their properties that arrive to the server in the
    > same postback. In your case, you seem to create controls every postback from
    > scratch depending on the selected ddl item. If it is the case, I don't see
    > why can't you wait with creating controls till the stage where you already
    > know what to create.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > "Tarscher" <> wrote in message
    >
    > news:...
    >
    > > Thanks Eliyah for the quick answer. Some moet questions tough.

    >
    > > You say I have to recreate the controls on the PreInit even. But how
    > > can I know the state of the drop down list (a static control) in the
    > > PreInit stage? I can thus not create my dynamic controls since I don't
    > > know what's in the drop down list.

    >
    > > Can you please explain me what I do when the dropdownlist
    > > SelectedIndexChanged had fired?

    >
    > > Thanks
    > > Stijn

    >
    > > On 12 feb, 15:34, "Eliyahu Goldin"
    > > <> wrote:
    > >> When you create controls first time, do it in Page_Load event or later.
    > >> When
    > >> you need to re-create them on postbacks, do it in PreInit event.

    >
    > >> --
    > >> Eliyahu Goldin,
    > >> Software Developer & Consultant
    > >> Microsoft MVP
    > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > >> "Tarscher" <> wrote in message

    >
    > >>news:...

    >
    > >> > hi all,

    >
    > >> > I have this seemingly simple problem. I have lost a lot of time on it
    > >> > though.

    >
    > >> > When a user selects a value from a dropdownlist (static control) a
    > >> > dynamic control is generated. I have to create the dynamic controls
    > >> > in the OnInit stage of the lifecycle. Since data from static controls
    > >> > is not yet available in the OnInit stage I can't know what dynamic
    > >> > control I have to create.

    >
    > >> > How can I overcome this problem? Since this question has probably
    > >> > already been answered a good tutorial will also do of course. I have
    > >> > been looking on the net but no success.

    >
    > >> > Thanks in advance
    > >> > Stijn
     
    Tarscher, Feb 12, 2007
    #5
  6. I just read this interchange, and I fear that you have created a flawed
    design. It's a case of "the cart before the horse" here. Better revisit your
    logic and see if you cannot find a less - complicated way to reach your goal.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Tarscher" wrote:

    > Eliyahu, what i try:
    >
    > - On the SelectedIndexChanged event of the dropdownlist I read out the
    > DDL data and create the appropriate controls
    > protected void KeywordDropDownList_SelectedIndexChanged(object
    > sender, EventArgs e)
    > {
    > string selectedKeyword =
    > KeywordDropDownList.SelectedValue.ToString();
    > DrawKeywordEditor(selectedKeyword);
    > }
    > - I then try to recreate the control in the Preinit stage but this
    > seems to fail.
    > base.OnPreInit(e);
    > IKeywordEditor myIKE = EditorHolder.FindControl("KeywordEditor") as
    > IKeywordEditor;
    > The EditorHolder is not known in this page (it is in the init stage).
    > When I recreate it in the Init stage I always get a null returned. The
    > KeywordEditor control is thus not found.
    >
    > I'm probably missing something crucial here.
    >
    > Stijn
    >
    >
    > On 12 feb, 16:51, "Eliyahu Goldin"
    > <> wrote:
    > > Stijn,
    > >
    > > You need to re-create dynamic controls only if you are in a postback and the
    > > controls need to pickup their properties that arrive to the server in the
    > > same postback. In your case, you seem to create controls every postback from
    > > scratch depending on the selected ddl item. If it is the case, I don't see
    > > why can't you wait with creating controls till the stage where you already
    > > know what to create.
    > >
    > > --
    > > Eliyahu Goldin,
    > > Software Developer & Consultant
    > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    > >
    > > "Tarscher" <> wrote in message
    > >
    > > news:...
    > >
    > > > Thanks Eliyah for the quick answer. Some moet questions tough.

    > >
    > > > You say I have to recreate the controls on the PreInit even. But how
    > > > can I know the state of the drop down list (a static control) in the
    > > > PreInit stage? I can thus not create my dynamic controls since I don't
    > > > know what's in the drop down list.

    > >
    > > > Can you please explain me what I do when the dropdownlist
    > > > SelectedIndexChanged had fired?

    > >
    > > > Thanks
    > > > Stijn

    > >
    > > > On 12 feb, 15:34, "Eliyahu Goldin"
    > > > <> wrote:
    > > >> When you create controls first time, do it in Page_Load event or later.
    > > >> When
    > > >> you need to re-create them on postbacks, do it in PreInit event.

    > >
    > > >> --
    > > >> Eliyahu Goldin,
    > > >> Software Developer & Consultant
    > > >> Microsoft MVP
    > > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    > >
    > > >> "Tarscher" <> wrote in message

    > >
    > > >>news:...

    > >
    > > >> > hi all,

    > >
    > > >> > I have this seemingly simple problem. I have lost a lot of time on it
    > > >> > though.

    > >
    > > >> > When a user selects a value from a dropdownlist (static control) a
    > > >> > dynamic control is generated. I have to create the dynamic controls
    > > >> > in the OnInit stage of the lifecycle. Since data from static controls
    > > >> > is not yet available in the OnInit stage I can't know what dynamic
    > > >> > control I have to create.

    > >
    > > >> > How can I overcome this problem? Since this question has probably
    > > >> > already been answered a good tutorial will also do of course. I have
    > > >> > been looking on the net but no success.

    > >
    > > >> > Thanks in advance
    > > >> > Stijn

    >
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 12, 2007
    #6
  7. Tarscher

    Tarscher Guest

    I assume that what I want to do has been done 1000 of times but maybe
    I implement it in the wrong way?

    What I want to do:
    A dropdownlist is populated with editornames. An editor is a user
    control. When the user makes a choice out of the DDL, the user control
    (editor) is rendered to the screen. I make the user control a dynamic
    control that I add to a placeholder. The fact that the user control
    needs to be dynamic is actually the only restrain. I really don't see
    how I can pass data to the dynamic control (editors).

    Someone has an idea on how to tackle this problem?

    Thanks
    Stijn


    On 12 feb, 18:05, Peter Bromberg [C# MVP]
    <> wrote:
    > I just read this interchange, and I fear that you have created a flawed
    > design. It's a case of "the cart before the horse" here. Better revisit your
    > logic and see if you cannot find a less - complicated way to reach your goal.
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
    >
    > "Tarscher" wrote:
    > > Eliyahu, what i try:

    >
    > > - On the SelectedIndexChanged event of the dropdownlist I read out the
    > > DDL data and create the appropriate controls
    > > protected void KeywordDropDownList_SelectedIndexChanged(object
    > > sender, EventArgs e)
    > > {
    > > string selectedKeyword =
    > > KeywordDropDownList.SelectedValue.ToString();
    > > DrawKeywordEditor(selectedKeyword);
    > > }
    > > - I then try to recreate the control in the Preinit stage but this
    > > seems to fail.
    > > base.OnPreInit(e);
    > > IKeywordEditor myIKE = EditorHolder.FindControl("KeywordEditor") as
    > > IKeywordEditor;
    > > The EditorHolder is not known in this page (it is in the init stage).
    > > When I recreate it in the Init stage I always get a null returned. The
    > > KeywordEditor control is thus not found.

    >
    > > I'm probably missing something crucial here.

    >
    > > Stijn

    >
    > > On 12 feb, 16:51, "Eliyahu Goldin"
    > > <> wrote:
    > > > Stijn,

    >
    > > > You need to re-create dynamic controls only if you are in a postback and the
    > > > controls need to pickup their properties that arrive to the server in the
    > > > same postback. In your case, you seem to create controls every postback from
    > > > scratch depending on the selected ddl item. If it is the case, I don't see
    > > > why can't you wait with creating controls till the stage where you already
    > > > know what to create.

    >
    > > > --
    > > > Eliyahu Goldin,
    > > > Software Developer & Consultant
    > > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > > > "Tarscher" <> wrote in message

    >
    > > >news:...

    >
    > > > > Thanks Eliyah for the quick answer. Some moet questions tough.

    >
    > > > > You say I have to recreate the controls on the PreInit even. But how
    > > > > can I know the state of the drop down list (a static control) in the
    > > > > PreInit stage? I can thus not create my dynamic controls since I don't
    > > > > know what's in the drop down list.

    >
    > > > > Can you please explain me what I do when the dropdownlist
    > > > > SelectedIndexChanged had fired?

    >
    > > > > Thanks
    > > > > Stijn

    >
    > > > > On 12 feb, 15:34, "Eliyahu Goldin"
    > > > > <> wrote:
    > > > >> When you create controls first time, do it in Page_Load event or later.
    > > > >> When
    > > > >> you need to re-create them on postbacks, do it in PreInit event.

    >
    > > > >> --
    > > > >> Eliyahu Goldin,
    > > > >> Software Developer & Consultant
    > > > >> Microsoft MVP
    > > > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > > > >> "Tarscher" <> wrote in message

    >
    > > > >>news:...

    >
    > > > >> > hi all,

    >
    > > > >> > I have this seemingly simple problem. I have lost a lot of time on it
    > > > >> > though.

    >
    > > > >> > When a user selects a value from a dropdownlist (static control) a
    > > > >> > dynamic control is generated. I have to create the dynamic controls
    > > > >> > in the OnInit stage of the lifecycle. Since data from static controls
    > > > >> > is not yet available in the OnInit stage I can't know what dynamic
    > > > >> > control I have to create.

    >
    > > > >> > How can I overcome this problem? Since this question has probably
    > > > >> > already been answered a good tutorial will also do of course. I have
    > > > >> > been looking on the net but no success.

    >
    > > > >> > Thanks in advance
    > > > >> > Stijn
     
    Tarscher, Feb 13, 2007
    #7
  8. Stijn,

    I personally hardly ever create any controls dynamically. Usually you can
    achieve the same with hidden static controls. Make only one of them visible
    at the time. If hide controls by setting Visible=false, they won't get
    rendered to the client which is exactly the same effect as not creating them
    in the first place.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Tarscher" <> wrote in message
    news:...
    >I assume that what I want to do has been done 1000 of times but maybe
    > I implement it in the wrong way?
    >
    > What I want to do:
    > A dropdownlist is populated with editornames. An editor is a user
    > control. When the user makes a choice out of the DDL, the user control
    > (editor) is rendered to the screen. I make the user control a dynamic
    > control that I add to a placeholder. The fact that the user control
    > needs to be dynamic is actually the only restrain. I really don't see
    > how I can pass data to the dynamic control (editors).
    >
    > Someone has an idea on how to tackle this problem?
    >
    > Thanks
    > Stijn
    >
    >
    > On 12 feb, 18:05, Peter Bromberg [C# MVP]
    > <> wrote:
    >> I just read this interchange, and I fear that you have created a flawed
    >> design. It's a case of "the cart before the horse" here. Better revisit
    >> your
    >> logic and see if you cannot find a less - complicated way to reach your
    >> goal.
    >> Peter
    >>
    >> --
    >> Site: http://www.eggheadcafe.com
    >> UnBlog: http://petesbloggerama.blogspot.com
    >> Short urls & more: http://ittyurl.net
    >>
    >> "Tarscher" wrote:
    >> > Eliyahu, what i try:

    >>
    >> > - On the SelectedIndexChanged event of the dropdownlist I read out the
    >> > DDL data and create the appropriate controls
    >> > protected void KeywordDropDownList_SelectedIndexChanged(object
    >> > sender, EventArgs e)
    >> > {
    >> > string selectedKeyword =
    >> > KeywordDropDownList.SelectedValue.ToString();
    >> > DrawKeywordEditor(selectedKeyword);
    >> > }
    >> > - I then try to recreate the control in the Preinit stage but this
    >> > seems to fail.
    >> > base.OnPreInit(e);
    >> > IKeywordEditor myIKE =
    >> > EditorHolder.FindControl("KeywordEditor") as
    >> > IKeywordEditor;
    >> > The EditorHolder is not known in this page (it is in the init stage).
    >> > When I recreate it in the Init stage I always get a null returned. The
    >> > KeywordEditor control is thus not found.

    >>
    >> > I'm probably missing something crucial here.

    >>
    >> > Stijn

    >>
    >> > On 12 feb, 16:51, "Eliyahu Goldin"
    >> > <> wrote:
    >> > > Stijn,

    >>
    >> > > You need to re-create dynamic controls only if you are in a postback
    >> > > and the
    >> > > controls need to pickup their properties that arrive to the server in
    >> > > the
    >> > > same postback. In your case, you seem to create controls every
    >> > > postback from
    >> > > scratch depending on the selected ddl item. If it is the case, I
    >> > > don't see
    >> > > why can't you wait with creating controls till the stage where you
    >> > > already
    >> > > know what to create.

    >>
    >> > > --
    >> > > Eliyahu Goldin,
    >> > > Software Developer & Consultant
    >> > > Microsoft MVP
    >> > > [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >>
    >> > > "Tarscher" <> wrote in message

    >>
    >> > >news:...

    >>
    >> > > > Thanks Eliyah for the quick answer. Some moet questions tough.

    >>
    >> > > > You say I have to recreate the controls on the PreInit even. But
    >> > > > how
    >> > > > can I know the state of the drop down list (a static control) in
    >> > > > the
    >> > > > PreInit stage? I can thus not create my dynamic controls since I
    >> > > > don't
    >> > > > know what's in the drop down list.

    >>
    >> > > > Can you please explain me what I do when the dropdownlist
    >> > > > SelectedIndexChanged had fired?

    >>
    >> > > > Thanks
    >> > > > Stijn

    >>
    >> > > > On 12 feb, 15:34, "Eliyahu Goldin"
    >> > > > <> wrote:
    >> > > >> When you create controls first time, do it in Page_Load event or
    >> > > >> later.
    >> > > >> When
    >> > > >> you need to re-create them on postbacks, do it in PreInit event.

    >>
    >> > > >> --
    >> > > >> Eliyahu Goldin,
    >> > > >> Software Developer & Consultant
    >> > > >> Microsoft MVP
    >> > > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >>
    >> > > >> "Tarscher" <> wrote in message

    >>
    >> > > >>news:...

    >>
    >> > > >> > hi all,

    >>
    >> > > >> > I have this seemingly simple problem. I have lost a lot of time
    >> > > >> > on it
    >> > > >> > though.

    >>
    >> > > >> > When a user selects a value from a dropdownlist (static control)
    >> > > >> > a
    >> > > >> > dynamic control is generated. I have to create the dynamic
    >> > > >> > controls
    >> > > >> > in the OnInit stage of the lifecycle. Since data from static
    >> > > >> > controls
    >> > > >> > is not yet available in the OnInit stage I can't know what
    >> > > >> > dynamic
    >> > > >> > control I have to create.

    >>
    >> > > >> > How can I overcome this problem? Since this question has
    >> > > >> > probably
    >> > > >> > already been answered a good tutorial will also do of course. I
    >> > > >> > have
    >> > > >> > been looking on the net but no success.

    >>
    >> > > >> > Thanks in advance
    >> > > >> > Stijn

    >
    >
     
    Eliyahu Goldin, Feb 13, 2007
    #8
  9. Tarscher

    Tarscher Guest

    Eliyaha, that's the technique I currently use but this makes it hard
    to add more user controls. And that's something I really want to
    avoid.

    On 13 feb, 10:33, "Eliyahu Goldin"
    <> wrote:
    > Stijn,
    >
    > I personally hardly ever create any controls dynamically. Usually you can
    > achieve the same with hidden static controls. Make only one of them visible
    > at the time. If hide controls by setting Visible=false, they won't get
    > rendered to the client which is exactly the same effect as not creating them
    > in the first place.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > "Tarscher" <> wrote in message
    >
    > news:...
    >
    > >I assume that what I want to do has been done 1000 of times but maybe
    > > I implement it in the wrong way?

    >
    > > What I want to do:
    > > A dropdownlist is populated with editornames. An editor is a user
    > > control. When the user makes a choice out of the DDL, the user control
    > > (editor) is rendered to the screen. I make the user control a dynamic
    > > control that I add to a placeholder. The fact that the user control
    > > needs to be dynamic is actually the only restrain. I really don't see
    > > how I can pass data to the dynamic control (editors).

    >
    > > Someone has an idea on how to tackle this problem?

    >
    > > Thanks
    > > Stijn

    >
    > > On 12 feb, 18:05, Peter Bromberg [C# MVP]
    > > <> wrote:
    > >> I just read this interchange, and I fear that you have created a flawed
    > >> design. It's a case of "the cart before the horse" here. Better revisit
    > >> your
    > >> logic and see if you cannot find a less - complicated way to reach your
    > >> goal.
    > >> Peter

    >
    > >> --
    > >> Site: http://www.eggheadcafe.com
    > >> UnBlog: http://petesbloggerama.blogspot.com
    > >> Short urls & more: http://ittyurl.net

    >
    > >> "Tarscher" wrote:
    > >> > Eliyahu, what i try:

    >
    > >> > - On the SelectedIndexChanged event of the dropdownlist I read out the
    > >> > DDL data and create the appropriate controls
    > >> > protected void KeywordDropDownList_SelectedIndexChanged(object
    > >> > sender, EventArgs e)
    > >> > {
    > >> > string selectedKeyword =
    > >> > KeywordDropDownList.SelectedValue.ToString();
    > >> > DrawKeywordEditor(selectedKeyword);
    > >> > }
    > >> > - I then try to recreate the control in the Preinit stage but this
    > >> > seems to fail.
    > >> > base.OnPreInit(e);
    > >> > IKeywordEditor myIKE =
    > >> > EditorHolder.FindControl("KeywordEditor") as
    > >> > IKeywordEditor;
    > >> > The EditorHolder is not known in this page (it is in the init stage).
    > >> > When I recreate it in the Init stage I always get a null returned. The
    > >> > KeywordEditor control is thus not found.

    >
    > >> > I'm probably missing something crucial here.

    >
    > >> > Stijn

    >
    > >> > On 12 feb, 16:51, "Eliyahu Goldin"
    > >> > <> wrote:
    > >> > > Stijn,

    >
    > >> > > You need to re-create dynamic controls only if you are in a postback
    > >> > > and the
    > >> > > controls need to pickup their properties that arrive to the server in
    > >> > > the
    > >> > > same postback. In your case, you seem to create controls every
    > >> > > postback from
    > >> > > scratch depending on the selected ddl item. If it is the case, I
    > >> > > don't see
    > >> > > why can't you wait with creating controls till the stage where you
    > >> > > already
    > >> > > know what to create.

    >
    > >> > > --
    > >> > > Eliyahu Goldin,
    > >> > > Software Developer & Consultant
    > >> > > Microsoft MVP
    > >> > > [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > >> > > "Tarscher" <> wrote in message

    >
    > >> > >news:...

    >
    > >> > > > Thanks Eliyah for the quick answer. Some moet questions tough.

    >
    > >> > > > You say I have to recreate the controls on the PreInit even. But
    > >> > > > how
    > >> > > > can I know the state of the drop down list (a static control) in
    > >> > > > the
    > >> > > > PreInit stage? I can thus not create my dynamic controls since I
    > >> > > > don't
    > >> > > > know what's in the drop down list.

    >
    > >> > > > Can you please explain me what I do when the dropdownlist
    > >> > > > SelectedIndexChanged had fired?

    >
    > >> > > > Thanks
    > >> > > > Stijn

    >
    > >> > > > On 12 feb, 15:34, "Eliyahu Goldin"
    > >> > > > <> wrote:
    > >> > > >> When you create controls first time, do it in Page_Load event or
    > >> > > >> later.
    > >> > > >> When
    > >> > > >> you need to re-create them on postbacks, do it in PreInit event.

    >
    > >> > > >> --
    > >> > > >> Eliyahu Goldin,
    > >> > > >> Software Developer & Consultant
    > >> > > >> Microsoft MVP
    > >> > > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > >> > > >> "Tarscher" <> wrote in message

    >
    > >> > > >>news:...

    >
    > >> > > >> > hi all,

    >
    > >> > > >> > I have this seemingly simple problem. I have lost a lot of time
    > >> > > >> > on it
    > >> > > >> > though.

    >
    > >> > > >> > When a user selects a value from a dropdownlist (static control)
    > >> > > >> > a
    > >> > > >> > dynamic control is generated. I have to create the dynamic
    > >> > > >> > controls
    > >> > > >> > in the OnInit stage of the lifecycle. Since data from static
    > >> > > >> > controls
    > >> > > >> > is not yet available in the OnInit stage I can't know what
    > >> > > >> > dynamic
    > >> > > >> > control I have to create.

    >
    > >> > > >> > How can I overcome this problem? Since this question has
    > >> > > >> > probably
    > >> > > >> > already been answered a good tutorial will also do of course. I
    > >> > > >> > have
    > >> > > >> > been looking on the net but no success.

    >
    > >> > > >> > Thanks in advance
    > >> > > >> > Stijn
     
    Tarscher, Feb 13, 2007
    #9
  10. Tarscher

    Russell Guest

    You could take the hidden controls a bit further... place them all in
    divs and set the style "display" property to "none". Absolute-
    position the divs to the same location on the page. Set "display" to
    "block" for the one that corresponds to the default selection in your
    dropdown (if there is one.) Add a hidden form field whose value is
    set to the ID of the initially selected control (or "" if there isn't
    one.) Make dropdown option values the same as the corresponding div
    ids and then add a client onchange handler
    (onchange="ToggleEditor(this);") which could call a function something
    like the following

    function ToggleEditor(select) {
    if (select.selectedIndex >= 0) {
    var hidden = document.getElementById('hiddenSelectedDivID');
    if (hidden) {
    var elem = document.getElementById(hidden.value);
    if (elem) {elem.style.display = 'none';}
    hidden.value = '';
    elem =
    document.getElementById(select.options(select.selectedIndex).value);
    if (elem) {
    elem.style.display = 'block';
    hidden.value = elem.id;
    }
    }
    }
    }

    This way you can change editors without a postback, unless there is
    other stuff you do with them on postbacks like populate them with data
    that can't be predetermined.

    On Feb 13, 5:01 am, "Tarscher" <> wrote:
    > Eliyaha, that's the technique I currently use but this makes it hard
    > to add more user controls. And that's something I really want to
    > avoid.
    >
    > On 13 feb, 10:33, "Eliyahu Goldin"
    >
    >
    >
    > <> wrote:
    > > Stijn,

    >
    > > I personally hardly ever create any controls dynamically. Usually you can
    > > achieve the same with hidden static controls. Make only one of them visible
    > > at the time. If hide controls by setting Visible=false, they won't get
    > > rendered to the client which is exactly the same effect as not creating them
    > > in the first place.

    >
    > > --
    > > Eliyahu Goldin,
    > > Software Developer & Consultant
    > > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > > "Tarscher" <> wrote in message

    >
    > >news:...

    >
    > > >I assume that what I want to do has been done 1000 of times but maybe
    > > > I implement it in the wrong way?

    >
    > > > What I want to do:
    > > > A dropdownlist is populated with editornames. An editor is a user
    > > > control. When the user makes a choice out of the DDL, the user control
    > > > (editor) is rendered to the screen. I make the user control a dynamic
    > > > control that I add to a placeholder. The fact that the user control
    > > > needs to be dynamic is actually the only restrain. I really don't see
    > > > how I can pass data to the dynamic control (editors).

    >
    > > > Someone has an idea on how to tackle this problem?

    >
    > > > Thanks
    > > > Stijn

    >
    > > > On 12 feb, 18:05, Peter Bromberg [C# MVP]
    > > > <> wrote:
    > > >> I just read this interchange, and I fear that you have created a flawed
    > > >> design. It's a case of "the cart before the horse" here. Better revisit
    > > >> your
    > > >> logic and see if you cannot find a less - complicated way to reach your
    > > >> goal.
    > > >> Peter

    >
    > > >> --
    > > >> Site: http://www.eggheadcafe.com
    > > >> UnBlog: http://petesbloggerama.blogspot.com
    > > >> Short urls & more: http://ittyurl.net

    >
    > > >> "Tarscher" wrote:
    > > >> > Eliyahu, what i try:

    >
    > > >> > - On the SelectedIndexChanged event of the dropdownlist I read out the
    > > >> > DDL data and create the appropriate controls
    > > >> > protected void KeywordDropDownList_SelectedIndexChanged(object
    > > >> > sender, EventArgs e)
    > > >> > {
    > > >> > string selectedKeyword =
    > > >> > KeywordDropDownList.SelectedValue.ToString();
    > > >> > DrawKeywordEditor(selectedKeyword);
    > > >> > }
    > > >> > - I then try to recreate the control in the Preinit stage but this
    > > >> > seems to fail.
    > > >> > base.OnPreInit(e);
    > > >> > IKeywordEditor myIKE =
    > > >> > EditorHolder.FindControl("KeywordEditor") as
    > > >> > IKeywordEditor;
    > > >> > The EditorHolder is not known in this page (it is in the init stage).
    > > >> > When I recreate it in the Init stage I always get a null returned. The
    > > >> > KeywordEditor control is thus not found.

    >
    > > >> > I'm probably missing something crucial here.

    >
    > > >> > Stijn

    >
    > > >> > On 12 feb, 16:51, "Eliyahu Goldin"
    > > >> > <> wrote:
    > > >> > > Stijn,

    >
    > > >> > > You need to re-create dynamic controls only if you are in a postback
    > > >> > > and the
    > > >> > > controls need to pickup their properties that arrive to the server in
    > > >> > > the
    > > >> > > same postback. In your case, you seem to create controls every
    > > >> > > postback from
    > > >> > > scratch depending on the selected ddl item. If it is the case, I
    > > >> > > don't see
    > > >> > > why can't you wait with creating controls till the stage where you
    > > >> > > already
    > > >> > > know what to create.

    >
    > > >> > > --
    > > >> > > Eliyahu Goldin,
    > > >> > > Software Developer & Consultant
    > > >> > > Microsoft MVP
    > > >> > > [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > > >> > > "Tarscher" <> wrote in message

    >
    > > >> > >news:...

    >
    > > >> > > > Thanks Eliyah for the quick answer. Some moet questions tough.

    >
    > > >> > > > You say I have to recreate the controls on the PreInit even. But
    > > >> > > > how
    > > >> > > > can I know the state of the drop down list (a static control) in
    > > >> > > > the
    > > >> > > > PreInit stage? I can thus not create my dynamic controls since I
    > > >> > > > don't
    > > >> > > > know what's in the drop down list.

    >
    > > >> > > > Can you please explain me what I do when the dropdownlist
    > > >> > > > SelectedIndexChanged had fired?

    >
    > > >> > > > Thanks
    > > >> > > > Stijn

    >
    > > >> > > > On 12 feb, 15:34, "Eliyahu Goldin"
    > > >> > > > <> wrote:
    > > >> > > >> When you create controls first time, do it in Page_Load event or
    > > >> > > >> later.
    > > >> > > >> When
    > > >> > > >> you need to re-create them on postbacks, do it in PreInit event.

    >
    > > >> > > >> --
    > > >> > > >> Eliyahu Goldin,
    > > >> > > >> Software Developer & Consultant
    > > >> > > >> Microsoft MVP
    > > >> > > >> [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

    >
    > > >> > > >> "Tarscher" <> wrote in message

    >
    > > >> > > >>news:...

    >
    > > >> > > >> > hi all,

    >
    > > >> > > >> > I have this seemingly simple problem. I have lost a lot of time
    > > >> > > >> > on it
    > > >> > > >> > though.

    >
    > > >> > > >> > When a user selects a value from a dropdownlist (static control)
    > > >> > > >> > a
    > > >> > > >> > dynamic control is generated. I have to create the dynamic
    > > >> > > >> > controls
    > > >> > > >> > in the OnInit stage of the lifecycle. Since data from static
    > > >> > > >> > controls
    > > >> > > >> > is not yet available in the OnInit stage I can't know what
    > > >> > > >> > dynamic
    > > >> > > >> > control I have to create.

    >
    > > >> > > >> > How can I overcome this problem? Since this question has
    > > >> > > >> > probably
    > > >> > > >> > already been answered a good tutorial will also do of course. I
    > > >> > > >> > have
    > > >> > > >> > been looking on the net but no success.

    >
    > > >> > > >> > Thanks in advance
    > > >> > > >> > Stijn- Hide quoted text -

    >
    > - Show quoted text -
     
    Russell, Feb 13, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald
    Replies:
    2
    Views:
    850
    Robbe Morris [C# MVP]
    Jan 17, 2006
  2. Greg Hoover
    Replies:
    1
    Views:
    491
    Robert Bossy
    Mar 25, 2008
  3. Jack

    Dynamic vs static user controls

    Jack, Sep 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    111
  4. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    471
    Eidolon
    May 4, 2004
  5. bernd
    Replies:
    2
    Views:
    123
    phaylon
    May 24, 2005
Loading...

Share This Page