2012-03-05 13 views
0

私はasp.netのWebページを持っています。コード:本文に属性を追加できませんでした

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Works.Login" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
    <head runat="server"> 
     <title></title> 
     <link rel="stylesheet" type="text/css" href="/styles/source/style.css" /> 
    </head> 
    <body id="PageBody" runat="server"> 
     <form id="form1" runat="server"> 
     <div style="text-align: center" > 

そして、コードの背後にあるbody属性に何かを追加したいと思います。

protected void Page_PreRender(object sender, EventArgs e) 
{ 
     if (PageBody != null) 
     { 
      PageBody.Attributes.Add("class", "some_image"); 

しかし、私はそれが全く機能しないことを発見しました。私はコードを踏んで、例外があることを発見しました。

InnerTextプロパティ= '((System.Web.UI.HtmlControls.HtmlContainerControl)(((System.Web.UI.HtmlControls.HtmlGenericControl)(PageBody))))。InnerTextプロパティ' 'はタイプの例外をスローSystem.Web.HttpException '}

ありがとうございます。

+0

例外メッセージは何ですか? – sll

+0

InnerTextは 'System.Web.HttpException'型の例外をスローしました。 –

答えて

1

行いをPageLoadで、それは動作します。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (PageBody != null) 
     { 
      PageBody.Attributes.Add("class", "myClass"); 
     } 
    } 

EDIT:(opがそれABT doubtfullあるので)これが作動していることが示さへの完全なコード

私のASPXページ

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="SO.WebForm4" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <style> 
    body 
    { 
     background-color:Olive; 
    }  
    .myClass 
    { 
     background-color:Orange; 

    } 
    </style> 

</head> 
<body id="PageBody" runat="server"> 
    <form id="form1" runat="server"> 
    <div> 
     <h2>Testing styles</h2> 
     <p>If the bg color is Orange, code is working</p> 
    </div> 
    </form> 
</body> 
</html> 

分離コード

using System; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace SO 
{ 
    public partial class WebForm4 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (PageBody != null) 
      { 
       PageBody.Attributes.Add("class", "myClass"); 
      } 
     } 
    } 
} 

、ここれます出力

enter image description here

+0

いいえ、例外はまだ存在します。私はそれをテストしました。 http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet/2006-07/msg01325.html –

+0

@Love:マスターページですか?あなたはクエストオンに言及しておくべきです。もしそれが正常なページであれば、それはうまくいくでしょう。私はそれをテストした – Shyju

+0

彼らはマスターやノーマルと同じですが、私は正常なページでテストしたが、幸運はありません。あなたのマシンでそれをテストできますか?私はasp.net 3.5を使用します。 –

0

これは私の作品:

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

しかし、私のためには全く働いていません。 –

関連する問題