Custom User Control and packaging into a dll

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

  1. Ralph

    Ralph Guest


    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

    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.

    Ralph, Feb 22, 2005
    1. Advertisements

  2. Ralph

    MasterGaurav Guest

    MasterGaurav, Feb 22, 2005
    1. Advertisements

  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(" <td>Some other text and things go here.\n");
    writer.Write(" </td>\n");

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


    /// <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);
    Ralph, Feb 23, 2005
  4. User contrls (*.ascx) are sure handy, but they aren't very reusable across
    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:
    Steve C. Orr [MVP, MCSD], Feb 23, 2005
  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;
    _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. :)

    Gaurav Vaish
    MasterGaurav, Feb 23, 2005
  6. Ralph

    Ralph Guest

    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, Mar 3, 2005
    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.