简体   繁体   English

在aspx.cs页面中获取HTML按钮

[英]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? 我可以从后端C#代码中获取纯HTML编写的按钮或锚(没有runat = server)吗?

I mean, my button is in aspx page and I want to get the button in aspx.cs page. 我的意思是,我的按钮在aspx页面中,我想在aspx.cs页面中获取该按钮。

No you cannot get html controls not decorated with runat="server" in code-behind. 不,您无法在代码隐藏中获取未用runat="server"装饰的html控件。
The FindControl method also works for only those controls that are marked runat="server" . FindControl方法也仅适用于标记为runat="server"控件。

I think you can do it by using jquery ajax and webmethod like below 我认为您可以通过使用如下的jquery ajax和webmethod来做到这一点

write in aspx page below code on button click 单击按钮下面的代码在aspx页面中写

<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 aspx.cs代码

[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. 我一直认为,您可以将ASP.NET标记和HTML标记混合使用,而HTML标记则可以选择是否运行runat =“ server”。 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. 您对Web语言所隐含的内容越多,对于一个崭露头角的程序员来说,学习它的难度就越小。 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. 如果开发人员必须标记一些标记(即)以使ASP.NET Engine忽略,那么标记和将来的增强之间就存在名称空间冲突的潜在问题。 By requiring the attribute, this is negated. 通过要求属性,可以否定该属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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