Setting values of properties in user controls from the parent form

Discussion in 'ASP .Net' started by Mark Rae, May 11, 2006.

  1. Mark Rae

    Mark Rae Guest

    Hi,

    Just a general quickie on setting properties of user controls from the
    parent form.

    Let's say I have a user control called note.ascx which displays a datagrid.
    That datagrid is populated according to an integer variable in the ViewState
    of the parent form.

    The code behind (still in v1.1) of the user control contains the following:

    public class note : System.Web.UI.UserControl
    {
    protected int pParentID;
    public int ParentID
    {
    set{pParentID = value;}
    }


    Let's say the parent form has the following:

    <%@ Register TagPrefix="ucNote" TagName="ucNotes" Src="~/note.ascx" %>
    ..
    ..
    ..
    <ucNote:ucNotes ID=ucNotes runat=server ParentID=<%=ViewState["ParentID"]%>
    />

    While that certainly works, would it be more efficient to move the ParentID
    tag out of the form page and set it in the code behind instead? This would
    also prevent the ParentID value from being visible in View Source, though
    that's not reallly much of a consideration in this case.

    Any assistance gratefully received.

    Mark
     
    Mark Rae, May 11, 2006
    #1
    1. Advertising

  2. Mark Rae

    Parag Guest

    Hi Mark,

    Setting the property for the User control in ASPX page is OK. But I dont think you can set it in code behind. Whenever you try to set it, it will be either init event or page load. In both the cases the user control is already compiled and its output is added to the page accordingly. But you can try one thing ... set the value in the session init and keep on changing the values as per the page in the session and make the user control pick the those values form the session. In this way you can avoid the problem of writing the property value in viewstate.

    I hope I have understood your problem and answered it accordingly. In case I am wrong at any point .. then please let me know as it will also add to my knowledge of ASP.NET

    Regards,
    Parag Kulkarni
    MTS,
    Persistent Systems Private Limited.




    "Mark Rae" <> wrote in message news:%...
    > Hi,
    >
    > Just a general quickie on setting properties of user controls from the
    > parent form.
    >
    > Let's say I have a user control called note.ascx which displays a datagrid.
    > That datagrid is populated according to an integer variable in the ViewState
    > of the parent form.
    >
    > The code behind (still in v1.1) of the user control contains the following:
    >
    > public class note : System.Web.UI.UserControl
    > {
    > protected int pParentID;
    > public int ParentID
    > {
    > set{pParentID = value;}
    > }
    >
    >
    > Let's say the parent form has the following:
    >
    > <%@ Register TagPrefix="ucNote" TagName="ucNotes" Src="~/note.ascx" %>
    > .
    > .
    > .
    > <ucNote:ucNotes ID=ucNotes runat=server ParentID=<%=ViewState["ParentID"]%>
    > />
    >
    > While that certainly works, would it be more efficient to move the ParentID
    > tag out of the form page and set it in the code behind instead? This would
    > also prevent the ParentID value from being visible in View Source, though
    > that's not reallly much of a consideration in this case.
    >
    > Any assistance gratefully received.
    >
    > Mark
    >
    >
     
    Parag, May 11, 2006
    #2
    1. Advertising

  3. Mark Rae

    Mark Rae Guest

    "Parag" <> wrote in message
    news:...

    Pareg,

    >Setting the property for the User control in ASPX page is OK. But I dont
    >think you can set it in code behind.


    ??? Of course you can, otherwise I wouldn't have asked the question! A class
    is a class is a class...

    >Whenever you try to set it, it will be either init event or page load.


    OK.

    >In both the cases the user control is already compiled and its output is
    >added to the page accordingly.


    The parent form's Page_Load fires before the user control's Page_Load...

    >But you can try one thing ... set the value in the session init and keep on
    >changing the values as per the
    >page in the session and make the user control pick the those values form
    >the session. In this way you
    >can avoid the problem of writing the property value in viewstate.


    You can't possibly be serious...

    >I hope I have understood your problem and answered it accordingly.


    I think you've answered your own question...! :)

    Just to clarify, I wasn't asking how a parent control can set properties on
    its user controls - that's dead easy - I was just asking whether that was
    the best way to do it...

    Thanks for the reply anyway...

    Mark
     
    Mark Rae, May 11, 2006
    #3
  4. Mark Rae

    Rick Guest

    Mark,

    I am not sure you are looking for an answer. Parent controls /
    containers set child properties all the time, and you as the developer
    choose the best time to do it.

    It the TextBox_TextChanged event handler you may read the value in a
    child TextBox and clear the contents by TextBox.Text = null; if they
    are invalid.

    In your Page_Load you may call a method to assign a DataSource to a
    child DataGrid and the call it's DataBind.

    To answer your rhetorical questions: 1. Yes you can set the properties
    of a child control from a parent container. 2. Only you can decide if
    it's the best way. Me I would use the inherent
    System.Web.UI.Control.Parent to access parent properties.
    (base.Response.Write(this.Parent.ID);)

    -Rick
     
    Rick, May 12, 2006
    #4
    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. Guest
    Replies:
    6
    Views:
    380
    Walter Wang [MSFT]
    Dec 23, 2006
  2. Wardie
    Replies:
    4
    Views:
    494
    Wardie
    Nov 8, 2006
  3. Dave
    Replies:
    7
    Views:
    284
    Andy Gaskell
    May 11, 2004
  4. David Webb
    Replies:
    6
    Views:
    200
    David Webb
    Dec 30, 2004
  5. jodleren

    Setting values on parent form

    jodleren, Feb 14, 2007, in forum: Javascript
    Replies:
    1
    Views:
    96
    jodleren
    Feb 14, 2007
Loading...

Share This Page