2011-12-12 14 views
3

コードビハインドからaspxファイルにHTMLタグを追加するにはどうすればよいですか?コードビハインドでHTMLタグを動的に作成する

私は新しいオブジェクトを作成

Graph MyChart = new Graph(); 

私はそれがこのオブジェクト

<Graph id="MyChart" runat="server" Height="500px"></Graph> 

そのための解決策は何のためにタグを追加したいですか?

+0

が重複する可能性をhttp://stackoverflow.com/questions/8464921/how-to-add-a-html-attribute-from-an-aspx-cs –

答えて

4

私たちが.NETコントロールやHTMLについて話しているのかどうかは不明ですが、私は両方の例を挙げています。

これはページの最後に追加されますが、私はあなたはそれが追加される場所を制御するためにPlaceHolderを使用することをお勧め:

Graph MyChart = new Graph(); 
MyChart.ID = "MyChart"; 
Page.Controls.Add(MyChart); 

//genericcontrol example 
HtmlGenericControl NewControl = new HtmlGenericControl("graph"); 

// Set the properties of the new HtmlGenericControl control. 
NewControl.ID = "MyGraph"; 
Page.Controls.Add(NewControl); 

PlaceHolder例:

<form id="form1" runat="server"> 
     <h3>PlaceHolder Example</h3> 
     <asp:PlaceHolder id="PlaceHolder1" 
      runat="server"/> 
    </form> 

    protected void Page_Load(Object sender, EventArgs e) 
    { 
    Graph MyChart = new Graph(); 
    MyChart.ID = "MyChart"; 
    PlaceHolder1.Controls.Add(MyChart); 

    //genericcontrol example 
    HtmlGenericControl NewControl = new HtmlGenericControl("graph"); 

    // Set the properties of the new HtmlGenericControl control. 
    NewControl.ID = "MyGraph"; 
    PlaceHolder1.Controls.Add(NewControl); 

    } 
+0

ありがとうございます。 runat = "server" Height = "510px" ..のような属性を追加したいのですが? – Hzyf

+0

runat = "server"はサーバー側の.netコントロールで自動的に公開されます。パブリックプロパティは上記のClientIDのように評価でき、HTML属性は次のように追加できます:MyChart.Attributes.Add( "style"、 "height:510px") ; –

+0

MyChart.ClientID = "MyChart"という行にエラーがあります。 "は読み込めません"と指定することはできません。これについて助けてください。 – Hzyf

関連する問題