Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Custom User Control and packaging into a dll

Reply
Thread Tools

Custom User Control and packaging into a dll

 
 
Ralph
Guest
Posts: n/a
 
      02-22-2005
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

 
Reply With Quote
 
 
 
 
MasterGaurav
Guest
Posts: n/a
 
      02-22-2005
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
--------------------------------

 
Reply With Quote
 
 
 
 
Ralph
Guest
Posts: n/a
 
      02-23-2005
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
> --------------------------------
>
>

 
Reply With Quote
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      02-23-2005
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/de...ebControls.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:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
MasterGaurav
Guest
Posts: n/a
 
      02-23-2005
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
--------------------------------

 
Reply With Quote
 
Ralph
Guest
Posts: n/a
 
      03-03-2005
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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Turning an ascx user control into a redistributable custom control dll problems osi_nl ASP .Net 0 08-19-2006 03:50 PM
Custom User Control is not rendered inside another user control in a server table. Leeor Chernov ASP .Net 2 10-16-2005 08:35 AM
Why does Ruby use both tcl83.dll and tk83.dll (instead of just tk83.dll)? H. Simpson Ruby 4 08-03-2004 04:45 PM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM



Advertisments