Design-Time UserControl Property Setting

Discussion in 'ASP .Net Web Controls' started by Alex Maghen, Jun 5, 2006.

  1. Alex Maghen

    Alex Maghen Guest

    I have a UserControl with a single constituent DropdownList control. My
    control takes 2 important properties:

    Property 1: "ListSource" is a key in a database for pulling out the list
    that will populate the contents of the DropdownList.

    Property 2: "SelectedValue" is the actual selected Value in the DropdownList.

    Here's my problem: If the lifecycle of the page that uses my control, I'm
    noticing that things happen in the following order:
    a. The page's Page_Load() happens
    b. My control's SelectedValue.Set property is called
    c. My control's Page_Load() happens

    Now, here's the problem with that: In my control, the process that causes
    the contituent DropdownList to get populated with items ocurrs in the
    control's Page_Load() event. As a result, the parent page is trying to set my
    control's SelectedValue property BEFORE the control has been populated and,
    therefore, it fails.

    Now, theoretically, I'd want to popultae my control's items at some other
    time in the life-cycle, but the problem is that I can't do that populating
    until I get the design-time property, "ListSource" which tells me how to
    populate it.

    Can you help me figure out how I can gain access to these design-time
    properties and populate my control *before* the Page_Load of the parent page
    ocurrs?

    Thanks.

    Alex
     
    Alex Maghen, Jun 5, 2006
    #1
    1. Advertising

  2. Hi Alex,

    Thank you for posting here.

    As for the UserControl's property setting issue you mentioend, based on my
    understanding it is due to the ascx usercontrol's server-side processing
    model. And the event sequence you found is correct:

    ===============
    a. The page's Page_Load() happens
    b. My control's SelectedValue.Set property is called
    c. My control's Page_Load() happens
    ===============

    Actually , to be more decent, the complete steps here should be:

    =============
    *Page's Load event
    ....... some other events
    *Control's parsing and initializing (set intial value for those custom
    properties)
    ..... some other events
    *Control's load event
    ==============

    I guess that your usercontrol's "SelectedValue" is implemented as below:
    =============
    public string SelectedValue
    {
    get{
    return DropDownList1.SelectedValue;
    }

    set
    {
    DropDownList1.SelectedValue = value;
    }
    }
    =============

    If so, since when the "SelectedValue" of the Usercontrol is called, the
    inner sub dropdownlist control may haven't been databound(populate data),
    then there will occur exceptions. To void this problem, we can just change
    our custom properties(on the usercontrol)'s implemenation instead of moving
    the databound code of the dropdownlist into other events. Generally, I
    would prefer to implement a custom property through a private field .e.g.

    private string _selectedvalue;

    public string SelectedValue
    {
    get{
    return _selectedvalue;
    }

    set
    {
    _selectedvalue = value;
    }
    }


    Thus, when the usercontrol is intializing at runtime, the custom
    properties' intial value will be stored in these private fields. And in
    usercontrol's Load event, we can populate data of other sub controls(such
    as dropdownlist) through these private fields' value. e.g.

    ===============
    protected void Page_Load(object sender, EventArgs e)
    {

    //perform databinding on dropdownlist first

    dropdownlist1.SelectedValue = _selectedValue;

    }

    ===========================

    This can help make the sub controls' properties be updated successfully
    after it has been populated with data(through databiding).

    Anyway, below is my test usercontrol's complete code, you can have a look
    if necessary


    =========ascx=========
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListUC.ascx.cs"
    Inherits="UserControls_ddl_ListUC" %>
    <asp:Label ID="lblCategories" runat="server" Text="Categories:
    "></asp:Label>
    <asp:DropDownList ID="lstCategories" runat="server">
    </asp:DropDownList>


    ==========code behind=========
    public partial class UserControls_ddl_ListUC : System.Web.UI.UserControl
    {

    private int _count;
    private string _selectedvalue;

    public int Count
    {
    get { return _count; }
    set { _count = value; }
    }

    public string SelectedValue
    {
    get { return _selectedvalue; }
    set { _selectedvalue = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {

    string sql = "select TOP {0} CategoryID, CategoryName from
    Categories";


    SqlConnection conn = new
    SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthwindConnS
    tr"].ConnectionString);

    conn.Open();

    SqlCommand comm = new SqlCommand(string.Format(sql, _count), conn);
    SqlDataReader reader =
    comm.ExecuteReader(CommandBehavior.CloseConnection);

    lstCategories.DataSource = reader;
    lstCategories.DataTextField = "CategoryName";
    lstCategories.DataValueField = "CategoryName";


    lstCategories.DataBind();

    reader.Close();

    lstCategories.SelectedValue = _selectedvalue;


    }

    }
    }
    =============================


    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 6, 2006
    #2
    1. Advertising

  3. Alex Maghen

    Alex Maghen Guest

    Steven -

    One thing about your sample code below...
    If I look at your Page_Load(), I see that you fill the DDL only under
    circumstances when this is not a PostBack (!IsPostBock). That's fine for
    filling the DDL. But I notice that you actually also set the DDL's selected
    value only when it's NOT a PostBack too. Is this right? If so, and I set the
    SelectedValue property during a PostBack, the chance I make won't show up,
    right? Don't I have to handle this property regardless of whether it is or is
    not a PostBack?

    Alex



    "Steven Cheng[MSFT]" wrote:

    > Hi Alex,
    >
    > Thank you for posting here.
    >
    > As for the UserControl's property setting issue you mentioend, based on my
    > understanding it is due to the ascx usercontrol's server-side processing
    > model. And the event sequence you found is correct:
    >
    > ===============
    > a. The page's Page_Load() happens
    > b. My control's SelectedValue.Set property is called
    > c. My control's Page_Load() happens
    > ===============
    >
    > Actually , to be more decent, the complete steps here should be:
    >
    > =============
    > *Page's Load event
    > ....... some other events
    > *Control's parsing and initializing (set intial value for those custom
    > properties)
    > ..... some other events
    > *Control's load event
    > ==============
    >
    > I guess that your usercontrol's "SelectedValue" is implemented as below:
    > =============
    > public string SelectedValue
    > {
    > get{
    > return DropDownList1.SelectedValue;
    > }
    >
    > set
    > {
    > DropDownList1.SelectedValue = value;
    > }
    > }
    > =============
    >
    > If so, since when the "SelectedValue" of the Usercontrol is called, the
    > inner sub dropdownlist control may haven't been databound(populate data),
    > then there will occur exceptions. To void this problem, we can just change
    > our custom properties(on the usercontrol)'s implemenation instead of moving
    > the databound code of the dropdownlist into other events. Generally, I
    > would prefer to implement a custom property through a private field .e.g.
    >
    > private string _selectedvalue;
    >
    > public string SelectedValue
    > {
    > get{
    > return _selectedvalue;
    > }
    >
    > set
    > {
    > _selectedvalue = value;
    > }
    > }
    >
    >
    > Thus, when the usercontrol is intializing at runtime, the custom
    > properties' intial value will be stored in these private fields. And in
    > usercontrol's Load event, we can populate data of other sub controls(such
    > as dropdownlist) through these private fields' value. e.g.
    >
    > ===============
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > //perform databinding on dropdownlist first
    >
    > dropdownlist1.SelectedValue = _selectedValue;
    >
    > }
    >
    > ===========================
    >
    > This can help make the sub controls' properties be updated successfully
    > after it has been populated with data(through databiding).
    >
    > Anyway, below is my test usercontrol's complete code, you can have a look
    > if necessary
    >
    >
    > =========ascx=========
    > <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListUC.ascx.cs"
    > Inherits="UserControls_ddl_ListUC" %>
    > <asp:Label ID="lblCategories" runat="server" Text="Categories:
    > "></asp:Label>
    > <asp:DropDownList ID="lstCategories" runat="server">
    > </asp:DropDownList>
    >
    >
    > ==========code behind=========
    > public partial class UserControls_ddl_ListUC : System.Web.UI.UserControl
    > {
    >
    > private int _count;
    > private string _selectedvalue;
    >
    > public int Count
    > {
    > get { return _count; }
    > set { _count = value; }
    > }
    >
    > public string SelectedValue
    > {
    > get { return _selectedvalue; }
    > set { _selectedvalue = value; }
    > }
    >
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if(!IsPostBack)
    > {
    >
    > string sql = "select TOP {0} CategoryID, CategoryName from
    > Categories";
    >
    >
    > SqlConnection conn = new
    > SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthwindConnS
    > tr"].ConnectionString);
    >
    > conn.Open();
    >
    > SqlCommand comm = new SqlCommand(string.Format(sql, _count), conn);
    > SqlDataReader reader =
    > comm.ExecuteReader(CommandBehavior.CloseConnection);
    >
    > lstCategories.DataSource = reader;
    > lstCategories.DataTextField = "CategoryName";
    > lstCategories.DataValueField = "CategoryName";
    >
    >
    > lstCategories.DataBind();
    >
    > reader.Close();
    >
    > lstCategories.SelectedValue = _selectedvalue;
    >
    >
    > }
    >
    > }
    > }
    > =============================
    >
    >
    > Hope this helps.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    >
     
    Alex Maghen, Jun 6, 2006
    #3
  4. Thanks for your response Alex,

    I think you've made a good question. Actually the previous example I
    provided simply store the usercontrol's custom property into a class field,
    and this field won't be stored into any persistent storage(also I only
    apply it onto the dropdownlist in the first load), therefore any change in
    the dropdownlist's selectedItem won't be synchornized to the custom
    property , nor will it be persisted between multiple postbacks. To resolve
    this, we need to do two things:

    1. We need to synchronize the dropdownlist's change on the "SelectedValue"
    to our usercontrol's "SelectedValue" whenever the dropdownlisted's
    "SelectedValue" is changed(by user or by code).

    2. We need to store our usercontrol's "SelectedValue" into a persistent
    storage so that we can always restore the status between multiple
    postbacks.

    Here is a modified version of the test usercontrol:

    ==ascx remain the same========

    ==========codebehind========
    public partial class UserControls_ddl_ListUC : System.Web.UI.UserControl
    {

    private int _count;

    public int Count
    {
    get { return _count; }
    set { _count = value; }
    }

    public string SelectedValue
    {
    get { return ViewState["_selectedvalue"] as string; }
    set
    {
    ViewState["_selectedvalue"] = value;
    }
    }



    protected void Page_Load(object sender, EventArgs e)
    {

    Response.Write("<br/>Page_Load........");
    if (!IsPostBack)
    {
    string sql = "select TOP {0} CategoryID, CategoryName from
    Categories";


    SqlConnection conn = new
    SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthwindConnS
    tr"].ConnectionString);

    conn.Open();

    SqlCommand comm = new SqlCommand(string.Format(sql, _count),
    conn);
    SqlDataReader reader =
    comm.ExecuteReader(CommandBehavior.CloseConnection);

    lstCategories.DataSource = reader;
    lstCategories.DataTextField = "CategoryName";
    lstCategories.DataValueField = "CategoryName";


    lstCategories.DataBind();

    reader.Close();


    }

    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
    lstCategories.SelectedValue = SelectedValue;

    }

    protected void lstCategories_SelectedIndexChanged(object sender,
    EventArgs e)
    {
    SelectedValue = lstCategories.SelectedValue;
    }
    }
    ==========================

    In the new one, I persist the usercontrol's "SelectedValue" into ViewState.
    Also, I used DropDownlist's "SelectedIndexChanged" event to synchronize the
    usercontrol's "SelectedValue" with dropdownlist's "SelectedValue". And
    another important change is I apply the usercontrol's "SelectedValue" to
    the dropdownlist in the "Prerender" event, this is the last event before
    page render whether the change can be persisted into page's ViewState.

    Hope this helps. If there is still anything unclear, please feel free to
    post here.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 7, 2006
    #4
  5. Alex Maghen

    Alex Maghen Guest

    Steven -

    Thanks for this. But I still have strange things going on. Also, it seems
    like there's still something not quite right about your sample:

    I noticed that the SelectedValue property GET for your sample does
    return(ViewState["_SelectedValue]...

    But I'm not sure if this is right because the only way you have for that
    information to be updated is in a server-side SelectedIndexChanged() event on
    the pull-down. Is that really want we want to do?

    Isn't there a nice, simple, stripped-down sample control that shows the
    right places to do all these types of things?

    Alex


    "Steven Cheng[MSFT]" wrote:

    > Thanks for your response Alex,
    >
    > I think you've made a good question. Actually the previous example I
    > provided simply store the usercontrol's custom property into a class field,
    > and this field won't be stored into any persistent storage(also I only
    > apply it onto the dropdownlist in the first load), therefore any change in
    > the dropdownlist's selectedItem won't be synchornized to the custom
    > property , nor will it be persisted between multiple postbacks. To resolve
    > this, we need to do two things:
    >
    > 1. We need to synchronize the dropdownlist's change on the "SelectedValue"
    > to our usercontrol's "SelectedValue" whenever the dropdownlisted's
    > "SelectedValue" is changed(by user or by code).
    >
    > 2. We need to store our usercontrol's "SelectedValue" into a persistent
    > storage so that we can always restore the status between multiple
    > postbacks.
    >
    > Here is a modified version of the test usercontrol:
    >
    > ==ascx remain the same========
    >
    > ==========codebehind========
    > public partial class UserControls_ddl_ListUC : System.Web.UI.UserControl
    > {
    >
    > private int _count;
    >
    > public int Count
    > {
    > get { return _count; }
    > set { _count = value; }
    > }
    >
    > public string SelectedValue
    > {
    > get { return ViewState["_selectedvalue"] as string; }
    > set
    > {
    > ViewState["_selectedvalue"] = value;
    > }
    > }
    >
    >
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > Response.Write("<br/>Page_Load........");
    > if (!IsPostBack)
    > {
    > string sql = "select TOP {0} CategoryID, CategoryName from
    > Categories";
    >
    >
    > SqlConnection conn = new
    > SqlConnection(WebConfigurationManager.ConnectionStrings["LocalNorthwindConnS
    > tr"].ConnectionString);
    >
    > conn.Open();
    >
    > SqlCommand comm = new SqlCommand(string.Format(sql, _count),
    > conn);
    > SqlDataReader reader =
    > comm.ExecuteReader(CommandBehavior.CloseConnection);
    >
    > lstCategories.DataSource = reader;
    > lstCategories.DataTextField = "CategoryName";
    > lstCategories.DataValueField = "CategoryName";
    >
    >
    > lstCategories.DataBind();
    >
    > reader.Close();
    >
    >
    > }
    >
    > }
    >
    > protected void Page_PreRender(object sender, EventArgs e)
    > {
    > lstCategories.SelectedValue = SelectedValue;
    >
    > }
    >
    > protected void lstCategories_SelectedIndexChanged(object sender,
    > EventArgs e)
    > {
    > SelectedValue = lstCategories.SelectedValue;
    > }
    > }
    > ==========================
    >
    > In the new one, I persist the usercontrol's "SelectedValue" into ViewState.
    > Also, I used DropDownlist's "SelectedIndexChanged" event to synchronize the
    > usercontrol's "SelectedValue" with dropdownlist's "SelectedValue". And
    > another important change is I apply the usercontrol's "SelectedValue" to
    > the dropdownlist in the "Prerender" event, this is the last event before
    > page render whether the change can be persisted into page's ViewState.
    >
    > Hope this helps. If there is still anything unclear, please feel free to
    > post here.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    >
    >
    >
    >
     
    Alex Maghen, Jun 7, 2006
    #5
  6. Thanks for your followup Alex,
    Here is my comments ni line:


    Thanks for this. But I still have strange things going on. Also, it seems
    like there's still something not quite right about your sample:
    =============================
    Would you please let me know the "strange things" you met or provide me a
    test page to demonstrate the issue.



    But I'm not sure if this is right because the only way you have for that
    information to be updated is in a server-side SelectedIndexChanged() event
    on
    the pull-down. Is that really want we want to do?
    ===============================
    In addition to the "SelectedIndexChanged() " event handler, the
    "SelectedValue" property's setter method will also update the
    ViewState["_selectedvalue"]. Here I use ViewState to persist the custom
    property("SelectedValue"), also, since the usercontrol's "SelectedValue"
    property should always represent the curent selected state of the inner
    dropdownlist, whenever the dropdownlist has been changed, we need to
    synchornize the dropdownlist.SelectedValue with our usercontrol's
    "SelectedValue" property. And the "SelectedIndexChanged" is the only event
    to synchorize the selected value from dropdownlist to usercontrol's
    property.



    Isn't there a nice, simple, stripped-down sample control that shows the
    right places to do all these types of things?
    ============================
    Yes, I admit that the example usercontrol involves several events to make
    it complete, however, I did this because each of the events has its own
    functionality and followup the ASP.NET page/server control's server-side
    lifecycle.

    The inline attribute value is assigned to the usercontrol's Property at
    initiailze time( when the usercontrol is loaded and is before init or load
    event)

    <uc1:ListUC ID="ListUC1" runat="server" Count="7" SelectedValue="Produce" />

    So we can not directly assign the "SelectedValue" to
    dropdownlist.SelectedValue in the setter , because at that time, the
    dropdownlist is still not created and hasn't been populated with any data.
    So I store it into a intermediate place, and since we need to make it
    persisted across multiple postback, I choose ViewState.

    In addition, I place the assignment from usercontrol's "SelectedValue" to
    dropdownlist.SelectedValue like:

    =========
    protected void Page_PreRender(object sender, EventArgs e)
    {
    lstCategories.SelectedValue = SelectedValue;

    }
    ==========

    in the usercontrol's "PreRender" event because at that time, the
    dropdownlist is certainly intialized and loaded(also populated with data
    through databinding), and any changes on server control in PreRender event
    will be able to persisted into ViewState(if that state does be stored in
    ViewState).

    BTW, if we're developing a custom web server control(not ascx usercontrol),
    there is some other patterns we can follow to create the control
    structure(such as put most sub control constructing code in
    "CreateChildControls" method), however, since this is a usercontrol, I'd
    rather use the current pattern in my test control.

    Anyway, please feel free to let me know your point or the problem you want
    to address so that we can concentrate more on them.

    In addition, here are some good articles describes ASP.NET page/controls'
    lifecycle and server-side processing model which will be helpful to us when
    creating custom web server control:

    #ASP.NET Page Life Cycle Overview
    http://msdn2.microsoft.com/en-us/library/ms178472.aspx

    #Maintaining State in a Control
    http://msdn.microsoft.com/library/en-us/cpguide/html/cpconMantainingStateInC
    ontrol.asp?frame=true

    #Understanding ASP.NET View State (ASP.NET Technical Articles)
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
    /viewstate.asp

    http://delphi.about.com/library/weekly/aa051705a.htm

    Hope this also helps some.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 8, 2006
    #6
  7. Alex Maghen

    Alex Maghen Guest

    Steven -

    I *will* send you a sample of my code but before I do, can you answer
    something that might help me debug this bizzarre "collision" problem I'm
    having between two instances of my control on the same page?

    My sample ASPX page has two of the MyCtl controls on it. And inn the ASPX
    page's Page_Load(), I have the following:

    if (!IsPostBack)
    {
    MyCtl1.SelectedValue = "MikeL";
    MyCtl2.SelectedValue = "AlexM";
    }

    When I do this, strangely, both controls show up with "AlexM" selected even
    though I have NO static members, functions, variable, etc. *anywhere* in
    MyCtl. NOW, if I comment out the send line...

    if (!IsPostBack)
    {
    MyCtl1.SelectedValue = "MikeL";
    //MyCtl2.SelectedValue = "AlexM";
    }

    then both controls come up with the default SelectedValue (blank), as if the
    first one was never set. FINALLY - and this is the one I REALLY don't get -
    if I comment out the FIRST line instead of the second one...

    if (!IsPostBack)
    {
    //MyCtl1.SelectedValue = "MikeL";
    MyCtl2.SelectedValue = "AlexM";
    }

    I get a far stranger problem! In this case I get a runtime "Cannot have
    multiple items selected in a DropDownList" error. What does this even mean?
    How is it even possible? I'm looking at my code and the only time I EVER make
    an assignment to me constituent DropDownList inside my control is when I do
    a simple "DDL.SelectedValue = ...". So even if I do that several times, how
    could it ever result in this error?

    Your thoughts? If this still doesn't mean anything to you, I'll send you a
    code sample. THANKS!

    Alex

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your followup Alex,
    > Here is my comments ni line:
    >
    >
    > Thanks for this. But I still have strange things going on. Also, it seems
    > like there's still something not quite right about your sample:
    > =============================
    > Would you please let me know the "strange things" you met or provide me a
    > test page to demonstrate the issue.
    >
    >
    >
    > But I'm not sure if this is right because the only way you have for that
    > information to be updated is in a server-side SelectedIndexChanged() event
    > on
    > the pull-down. Is that really want we want to do?
    > ===============================
    > In addition to the "SelectedIndexChanged() " event handler, the
    > "SelectedValue" property's setter method will also update the
    > ViewState["_selectedvalue"]. Here I use ViewState to persist the custom
    > property("SelectedValue"), also, since the usercontrol's "SelectedValue"
    > property should always represent the curent selected state of the inner
    > dropdownlist, whenever the dropdownlist has been changed, we need to
    > synchornize the dropdownlist.SelectedValue with our usercontrol's
    > "SelectedValue" property. And the "SelectedIndexChanged" is the only event
    > to synchorize the selected value from dropdownlist to usercontrol's
    > property.
    >
    >
    >
    > Isn't there a nice, simple, stripped-down sample control that shows the
    > right places to do all these types of things?
    > ============================
    > Yes, I admit that the example usercontrol involves several events to make
    > it complete, however, I did this because each of the events has its own
    > functionality and followup the ASP.NET page/server control's server-side
    > lifecycle.
    >
    > The inline attribute value is assigned to the usercontrol's Property at
    > initiailze time( when the usercontrol is loaded and is before init or load
    > event)
    >
    > <uc1:ListUC ID="ListUC1" runat="server" Count="7" SelectedValue="Produce" />
    >
    > So we can not directly assign the "SelectedValue" to
    > dropdownlist.SelectedValue in the setter , because at that time, the
    > dropdownlist is still not created and hasn't been populated with any data.
    > So I store it into a intermediate place, and since we need to make it
    > persisted across multiple postback, I choose ViewState.
    >
    > In addition, I place the assignment from usercontrol's "SelectedValue" to
    > dropdownlist.SelectedValue like:
    >
    > =========
    > protected void Page_PreRender(object sender, EventArgs e)
    > {
    > lstCategories.SelectedValue = SelectedValue;
    >
    > }
    > ==========
    >
    > in the usercontrol's "PreRender" event because at that time, the
    > dropdownlist is certainly intialized and loaded(also populated with data
    > through databinding), and any changes on server control in PreRender event
    > will be able to persisted into ViewState(if that state does be stored in
    > ViewState).
    >
    > BTW, if we're developing a custom web server control(not ascx usercontrol),
    > there is some other patterns we can follow to create the control
    > structure(such as put most sub control constructing code in
    > "CreateChildControls" method), however, since this is a usercontrol, I'd
    > rather use the current pattern in my test control.
    >
    > Anyway, please feel free to let me know your point or the problem you want
    > to address so that we can concentrate more on them.
    >
    > In addition, here are some good articles describes ASP.NET page/controls'
    > lifecycle and server-side processing model which will be helpful to us when
    > creating custom web server control:
    >
    > #ASP.NET Page Life Cycle Overview
    > http://msdn2.microsoft.com/en-us/library/ms178472.aspx
    >
    > #Maintaining State in a Control
    > http://msdn.microsoft.com/library/en-us/cpguide/html/cpconMantainingStateInC
    > ontrol.asp?frame=true
    >
    > #Understanding ASP.NET View State (ASP.NET Technical Articles)
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
    > /viewstate.asp
    >
    > http://delphi.about.com/library/weekly/aa051705a.htm
    >
    > Hope this also helps some.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    >
    >
    >
    >
     
    Alex Maghen, Jun 8, 2006
    #7
  8. Thanks for your response Alex,

    I admit that I can not quite get what happened inside, but I'm sure this is
    an abnormal behavior. Anyway, I've attached my complete test usercontrol
    and a test (attached them in this message, you can get it if you're reading
    the newsgroup though Outlook express). Meanwhile, you can also send me a
    simplified control with a test page to demonstrate your issue at your
    earliest convenience.

    Looking forward to your response.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 9, 2006
    #8
  9. Alex Maghen

    Alex Maghen Guest

    Steven -

    CRAP! I hate when I have to admit this kind of thing but... after all
    that... I figured it out and it's all my fault. Let's pretent this problem
    never happened and thanks so much for your help!

    Alex

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your response Alex,
    >
    > I admit that I can not quite get what happened inside, but I'm sure this is
    > an abnormal behavior. Anyway, I've attached my complete test usercontrol
    > and a test (attached them in this message, you can get it if you're reading
    > the newsgroup though Outlook express). Meanwhile, you can also send me a
    > simplified control with a test page to demonstrate your issue at your
    > earliest convenience.
    >
    > Looking forward to your response.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
     
    Alex Maghen, Jun 9, 2006
    #9
  10. Hi Alex,

    Really glad that you've figured it out and I think it most important that
    everything start working correctly.

    Have a good day!

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Jun 10, 2006
    #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. Fabiano
    Replies:
    2
    Views:
    652
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  2. NorCan

    UserControl property as dropdown at design time?

    NorCan, Jun 9, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    412
    Alessandro Zifiglio
    Jun 13, 2006
  3. jdp

    Viewing Usercontrol Property Changes at Design Time

    jdp, Jul 13, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    126
  4. jdp

    Viewing Usercontrol Property Changes at Design Time

    jdp, Aug 3, 2006, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    142
  5. Replies:
    0
    Views:
    141
Loading...

Share This Page