Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building some html and assigning it to the inner html of a div:

private string BuildTab1MiddleCol()
{
   StringBuilder buttonStr = new StringBuilder();
   buttonStr.Append("<div class=\"w3-col s2a\">");
   buttonStr.Append("  <div class=\"w3-container\"> <p>");
   buttonStr.Append(" <input type=\"image\" id=\"soft_code\" runat=\"server\" src=\"./Images/checkmarkUnchecked.png\" onserverclick=\"btnTracking_OnClick\" />");
   buttonStr.Append(" </p> </div>");
   buttonStr.Append(" </div>");
   return buttonStr.ToString();
}


When I click on the image it does execute the Page_Load event but does not execute the method btnTracking_OnClick.

if I take the string and paste it on the page itself:
<input type="image" id="hardCode" runat="server" src="./Images/checkmarkUnchecked.png" onserverclick="btnTracking_OnClick" />

the btnTracking_OnClick event fires.

What am I missing

What I have tried:

The usual chasing of answers up and down the web.
Posted
Updated 14-Feb-19 8:40am

Attributes like "runat" only work when they are put on server-side controls, ie ones in your mark-up like "asp:Image", what you are doing is rendering html with the runat tag so you are putting that attribute on the client-side control, but the web browser doesn't know what to do with that so ignores it.

If you want dynamic server-side controls you have to add them to a container object in your server code. Google for "asp.net create dynamic server-side control" and you'll find lots of examples.
 
Share this answer
 
well looks like they only way I could do it was to use placeholder and not write to a div inner html. So the method will now look like this:

private void BuilTab1dMiddleCol(PlaceHolder  phHolder, int nhID)
        {
    string id = "nh_" + nhID;
    ImageButton imgBtncheckOff = new ImageButton {ID = id, ImageUrl = "./Images/checkmarkUnchecked.png"};
    imgBtncheckOff.Click+= new ImageClickEventHandler(btnTracking_OnClick);
     phHolder.Controls.Add(new LiteralControl("<div class=\"w3-col s2a\">"));
    phHolder.Controls.Add(new LiteralControl("<div id=\"div_chk \" class=\"w3-container\"> <p>"));
    phHolder.Controls.Add(imgBtncheckOff);
    phHolder.Controls.Add(new LiteralControl(" </p> </div>"));
    phHolder.Controls.Add(new LiteralControl("</div>"));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900