Search code examples
c#asp.netcode-behind

aspx element is not recognized in code behind (asp.net)


I created an element with runat="server" tag but i cannot be recognized in the code behind.("time" is not recognized)

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="game.aspx.cs" Inherits="RapidTyper.game" %>

<!DOCTYPE html>
        <div runat="server">
    <a runat="server" id="time"></a>
        </div>
    <asp:Button ID="UpdateResults" runat="server"  ClientIDMode="Static" style="display:none;" OnClick="Button1_Click" Text="" />

code behind

 protected void Button1_Click(object sender, EventArgs e)
    {
        int score = time.Text;

    }

Solution

  • the element <a runat="server" id="time"></a> is not an asp.net element but an Html element that have no Text property.

    You can use the LinkButton <asp:LinkButton runat="server" ID="time2"></asp:LinkButton> that renders <a> Tag and have Text property.

    • also the <a> Tag is not an input element and is not post back anything - so the score = time.Text is not going to get anything other than the one you have all ready set previously.