2012-02-27 49 views
1

的內容下面是我希望能夠找到控制ID爲「exampleID」,並使用C#寫的東西進去我的Default.aspx編輯ASP.NET控件

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <h2> 
     WELCOME  
    </h2> 
    <p id="exampleId"> 
     I want to edit here 
    </p> 

</asp:Content> 

的內容。

+2

將它變成一個'runat =「服務器」'控件。 – Oded 2012-02-27 13:25:31

+0

使用asp.net提供的控件之一怎麼樣?像''可以通過exampleId.Text =「Hello,World!」來訪問;' – aweis 2012-02-27 13:28:14

+0

請不要在標題中包含C#標籤,將其標記爲C#謝謝! – 2012-02-27 13:52:42

回答

4

runat =「server」在標籤中缺失以從後面的代碼訪問它。

HTML

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <h2> 
     WELCOME  
    </h2> 
    <p id="exampleId" runat = "server"> 
     I want to edit here 
    </p> 

</asp:Content> 

C#代碼

exampleId.InnerText = "Your text"; 
1

你幾個選擇這裏:

  1. 添加RUNAT =「服務器」
  2. 添加asp.net服務器端控件「文本」內部p

    <p> 
        <asp:Literal ID="exampleId" runat="server" /> 
    </p> 
    

    不要使用標籤爲,因爲這將額外SPAN渲染,你不要不想要。

希望它有幫助。