简体   繁体   中英

Get HTML button in aspx.cs page

Can I get a button or anchor written in pure HTML (with no runat=server) from my backend C# code?

I mean, my button is in aspx page and I want to get the button in aspx.cs page.

No you cannot get html controls not decorated with runat="server" in code-behind.
The FindControl method also works for only those controls that are marked runat="server" .

I think you can do it by using jquery ajax and webmethod like below

write in aspx page below code on button click

<script language="javascript" type="text/javascript"> 
$(document).ready(function() 
{
 $("#btnGetHTML").click(function()
   {
       $.ajax({
                url: "YourPAge.aspx/GetHTML",
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) 
                         {
                              //You will get html code here 
                              alert("success: " + data.d); 
                         },
                failure: function (data) 
                         {
                                alert("Failure : " + data.d);
                         },
                error: function (data) 
                        {
                                alert("Error3 : " + data.d);
                         }
         });
   }
}
</script>

aspx.cs code

[WebMethod]
public static string GetHTML()
{
  string html="<html><body></body></html>" 
  return html; 
}

I've always believed it was there more for the understanding that you can mix ASP.NET tags and HTML Tags, and HTML Tags have the option of either being runat="server" or not. It doesn't hurt anything to leave the tag in, and it causes a compiler error to take it out. The more things you imply about web language, the less easy it is for a budding programmer to come in and learn it. That's as good a reason as any to be verbose about tag attributes.

the importance of is more for consistency and extensibility.

If the developer has to mark some tags (viz. ) for the ASP.NET Engine to ignore, then there's also the potential issue of namespace collisions among tags and future enhancements. By requiring the attribute, this is negated.

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