简体   繁体   中英

Event not getting fired in code behind

PnlImages.Controls.Add(new LiteralControl("  <img id=\"img\" class=\"content\" src=\"" + url + "\" runat=\"server\" name=\"img\" onclick=\"return onClick(this);\" />"));


public void onClick(object sender)
{

}

debugger is not firing instead giving error "object expected".
Please help.

The runat=\\"server\\" attribute is not an HTML attribute, but an instruction for ASP.Net to instantiate ASP.Net controls (System.Web.UI namespace) which generate not only HTML, but also ViewState, event handlers etc.

The onclick attribute refers to a JavaScript function that needs to be implemented in HTML or a linked .js file, and not an OnClick method in the C# back-end code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM