Pass double quote from VB to HTML
-
can i do that inside a do while loop like this...
Do While lbl <= 7 Dim newlbl As New Label newlbl.ID = "newlbl" & lnum newlbl.Text = lnum newlbl.BackColor = System.Drawing.Color.Blue newlbl.BorderStyle = BorderStyle.Inset newlbl.BorderColor = System.Drawing.Color.Black newlbl.BorderWidth.Pixel(1) Page.Controls.Add(newlbl)
Absolutely. Just keep in mind that dynamic controls need to be recreated each trip back to the server. ASP.NET will not recreate them on postback. --Jesse
-
Absolutely. Just keep in mind that dynamic controls need to be recreated each trip back to the server. ASP.NET will not recreate them on postback. --Jesse
well that code i posted doesnt work. Nothing shows up on the screen
-
well that code i posted doesnt work. Nothing shows up on the screen
The equivilent C# code works just fine for me. Two things that strike me from the code you posted. The line
newlbl.BorderWidth.Pixel(1)
isn't correct. The code should read:newlbl.BorderWidth = Unit.Pixel(1)
, asPixel
is a static (shared) method of the Unit class. I also don't see anEnd While
in your code. I don't know if either is the cause of your problems, but they should be fixed none the less. You really should consider settingOption Explicit
andOption Strict
on all of the time, if you don't already. They will save you from alot of subtle problems. The rest of your example looks fine to me. If correcting the two points that I mentioned doesn't fix your problem, I'd suggest you start debugging. For example, you don't see anything in the browser. Have you done a "view source" and checked the HTML? Just in case you find it helpful, here is the C# translation of your code that I used to verify.private void Page_Load(object sender, System.EventArgs e)
{
Label label = null;
for(int index = 0; index < 7; index++)
{
label = new Label();
label.Text = index.ToString();
label.BackColor = System.Drawing.Color.Blue;
label.BorderStyle = BorderStyle.Inset;
label.BorderColor = System.Drawing.Color.Black;
label.BorderWidth = Unit.Pixel(1);
-
The equivilent C# code works just fine for me. Two things that strike me from the code you posted. The line
newlbl.BorderWidth.Pixel(1)
isn't correct. The code should read:newlbl.BorderWidth = Unit.Pixel(1)
, asPixel
is a static (shared) method of the Unit class. I also don't see anEnd While
in your code. I don't know if either is the cause of your problems, but they should be fixed none the less. You really should consider settingOption Explicit
andOption Strict
on all of the time, if you don't already. They will save you from alot of subtle problems. The rest of your example looks fine to me. If correcting the two points that I mentioned doesn't fix your problem, I'd suggest you start debugging. For example, you don't see anything in the browser. Have you done a "view source" and checked the HTML? Just in case you find it helpful, here is the C# translation of your code that I used to verify.private void Page_Load(object sender, System.EventArgs e)
{
Label label = null;
for(int index = 0; index < 7; index++)
{
label = new Label();
label.Text = index.ToString();
label.BackColor = System.Drawing.Color.Blue;
label.BorderStyle = BorderStyle.Inset;
label.BorderColor = System.Drawing.Color.Black;
label.BorderWidth = Unit.Pixel(1);
Thanks for all your help. It is greatly appriciated. I have looked at the source code and there is nothing there that should be. I have no idea whats going on with this haha. I suppose i'll have to find another way to do what i need done. Again thanks Tommy
-
Thanks for all your help. It is greatly appriciated. I have looked at the source code and there is nothing there that should be. I have no idea whats going on with this haha. I suppose i'll have to find another way to do what i need done. Again thanks Tommy
My pleasure. I hope things work out for you. :) --Jesse
-
well that code i posted doesnt work. Nothing shows up on the screen
Hi there, There are two simple things come to mind: + The code inside the while loop does not actually get reached at all. You can simply run your application in debug mode and see if that happens. + If you put this code in a custom control, then you can check if the custom control is added to the page.
-
Hi there, There are two simple things come to mind: + The code inside the while loop does not actually get reached at all. You can simply run your application in debug mode and see if that happens. + If you put this code in a custom control, then you can check if the custom control is added to the page.
Ok if i put it straight into my VB form code it renders it. If i put it into a sub procedure and call it from the HTML using the <% %> it doesnt create my new objects. This is all so new to me. Do you know why it would do this?
-
Ok if i put it straight into my VB form code it renders it. If i put it into a sub procedure and call it from the HTML using the <% %> it doesnt create my new objects. This is all so new to me. Do you know why it would do this?
The reason those label controls cannot be seen at the client side is that the code to create those controls runs after the Page_PreRender event of the Page life cycle. When the page contents are rendered at the Render phase, the labels are not actually added to the page so you don't see them on the screen, you can see that when running your application in debug mode. Here, you may want to place the labels at the place where you put the code
<% %>
in the page, right? There are two simple ways to work around this: + You can use a container such asPlaceHolder
control in the page where you want to put the labels, the sample code in code behind is like this:Private Sub Page\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here RenderLabelList1() End Sub Protected Sub RenderLabelList1() Dim lnum As Integer = 0 Do While lnum <= 7 Dim newlbl As New Label newlbl.ID = "newlbl" & lnum newlbl.Text = lnum newlbl.BackColor = System.Drawing.Color.Blue newlbl.BorderStyle = BorderStyle.Inset newlbl.BorderColor = System.Drawing.Color.Black newlbl.BorderWidth.Pixel(1) 'Page.Controls.Add(newlbl) PlaceHolder1.Controls.Add(newlbl) lnum = lnum + 1 Loop End Sub
+ You can write out the rendered html markup of the labels to the page, the sample code in code-behind
Protected Function RenderLabelList() As String Dim lnum As Integer = 0 Dim writer As System.IO.StringWriter = New System.IO.StringWriter Dim buffer As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(writer) Do While lnum <= 7 Dim newlbl As New Label newlbl.ID = "newlbl" & lnum newlbl.Text = lnum newlbl.BackColor = System.Drawing.Color.Blue newlbl.BorderStyle = BorderStyle.Inset newlbl.BorderColor = System.Drawing.Color.Black newlbl.BorderWidth.Pixel(1) 'Page.Controls.Add(newlbl) newlbl.RenderControl(buffer) lnum = lnum + 1 Loop Return writer.ToString() End Function
The markup in the web page for both two ways is simple like this:
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder id="Pl -
Ok if i put it straight into my VB form code it renders it. If i put it into a sub procedure and call it from the HTML using the <% %> it doesnt create my new objects. This is all so new to me. Do you know why it would do this?
Minh's advice is dead-on. I just wanted to add one additional point. The use of <% %> is not considered a best practice and should be avoided if at all possible. Each time a <% %> is encountered, context switching occurrs which carries a performance penalty. If you need to inject controls at a certain location, you would be served far better by placing a container object into the HTML and manipulating it from the code-behind. For example, you could place a panel where you need to add the labels by doing
<asp:Panel id = "labelHolder" runat = "server" />
. In your code-behind, you would declare a protected variable of type Panel with the same name as the id:Dim labelHolder as Panel
. To place your labels you would just add them to the panel's control collection:labelHolder.Controls.Add(myLabel)
. Hope that helps. :) --Jesse -
Minh's advice is dead-on. I just wanted to add one additional point. The use of <% %> is not considered a best practice and should be avoided if at all possible. Each time a <% %> is encountered, context switching occurrs which carries a performance penalty. If you need to inject controls at a certain location, you would be served far better by placing a container object into the HTML and manipulating it from the code-behind. For example, you could place a panel where you need to add the labels by doing
<asp:Panel id = "labelHolder" runat = "server" />
. In your code-behind, you would declare a protected variable of type Panel with the same name as the id:Dim labelHolder as Panel
. To place your labels you would just add them to the panel's control collection:labelHolder.Controls.Add(myLabel)
. Hope that helps. :) --JesseThanks to both of you. I am now able to atleast get the labels on the page! haha. Now i just need to figure out how to get them in the rows and columns needed. lol. Again, thanks for all your time and help Tommy