Showing posts with label lines. Show all posts
Showing posts with label lines. Show all posts

Saturday, March 31, 2012

Separate dynamic controls with a "<br>"

I'm adding controls to my page in code and needed <br> between the controls to put them on new lines. I just want to write <br>'s to the page without using the HtmlAnchor control. Is there a better way than this?

Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();

HtmlAnchor an1 = new HtmlAnchor();
HtmlAnchor an2 = new HtmlAnchor();

l1.Text = "Label1";
l2.Text = "Label2";
l3.Text = "Label3";

an1.InnerHtml = an2.InnerHtml = "<br>";

Page.Controls.Add(l1);
Page.Controls.Add(an1);
Page.Controls.Add(l2);
Page.Controls.Add(an2);
Page.Controls.Add(l3);You could encapsulate the controls inside User Controls, and simply have a <br> appended in the HTML there.
You could encapsulate the controls inside User Controls, and simply have a <br> appended in the HTML there.
OK now I have the controls in a user control so how can I append the HTML? I still couldn't think of a better way than what I already have.
Just right click and go into the HTML Source for the user control, and put <br> at the bottom of it :)
But the <br> needs to be loaded dynamically between every element inside the user control. I thought I could just use some sort of HTML writer to write between the elements on the page.
Create a Generic Html Control HtmlControl namespace set it up as a <br> and add it between the controls.

If it needs to be added for every added control override the add/added control member and do it there.
Take the time to scroll down your toolbox until you find the Literal Control. That control is rarely seen because you normally have to scroll down to it. You see, it literally outputs what you put into it. So, you can put html into it, and it spits it out into the response stream just as you had put it in the .Text property. This is useful if you want to display html content from a database, or do exactly what you want to do. Example:

Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();

LiteralControl lc1= new LiteralControl ();
LiteralControl lc2= new LiteralControl ();

l1.Text = "Label1";
l2.Text = "Label2";
l3.Text = "Label3";

lc1.Text= lc2.Text= "<br>";

Page.Controls.Add(l1);
Page.Controls.Add(lc1);
Page.Controls.Add(l2);
Page.Controls.Add(lc2);
Page.Controls.Add(l3);

Thursday, March 29, 2012

Seperate Content from Code

After nearing completion of my first full .Net web application I am left with a page consisitng of around 600 lines of code, approximately split half and half between VB script and HTML page content.

Can anyone point me in the right direction of the best way to encapsulate the VB code into its own seperate file and then rerefence this from the form in the HTML page?

Hi,

you could place the code into separate aspx.vb file into its own class deriving form System.Web.UI.Page. Then putting the aspx to inherit from this code-behind class (with Inherits directive) would work.

Say you have files

page1.aspx (important ones are CodeFile and Inherits attributes)

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" %>

...

page1.aspx.vb

Partial Class page1

Inherits System.Web.UI.Page

End Class

When you could put code into aspx.vb file into the class.

More information :http://www.asp.net/QuickStart/aspnet/doc/pages/codebehind.aspx

Of course if have code & logic which is such that it's common to more than one form, you might want to consider having a separate component (class library or in App_Code) where you could centralize logic that is needed in multiple places.


Thanks joteke, I think I'm slowly getting there.

So the .aspx file inherits the class that you create in the aspx.vb file ?

Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

What does the'AutoEventWireup' attribute do?


Also, do i need to import the namespaces in the aspx file or the aspx.vb file ?

duncanhall:

So the .aspx file inherits the class that you create in the aspx.vb file ?

Yes. Basically with partial classes in ASP.NET 2.0 (when using CodeFile attribute) a second partial class is created. This class contains control declarations for controls residing on aspx (removes the need for you to declare them on aspx.vb as you had to do with ASP.NEt v1.x).

These two partial classes are merged when compilation occurs and are used as base class for class which is created when the actual aspx is parsed and compiled (yes, basically the aspx itself)

duncanhall:



Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

Yes, note that the code-behind class is now equally part of your page.

duncanhall:


What does the'AutoEventWireup' attribute do?

It indicates whether Page's event are autowired. If enabled, it means that you have a method which matches Page_[EventName] or [ControlID]_[EventName] name so that method would automatically be assigned as event handler for the correspondng event of P?ager or the Control. Normally these event methods need to be wired explicitly.

See:ASP.NET Web Server Control Event Model


If code is in code-behind, then you import in code-behind.
Thanks a lot joteke, everything seems to make sense now, and my first app is pretty much finished!