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


    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 Rae, May 11, 2006
    1. Advertisements

  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

    Parag Kulkarni
    Persistent Systems Private Limited.
    Parag, May 11, 2006
    1. Advertisements

  3. Mark Rae

    Mark Rae Guest

    ??? Of course you can, otherwise I wouldn't have asked the question! A class
    is a class is a class...
    The parent form's Page_Load fires before the user control's Page_Load...
    You can't possibly be serious...
    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 Rae, May 11, 2006
  4. Mark Rae

    Rick Guest


    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.

    Rick, May 12, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.