HOY
HOY

Reputation: 1007

Editing the content of ASP.NET Control

Below is the content of my Default.aspx

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

</asp:Content>

I want to be able to find the control with ID "exampleID", and write something into it using c#.

Upvotes: 1

Views: 2754

Answers (2)

Sebastian Siek
Sebastian Siek

Reputation: 2075

You have few options here:

  1. Add runat="server"
  2. Add asp.net server side control "literal" inside p

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

    Don't use Label for that, as this will render with additional SPAN, which you don't want.

Hope it helps.

Upvotes: 1

Pankaj
Pankaj

Reputation: 10095

runat = "server" was missing in the tag to access it from code behind.

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# Code

exampleId.InnerText = "Your text";

Upvotes: 4

Related Questions