Reputation: 753
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.
Upvotes: 0
Views: 1181
Reputation: 2258
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"
.
Upvotes: 2
Reputation: 1066
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;
}
Upvotes: 1
Reputation: 150
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.
Upvotes: 0