Change id of elements in ContentPlaceHolder

543 views Asked by At

I use masterpage in asp.net web form, I use span to display message to users

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">

     <div class="col-md-10 col-sm-10">
         <span id="spnMessage" class="btnText" runat="server"></span>
     </div>

</asp:Content>

and code is:

spnMessage.InnerText = "Add record successfully ";

but in runtime id of span change to "ContentPlaceHolder2_spnMessage" and my code does not work. What should I do?

1

There are 1 answers

0
VDWWD On

Change the span into a Literal. Then there will be no more issues with ID conflicts.

<asp:Literal ID="spnMessage" runat="server"></asp:Literal>

Then accessing the Literal from code behind will always work.

spnMessage.Text = "Add record successfully ";

Or if you need to use the <span> element for CSS purposes, you can still wrap the Literal with it.

<span><asp:Literal ID="spnMessage" runat="server"></asp:Literal></span>

You could also use a aspnet Label. A label will generate it's own <div> tag in HTML.

<asp:Label ID="spnMessage" runat="server" Text="Add record successfully"></asp:Label>

Will become

<div>Add record successfully</div>