Saturday, March 31, 2012
Separate dynamic controls with a "<br>"
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
Seperating Business Logic From Presentation (Using Code Behind)
If i were designing a database driven application in Classic ASP i would write something like the following to access and display the data...
</CODE>
<%
Dim rs
Dim rsArray
Dim i
set rs = server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = Application("connStr")
SQL = "SELECT * FROM...."
rs.Open strSQL
if not (rs.BOF and rs.EOF) Then
rsArray = rs.getRows()
end if
rs.Close
if isArray(rsArray) Then
for i = lbound(rsArray,2) tp ubound(rsArray,2)
%>
<tr>
<td class="class1"><%=rsArray(0,i)%></td>
<td class="class2"><%=.....%></td>
<td rowspan=2><img src=...></td>
</tr>
<%
next
end if
%>
</CODE
Question... To my knowledge the point of code-behind is to seperate the Server-side Code from the Client-Side code.
To that end... how would you go about doing that in ASP.NET? It just doesn't make sense to me.
Even if you did a page_load event that accessed the data how would you then pass that data to the client side code so that you could format it and place it in a table with other client side objects in it like pictures? Or dynamically craete the name of the pictures in the table but doing somethign like <img src="http://pics.10026.com/?src=pic<%=rsArray...%>"...> etc
This part of ASP.Net completely confuses me.I presume i need to use one of the data controls?
Yes, you can use datagrids and repeaters to do the same thing. Get data from your datasource then databind to a repeater or datagrid.
You can also databind to all sorts of other controls (dropdown lists, check box lists, radio lists...)
and using those controls i can format/arrange my data as i want?
is there a simple coding example?
Yeah, the tutorials on this site have some examples.
You can format the data any way you want. It'll take some getting use to. You'd use data binding syntax instead of <% %>, which actually still feels like you're mixing code in with html... but it's supposed to be better somehow.