Thursday, March 29, 2012
Newbie: Is it possible to set <body> attributes via code behind?
of the <body> tag via code-behind. I am currently setting the onload event i
n
the body tag of an ASP.NET web form using old-fashion asp type <%.. code...
%> blocks.
This "spaghetti-scripting" is difficult to manage, and is essentially a
hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
further causes problems when i try to wire events.
I tried, for example, to embbed an <asp:label /> and other controls in the
<body> tag, but VS.NET doesn't let me (probably for good reason too).
Anyone have ideas?
Thanks,<body id="body" runat="server">
codebehind:
protected body as HtmlGenericControl
Page_Load..
body.Attributes.Add("onLoad", "alert('Karl for the win!');")
Karl
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
values
> of the <body> tag via code-behind. I am currently setting the onload event
in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
values
> of the <body> tag via code-behind. I am currently setting the onload event
in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
Give the body an id and a runat=server. For example <body id=body1
runat=server>. In codebehind (vb) put
Protected body1 as htmlgenericcontrol in your declarations. Then you can set
any attribute that you can set in html. Here is an example of setting the
onload attribute.
body1.attributes("onload") ="java script:doSomething();return false;"
Mike
throw a RUNAT=SERVER in it and declare it in the codebehind. It should
work...
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
> values
> of the <body> tag via code-behind. I am currently setting the onload event
> in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
> code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
Mark <body runat=server id=_body> then manually in your codebehind add:
protected System.Web.UI.HtmlControls.HtmlGenericControl _body;
And then you should be abe to use _body.Attributes.Add() and any other API
to modify the element.
-Brock
DevelopMentor
http://staff.develop.com/ballen
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
> values of the <body> tag via code-behind. I am currently setting the
> onload event in the body tag of an ASP.NET web form using old-fashion
> asp type <%.. code... %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially
> a hack. And, i cannot view the HTML in DESIGN view, only CODE view.
> Which further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in
> the <body> tag, but VS.NET doesn't let me (probably for good reason
> too).
> Anyone have ideas?
> Thanks,
>
"Karl for the win!" ? hehehehe I like it! :}
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23AhK3h8LFHA.2772@.TK2MSFTNGP12.phx.gbl...
> <body id="body" runat="server">
> codebehind:
> protected body as HtmlGenericControl
> Page_Load..
> body.Attributes.Add("onLoad", "alert('Karl for the win!');")
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "charliewest" <charliewest@.discussions.microsoft.com> wrote in message
> news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> values
> in
> code...
>
Newbie: Is it possible to set <body> attributes via code behind?
of the <body> tag via code-behind. I am currently setting the onload event in
the body tag of an ASP.NET web form using old-fashion asp type <%.. code...
%> blocks.
This "spaghetti-scripting" is difficult to manage, and is essentially a
hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
further causes problems when i try to wire events.
I tried, for example, to embbed an <asp:label /> and other controls in the
<body> tag, but VS.NET doesn't let me (probably for good reason too).
Anyone have ideas?
Thanks,<body id="body" runat="server"
codebehind:
protected body as HtmlGenericControl
Page_Load..
body.Attributes.Add("onLoad", "alert('Karl for the win!');")
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
values
> of the <body> tag via code-behind. I am currently setting the onload event
in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
values
> of the <body> tag via code-behind. I am currently setting the onload event
in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
Give the body an id and a runat=server. For example <body id=body1
runat=server>. In codebehind (vb) put
Protected body1 as htmlgenericcontrol in your declarations. Then you can set
any attribute that you can set in html. Here is an example of setting the
onload attribute.
body1.attributes("onload") ="javascript:doSomething();return false;"
Mike
throw a RUNAT=SERVER in it and declare it in the codebehind. It should
work...
--
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"charliewest" <charliewest@.discussions.microsoft.com> wrote in message
news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
> values
> of the <body> tag via code-behind. I am currently setting the onload event
> in
> the body tag of an ASP.NET web form using old-fashion asp type <%..
> code...
> %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially a
> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
> further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in the
> <body> tag, but VS.NET doesn't let me (probably for good reason too).
> Anyone have ideas?
> Thanks,
Mark <body runat=server id=_body> then manually in your codebehind add:
protected System.Web.UI.HtmlControls.HtmlGenericControl _body;
And then you should be abe to use _body.Attributes.Add() and any other API
to modify the element.
-Brock
DevelopMentor
http://staff.develop.com/ballen
> I'm sure it's possible, but it hasn't occurred to me "how" to set the
> values of the <body> tag via code-behind. I am currently setting the
> onload event in the body tag of an ASP.NET web form using old-fashion
> asp type <%.. code... %> blocks.
> This "spaghetti-scripting" is difficult to manage, and is essentially
> a hack. And, i cannot view the HTML in DESIGN view, only CODE view.
> Which further causes problems when i try to wire events.
> I tried, for example, to embbed an <asp:label /> and other controls in
> the <body> tag, but VS.NET doesn't let me (probably for good reason
> too).
> Anyone have ideas?
> Thanks,
"Karl for the win!" ? hehehehe I like it! :}
--
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Karl Seguin" <karl REMOVE @. REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%23AhK3h8LFHA.2772@.TK2MSFTNGP12.phx.gbl...
> <body id="body" runat="server">
> codebehind:
> protected body as HtmlGenericControl
> Page_Load..
> body.Attributes.Add("onLoad", "alert('Karl for the win!');")
> Karl
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/ - New and Improved (yes, the popup is
> annoying)
> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
> come!)
> "charliewest" <charliewest@.discussions.microsoft.com> wrote in message
> news:2E9D247C-4445-43A3-935D-B676C8ADE617@.microsoft.com...
>> I'm sure it's possible, but it hasn't occurred to me "how" to set the
> values
>> of the <body> tag via code-behind. I am currently setting the onload
>> event
> in
>> the body tag of an ASP.NET web form using old-fashion asp type <%..
> code...
>> %> blocks.
>>
>> This "spaghetti-scripting" is difficult to manage, and is essentially a
>> hack. And, i cannot view the HTML in DESIGN view, only CODE view. Which
>> further causes problems when i try to wire events.
>>
>> I tried, for example, to embbed an <asp:label /> and other controls in
>> the
>> <body> tag, but VS.NET doesn't let me (probably for good reason too).
>>
>> Anyone have ideas?
>>
>> Thanks,
Monday, March 26, 2012
Newbie: parser error when using a Literal tag
I wanted to add a Literal tag to a page like in this article:
http://www.odetocode.com/Code/82.aspx
But I keep getting a parser error on my page:
Parser Error Message: The base class includes the field 'stylesheet', but its type (System.Web.UI.WebControls.Literal) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).
Here's my code:
-- from .aspx:
<LINK href="http://links.10026.com/?link=<asp:Literal id="stylesheet" runat="Server" />" type="text/css" rel="stylesheet">
-- from .aspx.vb:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web.Security
Imports blueinc.BusinessLogicLayer
Imports blueinc.DataAccessLayerNamespace myCompany.Web
Public Class _default
Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()End Sub
Protected WithEvents dlEvents As System.Web.UI.WebControls.DataList
Protected WithEvents PlaceHolder As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents lnkRegister As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblUserLoggedOn As System.Web.UI.WebControls.Label
Protected WithEvents Separator As System.Web.UI.WebControls.Label
Protected WithEvents lnkLogOff As System.Web.UI.WebControls.LinkButton
Protected WithEvents dgClinics As System.Web.UI.WebControls.DataGrid'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.ObjectPrivate Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub#End Region
Dim EventsTable As BusinessLogicLayer.Events = New BusinessLogicLayer.Events
Public ClinicsTable As BusinessLogicLayer.Clinics = New BusinessLogicLayer.Clinics
Protected stylesheet As System.Web.UI.WebControls.LiteralPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim uc As Control = Page.LoadControl("UserControls/Login.ascx")If Not Request.IsAuthenticated Then
PlaceHolder.Controls.Add(uc)
lblUserLoggedOn.Visible = False
lnkLogOff.Visible = False
Else
Dim UserDetails As myCompany.BusinessLogicLayer.UserDetails
UserDetails = Session("UserDetails")PlaceHolder.Controls.Remove(uc)
If Not IsNothing(UserDetails.UserName) Then
lblUserLoggedOn.Text = "Currently logged on as " & UserDetails.UserName
Else
lblUserLoggedOn.Text = "Authenticated but logged on as NO-ONE!"
End If
lblUserLoggedOn.Visible = True
lnkLogOff.Visible = TrueEnd If
If Not IsPostBack Then
dlEvents.DataSource = EventsTable.GetList
dlEvents.DataBind()dgClinics.DataSource = ClinicsTable.GetList
dgClinics.DataBind()Dim OrgName As String
OrgName = Session("org")
If OrgName = "" Then
OrgName = Request("org")
End If
If OrgName = "" Then
OrgName = "foo"
End Ifstylesheet.Text = OrgName & "/styles.css"
End IfEnd Sub
I would have snipped the code-behind, but I figured I'd end up snipping the offending line... I'd be happy to provide more info if required.
J<LINK href="http://links.10026.com/?link=" type="text/css" rel="stylesheet" id="stylesheet" runat="server" />
Protected WithEvents stylesheet As System.Web.UI.HtmlControls.HtmlGenericControl' code:
stylesheet.Attributes.Add("href", "whatever value")
I think you're confusing the parser with nested tags. Try to make stylesheet a string instead of a literal and inject its value using <%= %>.
Even better, make the LINK tag runat=server (and declare it as a HtmlGenericControl in the code-behind) and set its properties using the Attributes collection.
Thank you both, its working now. I am making the link runat=server.
And when I took the literal out of the <link/> it also worked, so at least I know what the problem was!
Cheers,
J