Compartilhar via


Como: Converter páginas de Web Forms em controles de usuário do ASP.NET

Se você tem desenvolvido um página da Web do ASP.NET e quiser acessar sua funcionalidade em seu aplicativo, você pode fazer algumas alterações secundárias na página para alterá-la para um controle de usuário.

Para converter um arquivo único da página da Web do ASP.NET em um controle de usuário

  1. Renomeie o controle para que a extensão de nome fique .ascx.

  2. Remove the html, body, and form elements from the page.

  3. Altere a diretiva @ Page para uma diretiva @ Control.

  4. Remove all attributes of the @ Control directive except Language, AutoEventWireup (if present), CodeFile, and Inherits.

  5. Include a className attribute in the @ Control directive.Isso permite que o controle de usuário seja altamente digitado quando ele for adicionado a uma página.

Para converter um página da Web do ASP.NET code-behind em um controle de usuário

  1. Renomeie o arquivo .aspx portanto a extensão de nome é .ascx.

  2. Renomeie o arquivo code-behind para ter a extensão de nome .ascx.vb ou .ascx.cs, dependendo da linguagem de programação que é do arquivo code-behind.

  3. Open the code-behind file and change the class from which it inherits from Page to UserControl.

  4. No arquivo .aspx, faça o seguinte:

    1. Remove the html, body, and form elements from the page.

    2. Altere a diretiva @ Page para uma diretiva @ Control.

    3. Remove all attributes of the @ Control directive except Language, AutoEventWireup (if present), CodeFile, and Inherits.

    4. In the @ Control directive, change the CodeFile attribute to point to the renamed code-behind file.

  5. Include a className attribute in the @ Control directive.Isso permite que o controle de usuário seja altamente digitado quando ele for adicionado a uma página.

Exemplo

O exemplo a seguir mostra um página da Web do ASP.NET Single-File em sua forma original e o controle de usuário resultante depois converter a página.

Observação de segurança:

Este exemplo tem uma caixa de texto que aceita entrada do usuário, que é uma ameaça potencial de segurança.Por padrão, páginas Web ASP.NET validam se as entradas de usuário não incluem scripts ou elementos HTML.Para obter mais informações, consulte Visão Geral de Scripts Maliciosos.

<%@ Page Language="VB" %>
<html>
<script runat=server>
Sub EnterBtn_Click(sender as Object, e as EventArgs)
    Label1.Text = "Hi " & Name.Text & " welcome to ASP.NET!"
End Sub
</script>
<body>
<h3> <u>Web Forms Page</u> </h3>
<form>
    Enter Name: <asp:textbox id="Name" runat=server/>
    <asp:button Text="Enter" OnClick="EnterBtn_Click"
        runat=server/>
<br>
<br>
<asp:label id="Label1" runat=server/>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<html>
<script runat=server>
void EnterBtn_Click(Object sender, EventArgs e)
{
    Label1.Text = "Hi " + Name.Text + " welcome to ASP.NET!";
}
</script>
<body>
<h3> <u>Web Forms Page</u> </h3>
<form>
Enter Name: <asp:textbox id="Name" runat=server/>
<asp:button Text="Enter" OnClick="EnterBtn_Click"
    runat=server/>
<br>
<br>
<asp:label id="Label1" runat=server/>
</form>
</body>
</html>
<%@ Control Language="VB" ClassName="SampleUserControl" %>
<h3> <u>User Control</u> </h3>
<script runat=server>
Sub EnterBtn_Click(sender as Object, e as EventArgs)
    Label1.Text = "Hi " & Name.Text & " welcome to ASP.NET!"
End Sub
</script>
Enter Name: <asp:textbox id="Name" runat=server/>
<asp:button Text="Enter" OnClick="EnterBtn_Click"
    runat=server/>
<br>
<br>
<asp:label id="Label1" runat=server/>
<%@ Control Language="C#" ClassName="SampleUserControl" %>
<h3> <u>User Control</u> </h3>
<script runat=server>
void EnterBtn_Click(Object Sender, EventArgs e)
{
    Label1.Text = "Hi " + Name.Text + " welcome to ASP.NET!";
}
</script>
Enter Name: <asp:textbox id="Name" runat=server/>
<asp:button Text="Enter" OnClick="EnterBtn_Click"
runat=server/>
<br>
<br>
<asp:label id="Label1" runat=server/>

Consulte também

Tarefas

Como: Criar controles de usuário do ASP.NET

Conceitos

Visão geral sobre controles de usuário ASP.NET