Anchor tag's onClick is not working

I am creating an anchor tag on the fly. It gets created, but for some reason the onClick is not working.

Here is my generation code:

HtmlGenericControl emailsubject = new HtmlGenericControl("div");
emailsubject.ID = count++ + "emailSubject";
emailsubject.InnerHtml = "Subject: " 
    + "<a id=\"summa\" href=\"#\" onClick=\"subject_Click();\">" 
    + results2["EmailSubject"].ToString() 
    + "</a>";

Here is my onClick function:

public void subject_Click()
{
    Response.Write("Clicked");
}

Upvotes: 1

Views: 726

Answers (1)

Pseudonym
Pseudonym

Reputation: 2072

The way you have it set up right now, you're trying to call a javascript function from an html element. Try either initializing the onclick function of the control you are creating or make use of another asp.net control (ASP:Hyperlink) in order to call functions in code behind.

Upvotes: 2

Related Questions