Custom User Control and packaging into a dll

R

Ralph

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
 
R

Ralph

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
}
 
S

Steve C. Orr [MVP, MCSD]

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
 
M

MasterGaurav

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
--------------------------------
 
R

Ralph

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top