Add attribute to the body failed

Total Post:135


 1311  View(s)
Rate this:

I have an web page.

The code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Works.Login" %>
<html xmlns="" >
   <head runat="server">
      <link rel="stylesheet" type="text/css" href="/styles/source/style.css" />
   <body id="PageBody" runat="server">
      <form id="form1" runat="server">
         <div style="text-align: center" >
And in code behind I want to add something to the body attribute.
protected void Page_PreRender(object sender, EventArgs e)
        if (PageBody != null)
            PageBody.Attributes.Add("class", "some_image");

However I found it doesn't work at all. I stepped through into the code and found there is an exception.

InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(((System.Web.UI.HtmlControls.HtmlGenericControl)(PageBody)))).InnerText' threw an exception of type 'System.Web.HttpException'}

Thanks for your help.

  1. Re: Add attribute to the body failed

    Hi Chintoo, 

    This works for me:

    PageBody.Attributes["class"] = "some_image";


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.