Custom User Control and packaging into a dll

Discussion in 'ASP .Net Building Controls' started by Ralph, Feb 22, 2005.

  1. Ralph

    Ralph Guest

    Hi,

    I have been experimenting creating a User Control and overriding its Render
    method so that I have no reliance on an external ascx file and can package
    this control into a dll.

    I dynamically create my controls in Page_Load() and everything works fine
    while this code is part of my main application dll.

    My problem occurs when I move the code for the User control from my
    application dll into my library dll.

    Within my User control I have a Hidden HTML input control. I can access this
    control and its value without any problems when the User control is part of
    my application dll, when I move the User Control into the library dll this
    control doesn't keep its value, it is like it loses its ViewState or
    something.

    I was wondering if there's something that I need to do in order to allow me
    to package the User Control into a dll other than my main web application
    dll, so that all the controls maintain their state.

    thanks
    Ralph
     
    Ralph, Feb 22, 2005
    #1
    1. Advertising

  2. Ralph

    MasterGaurav Guest

    MasterGaurav, Feb 22, 2005
    #2
    1. Advertising

  3. Ralph

    Ralph Guest

    Hi Gaurav,

    Here's what I am trying to do below, but without the use of an ascx page.
    The hidden input is populated by a popup window and when the page is
    submitted I want to access this value through the FileID property.

    I keep getting an exception error when I access the FileID property as its
    always blank and hence Parse.Int32 fails.

    public class FileControlTest : System.Web.UI.UserControl {
    private int _fileid;
    protected HtmlInputHidden HiddenFileID;

    public int FileID {
    get {
    return int.Parse(HiddenFileID.Value);
    }
    set { _fileid = value; }
    }

    private void Page_Load(object sender, System.EventArgs e) {
    if (!IsPostBack) {
    HiddenFileID = new HtmlInputHidden();
    HiddenFileID.Value = _fileid.ToString();
    }
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer) {
    writer.Write("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
    writer.Write("<tr>\n");
    writer.Write(" <td>Some other text and things go here.\n");
    HiddenFileID.RenderControl(writer);
    writer.Write(" </td>\n");
    writer.Write("</tr>\n");
    writer.Write("</table>");
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    //
    InitializeComponent();
    base.OnInit(e);

    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }

    "MasterGaurav" wrote:

    > What do you mean by "dynamically creating controls" in Page_Load? Can
    > you show some code?
    >
    > --
    > Cheers,
    > Gaurav Vaish
    > http://www.mastergaurav.org
    > http://mastergaurav.blogspot.com
    > --------------------------------
    >
    >
     
    Ralph, Feb 23, 2005
    #3
  4. User contrls (*.ascx) are sure handy, but they aren't very reusable across
    projects.
    To create a class library (*.dll), you should start up Visual Studio and
    create a new project of type "class library". This will result in Custom
    controls instead of User controls, and they are much more reusable and much
    easier to extend which seems to be what you desire.

    Here's more info:
    http://SteveOrr.net/faq/UserCustom.aspx
    http://msdn.microsoft.com/library/d...l/vbconWebUserControlsVsCustomWebControls.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net


    "Ralph" <quakefest at ozemail dot com dot au> wrote in message
    news:...
    > Hi,
    >
    > I have been experimenting creating a User Control and overriding its
    > Render
    > method so that I have no reliance on an external ascx file and can package
    > this control into a dll.
    >
    > I dynamically create my controls in Page_Load() and everything works fine
    > while this code is part of my main application dll.
    >
    > My problem occurs when I move the code for the User control from my
    > application dll into my library dll.
    >
    > Within my User control I have a Hidden HTML input control. I can access
    > this
    > control and its value without any problems when the User control is part
    > of
    > my application dll, when I move the User Control into the library dll this
    > control doesn't keep its value, it is like it loses its ViewState or
    > something.
    >
    > I was wondering if there's something that I need to do in order to allow
    > me
    > to package the User Control into a dll other than my main web application
    > dll, so that all the controls maintain their state.
    >
    > thanks
    > Ralph
    >
     
    Steve C. Orr [MVP, MCSD], Feb 23, 2005
    #4
  5. Ralph

    MasterGaurav Guest

    I'm not quite sure... but try one thing:

    public int FileID
    {
    get {
    if(HiddenFileID != null)
    {
    return int.Parse(...);
    }
    return _fileID;
    }
    set
    {
    _fileID = value;
    if(HiddenFileID == null)
    {
    HiddenFileID = new HtmlInputHidden();
    }
    HiddenFileID = _fileID;
    }
    }


    And do not initialize the HiddenFileID in Page_Load.

    I think the issue is related to the order of execution. If the code
    above works then I know what's going on. :)


    --
    Cheers,
    Gaurav Vaish
    http://www.mastergaurav.org
    http://mastergaurav.blogspot.com
    --------------------------------
     
    MasterGaurav, Feb 23, 2005
    #5
  6. Ralph

    Ralph Guest

    RE: Custom User Control, Persistence and packaging into a dll

    Just a quick follow up, I figured out what was wrong.
    There was a few things that I corrected
    1) I wasn't adding the controls to the user control, I was just rendering
    them (does this add them anyway???)
    2) I now create the controls in OnInit() instead of Page_Load()
    3) I was changing the ID field of the control in OnInit() for some external
    javascript functions when it wasn't necessary. I just refactored code and
    removed the need for this.

    By doing these things my control now works great and data persistence is
    maintained between postbacks.

    thanks for the help and pointers
    Ralph

    "Ralph" wrote:

    > Hi,
    >
    > I have been experimenting creating a User Control and overriding its Render
    > method so that I have no reliance on an external ascx file and can package
    > this control into a dll.
    >
    > I dynamically create my controls in Page_Load() and everything works fine
    > while this code is part of my main application dll.
    >
    > My problem occurs when I move the code for the User control from my
    > application dll into my library dll.
    >
    > Within my User control I have a Hidden HTML input control. I can access this
    > control and its value without any problems when the User control is part of
    > my application dll, when I move the User Control into the library dll this
    > control doesn't keep its value, it is like it loses its ViewState or
    > something.
    >
    > I was wondering if there's something that I need to do in order to allow me
    > to package the User Control into a dll other than my main web application
    > dll, so that all the controls maintain their state.
    >
    > thanks
    > Ralph
    >
     
    Ralph, Mar 3, 2005
    #6
    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. Tina
    Replies:
    1
    Views:
    307
    Ken Dopierala Jr.
    Oct 31, 2004
  2. osi_nl
    Replies:
    0
    Views:
    1,209
    osi_nl
    Aug 19, 2006
  3. puzzlecracker
    Replies:
    2
    Views:
    317
  4. Tina

    packaging a project into a component

    Tina, Oct 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    125
  5. H. Simpson
    Replies:
    4
    Views:
    289
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page