简体   繁体   English

在asp.net中,使用asp.net Web控件和简单的HTML输入控件有何区别?

[英]In asp.net what is the difference between using asp.net web control and simple html imput control

In my asp.net web control form i am using two text box 1st is simple input html control and 2nd is asp.net input web control. 在我的asp.net Web控件表单中,我使用两个文本框,第一个是简单输入html控件,第二个是asp.net输入Web控件。

<form id="form1" runat="server">

        Email: <input type="text" id="txt_email" name="txt_email" value="" /><br />
        Email2: <asp:TextBox ID="txt_email2" runat="server"></asp:TextBox><br />       

        <asp:Button ID="btn_login" Name="btn_login" runat="server" Text="Button" 
            onclick="btn_login_Click" />

    </form>

I need to know what is the difference using simple control and asp.net input control both of them pass the value to code behind after the form submit. 我需要知道使用简单控件和asp.net输入控件有什么区别,它们都在表单提交后将值传递给代码。 can any one help me on this? 谁可以帮我这个事?

As defined in your example input type="text" won't even be visible to code-behind because it is missing runat="server" attribute. 正如您的示例中所定义的,由于缺少runat="server"属性,因此代码隐藏甚至看不到input type="text"

If you do add it - there're still differences. 如果您添加它-仍然存在差异。 ASP.NET TextBox is more advanced and in par with the rest of ASP.NET model (eg it has property .Text vs. .Value of an HtmlInput control, it has events and other properties). ASP.NET TextBox是更高级的,并且与其余ASP.NET模型相当(例如,它具有HtmlInput控件的.Text.Value属性,它具有事件和其他属性)。

But if you simple need to pass text information back to the server, either of them will do the job. 但是,如果您仅需要将文本信息传递回服务器,则它们中的任何一个都可以完成工作。

The biggest differences are that 最大的不同是

  1. the asp.net controls are rendered on the server, and thus they have more overhead on your server than using traditional controls - traditional controls (by default) are rendered once then basically reside on the client's browser, asp controls are persistent on the server side. asp.net控件在服务器上呈现,因此与使用传统控件相比,它们在服务器上的开销更大-传统控件(默认情况下)呈现一次,然后基本上驻留在客户端的浏览器上,asp控件在服务器端是持久的。
  2. the asp controls can be accessed and worked with directly in the code behind files. 可以直接在文件后面的代码中访问和使用asp控件。
  3. asp controls have some additional tags that can be used on their fields usually. asp控件还有一些其他标记,通常可以在它们的字段上使用。
  4. as was pointed out by @Yuriy-Galanter, how the value is accessed is slightly different. 正如@ Yuriy-Galanter指出的那样,如何访问该值略有不同。

The asp:Textbox renders HTML to the client/browser when the page request is made. 发出页面请求时,asp:Textbox会将HTML呈现到客户端/浏览器。 Picture the ASP.NET control, in this case an asp:TextBox as a server side bit of code that knows to render a <input type="text"> HTML element when the request for the aspx page is made to the server. 将ASP.NET控件(在本例中为asp:TextBox)作为服务器端的代码,当向服务器发出对aspx页的请求时,该代码知道呈现<input type="text"> HTML元素。

The ASP.NET compiler, when parsing your aspx page just spits out the <input type="text"> HTML element you have for Email: and for Email2: the ASP.NET compiler knows that is a server control because of the runat="server" tag. ASP.NET编译器在解析aspx页面时,只会吐出您在Email:和Email2中使用的<input type="text"> HTML元素:由于runat =,ASP.NET编译器知道这是一个服务器控件。 “服务器”标签。 So the ASP.NET compiler, having a reference to the ASP.NET assemblies on the server, reads the code for the <asp:TextBox> and knows to ultimately respond to the page request with an <input type="text" id="txt_email2" /> 因此,具有对服务器上ASP.NET程序集的引用的ASP.NET编译器将读取<asp:TextBox>的代码,并知道最终使用<input type="text" id="txt_email2" />

The server side controls are accessible in your code behind page. 服务器端控件可在页面后面的代码中访问。 So is accessible in the code behind but the <input> element is not. 因此可以在后面的代码中访问,但<input>元素则不可。 Good for you to consider in your research at this point, that if you add runat="server" to your element, it is accessible in your code behind. 此时,您可以在研究中考虑一下,如果将runat =“ server”添加到元素,则可以在后面的代码中访问它。

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

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