all in code behind?

drewpecunia

New member
Joined
May 7, 2003
Messages
4
Location
Issaquah
As an exercise in learning I am trying to put all the code for a page in .cs and then compile it to a dll. Then I call that dll from the bin directory into an aspx page.

I can get my cs to compile but on return my page is blank. I think there might be something wrong with my .cs file--but can not see anything right off. Can any of you see where I might be off a little. (ok-well with my code) :)

using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace myPage
{

public class myPage:Control
{
protected System.Web.UI.WebControls.TextBox txtTextBox;
protected System.Web.UI.WebControls.Label lblLabel;

public void CreateChildControls(HtmlTextWriter stringWriter)
{

// Use htmlWriter to write HTML.
string strOpenHTML;
strOpenHTML = "<html><head><title>My Page</title></head>";
strOpenHTML += "<body>Enter some text:";
this.Controls.Add( new LiteralControl( strOpenHTML ) );

// Add HTMLForm Tag
HtmlForm frmForm = new HtmlForm();
frmForm.ID = "myForm";
Controls.Add( frmForm );


// Add a TextBox
txtTextBox.ID = "myTextBox";
frmForm.Controls.Add( txtTextBox );

// Add a Button
Button btnButton = new Button();
btnButton.Text = "Click Here!";

//AddHandler btnButton.Click, AddressOf Button_Click;
btnButton.Click += new EventHandler(Button_Click);
frmForm.Controls.Add( btnButton );
btnButton.Text = "Click Here!";
frmForm.Controls.Add( btnButton );

// Add Page Break
frmForm.Controls.Add( new LiteralControl( "<p>" ) );

// Add a Label
lblLabel.ID = "myLabel";
frmForm.Controls.Add( lblLabel );

// Add Closing HTML Tags
string strCloseHTML;
strCloseHTML = "</form></body></html>";
Controls.Add( new LiteralControl( strCloseHTML ) );
}

void Button_Click( object s, EventArgs e )
{
lblLabel.Text = txtTextBox.Text;
}

}
 
Thanks for your quick response! :)

Ok I did that but I still get a blank page. Do you think I might be calling the literal controls wrong? I think I am creating the Textbox correctly at the top and calling it correctly in code.
 
You also never call that sub that creates the control. Try to make a sub routine that handles MyBase.Init and make a call to your control creating sub.
Also, wouldnt it be easier to let VS.NET do this for you? :)
 
Let me keep trying. I am trying your suggestions and I will get back to you soon. I know I am missing simple things. Thanks for your patience and help.
 
This may be obvious but did you tell your asp.net page what is and where is the file for code behind?
 
Back
Top