Top Banner
Home Library Learn Downloads Support Comm unity Sign in | España - Español | | Principio del formulario MSDN Library .NET Development Articles and Overviews Web Applications (ASP.NET) ASP.NET 2.0 Using Controls GridView Examples for ASP.NET 2.0 Table of Contents: GridView Examples for ASP.NET 2.0 GridView Examples for ASP.NET 2.0: Improvements in Data Access and Display GridView Examples for ASP.NET 2.0: Accessing Data with the DataSource Controls GridView Examples for ASP.NET 2.0: Formatting the GridView GridView Examples for ASP.NET 2.0: Displaying Master/Detail Data in a GridView GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column GridView Examples for ASP.NET 2.0: Working with TemplateFields GridView Examples for ASP.NET 2.0: Drilling Down into Detailed Data GridView Examples for ASP.NET 2.0: Displaying Summary Data in the Footer GridView Examples for ASP.NET 2.0: Deleting a GridView's Underlying Data GridView Examples for ASP.NET 2.0: Editing the Underlying Data in a GridView
13
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Usar Vb Datagrid

Home Library Learn Downloads Support Comm unity

Sign in   | España - Español   |   

| Principio del formulario

MSDN Library

.NET Development

Articles and Overviews

Web Applications (ASP.NET)

ASP.NET 2.0

Using Controls

GridView Examples for ASP.NET 2.0

Table of Contents: GridView Examples for ASP.NET 2.0

GridView Examples for ASP.NET 2.0: Improvements in Data Access and Display

GridView Examples for ASP.NET 2.0: Accessing Data with the DataSource Controls

GridView Examples for ASP.NET 2.0: Formatting the GridView

GridView Examples for ASP.NET 2.0: Displaying Master/Detail Data in a GridView

GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data

GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column

GridView Examples for ASP.NET 2.0: Working with TemplateFields

GridView Examples for ASP.NET 2.0: Drilling Down into Detailed Data

GridView Examples for ASP.NET 2.0: Displaying Summary Data in the Footer

GridView Examples for ASP.NET 2.0: Deleting a GridView's Underlying Data

GridView Examples for ASP.NET 2.0: Editing the Underlying Data in a GridView

Page 2: Usar Vb Datagrid

GridView Examples for ASP.NET 2.0: Deleting a GridView's Underlying Data102 out of 137 rated this helpful - Rate this topic

 

Click here to return to the TOC.

All of the examples we've seen thus far have been read-only GridViews. The examples might have allowed for drilling down into data or sorting or paging data, but regardless the end user was only able to view data. There are times, though, when you will want to let users delete or update the data that powers your Web application. There are various techniques that can be used to edit and delete data, one of them being utilizing the GridView's built-in deletion and editing capabilities.

As with paging or sorting data, deleting data that comes directly from a database by means of a SqlDataSource is easy to accomplish, requiring only a few points and clicks of the mouse. To delete data from a GridView that is populated by means of an ObjectDataSource, however, the underlying data access layer class must provide a method for deleting the data.

In this section we'll see how to delete data from a GridView that utilizes both a SqlDataSource and an ObjectDataSource. We'll also look at how to add client-side confirmation to the GridView's delete capabilities to help protect your users from accidentally deleting a record.

Deleting Data Coming from a SqlDataSource

In the Filtering the Data Shown in a GridView section we saw how to display the order details in a GridView for a particular product selected from a DropDownList. Let's enhance this demo to include the capability to delete an order detail.

In order to enable deletion from the GridView we need to enhance the SqlDataSource to include a DELETE statement in addition to its SELECT statement. The easiest way to add a DELETE statement is through the SqlDataSource's wizard. On step 2, where you pick the table or view and its fields to return, there's an Advanced button that, when clicked, displays the dialog box shown in Figure 35.

Figure 35

By clicking the top checkbox—Generate Insert, Update, and Delete Statements—the SqlDataSource will automatically create the needed INSERT, UPDATE, and DELETE statements, along with the required parameter declarations. Note that the dialog box in Figure 35 also has a second checkbox, Use optimistic concurrency. If this box is checked when the user updates or deletes a record through the GridView a check is made to ensure that the data for the altered row hasn't been changed since they loaded the GridView. If the record has changed the update or delete will fail because a row won't be matched; it is up to you, the page developer, to decide what course of action to take in this case. If Use optimisitic concurrency is left unchecked, the row to be updated or deleted is simply matched by its primary key value(s), ignoring the values of the non-primary key fields.

In essence, check this second checkbox if you want to ensure that user's updates or deletes won't occur if they step on other user's changes; leave it unchecked if you just want to last update/delete to prevail. Checking or leaving this checkbox unchecked simply sets the SqlDataSource's ConflictDetection property to CompareAllValues orOverwriteChanges, respectively. As we'll see in the next section, the ObjectDataSource also has a ConflictDetection property that is used to specify whether or not optimistic concurrency should be used.

Note   When configuring the DataSource to support deleting through a GridView, be sure that the SELECT query returns the field(s) that makeup the underlying data's primary key, even if you don't plan on displaying these field(s) in the GridView. As you can see below, the SelectCommand for the SqlDataSource that retrieves records from the Order Details table is bringing back OrderID and ProductID, the composite primary key fields. Furthermore, the GridView'sDataKeyNames property must be set to the primary key field(s). (If there is more than one primary key field, separate each by a comma.)

As the first checkbox's title implies, checking it will generate not only a DELETE statement, but INSERT and UPDATE statements, as well. After checking this checkbox theSqlDataSource's declarative syntax has expanded to:

<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:NWConnectionString %>" DeleteCommand="DELETE FROM [Order Details] WHERE [OrderID] = @original_OrderID AND [ProductID] = @original_ProductID" ID="SqlDataSource1" InsertCommand="INSERT INTO [Order Details] ([OrderID], [ProductID], [UnitPrice], [Quantity]) VALUES (@OrderID, @ProductID, @UnitPrice, @Quantity)" runat="server" SelectCommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details] WHERE ([ProductID] = @ProductID)" UpdateCommand="UPDATE [Order Details] SET [UnitPrice] = @UnitPrice, [Quantity] = @Quantity WHERE [OrderID] = @original_OrderID AND [ProductID] = @original_ProductID"> <DeleteParameters> <asp:Parameter Name="original_OrderID" Type="Int32" /> <asp:Parameter Name="original_ProductID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="UnitPrice" Type="Decimal" /> <asp:Parameter Name="Quantity" Type="Int16" /> <asp:Parameter Name="original_OrderID" Type="Int32" /> <asp:Parameter Name="original_ProductID" Type="Int32" /> </UpdateParameters> <SelectParameters> <asp:ControlParameter ControlID="productSelector" Name="ProductID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="OrderID" Type="Int32" /> <asp:Parameter Name="ProductID" Type="Int32" /> <asp:Parameter Name="UnitPrice" Type="Decimal" /> <asp:Parameter Name="Quantity" Type="Int16" /> </InsertParameters></asp:SqlDataSource>

Note the additional <DeleteParameters>, <UpdateParameters>, <InsertParameters>, and DeleteCommand, InsertCommand, and UpdateCommand properties. Since we are only interested in deleting in

this example, you can safely remove the InsertCommand, UpdateCommand, <UpdateParameters>, and <InsertParameters>.

Once you have configured the SqlDataSource to have a DeleteCommand, the associated GridView's Smart Tag will include a checkbox titled Enable Deleting. If you check this, a CommandField with a Delete button is added to the GridView (see Figure 36).

Page 3: Usar Vb Datagrid

Figure 36 (Click on the graphic for a larger image)

And that's all there is to it! No code is required. One downside is that there's no confirmation upon deleting from the GridView. That is, as soon as the Delete button is clicked for a particular GridView row the page is posted back and that row is lost. Ideally end users would be presented with a prompt asking them to confirm the delete. We'll see how to accomplish this in an upcoming section,  Utilizing Client-Side Script to Confirm Deletions.

The following shows the ASP.NET page's declarative syntax. When studying this markup be sure to take note of the following:

The orderDetailsGridView GridView's DataKeyNames field is set to the primary key fields for the Order Details table—OrderID and ProductID. When the Delete button in the GridView is clicked, the page is posted back and the SqlDataSource's DeleteCommand is executed, using the clicked GridView row's DataKeyNames values for theOrderID and ProductID parameter values in the DELETE statement.

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <h2>You Can Delete Order Detail Information for Orders that Have Included Shipments of the Selected Product </h2> <asp:SqlDataSource ID="productListingDataSource" ConnectionString="<%$ ConnectionStrings:NWConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]" Runat="server"> </asp:SqlDataSource> <asp:DropDownList ID="productSelector" Runat="server" DataSourceID="productListingDataSource" DataTextField="ProductName" DataValueField="ProductID" AutoPostBack="True"> </asp:DropDownList>&nbsp; <asp:SqlDataSource ID="orderDetailsForProduct" DataSourceMode="DataReader" ConnectionString="<%$ ConnectionStrings:NWConnectionString %>" SelectCommand="SELECT [OrderID], [ProductID], [UnitPrice], [Quantity] FROM [Order Details] WHERE ([ProductID] = @ProductID2)" Runat="server" DeleteCommand="DELETE FROM [Order Details] WHERE [OrderID] = @original_OrderID AND [ProductID] = @original_ProductID"> <DeleteParameters> <asp:Parameter Type="Int32" Name="OrderID"></asp:Parameter> <asp:Parameter Type="Int32" Name="ProductID"></asp:Parameter> </DeleteParameters> <SelectParameters> <asp:ControlParameter Name="ProductID2" Type="Int32" ControlID="productSelector" PropertyName="SelectedValue"></asp:ControlParameter> </SelectParameters> </asp:SqlDataSource> <asp:GridView ID="orderDetailsGridView" Runat="server" AutoGenerateColumns="False" DataSourceID="orderDetailsForProduct" DataKeyNames="OrderID,ProductID" BorderColor="Tan" CellPadding="2" BackColor="LightGoldenrodYellow" BorderWidth="1px" ForeColor="Black" GridLines="None" OnRowDeleting="orderDetailsGridView_RowDeleting"> <FooterStyle BackColor="Tan"></FooterStyle> <PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle> <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle> <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle> <Columns> <asp:CommandField DeleteText="Delete Order Line Item" ShowDeleteButton="True"></asp:CommandField> <asp:BoundField HeaderText="Order ID" DataField="OrderID" SortExpression="OrderID"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Quantity" DataField="Quantity" SortExpression="Quantity" DataFormatString="{0:d}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> </Columns> <SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle> </asp:GridView>&nbsp;</div> </form></body></html>

To aid with deleting, the GridView provides a RowDeleting event that fires before the underlying data is actually deleted. If you want to only allow the data to be deleted if some conditions are met, you can create an event handler for this event. The event handler is passed as its second parameter an object of type GridViewDeleteEventArgs, which has a couple of helpful properties:

Page 4: Usar Vb Datagrid

Values, which provides data on the values of the row being deleted, and

Cancel, which, if set to True, short-circuits the deletion.

There's also a RowDeleted event that is fired after the underlying record has been successfully deleted.

Deleting Data Coming from an ObjectDataSource

Deleting a record in a GridView that is bound to an ObjectDataSource is fundamentally the same as deleting a record from a GridView bound to a SqlDataSource. The only difference is in the back-end: the data access layer class that the ObjectDataSource is accessing must provide a method for deleting a record.

When manipulating the data exposed by an ObjectDataSource, there are two methods to work with this data. The ObjectDataSource has a ConflictDetection property that can be one of two values:

OverwriteChanges (the default), or

CompareAllValues

The OverwriteChanges option is intended to be used if you don't care about the possibility of some other user stepping on the data being deleted or updated by the current user. In this scenario, whatever user commits their deletion or update last is the winner. There are times, though, where you may want to short circuit a user's changes if you detect someone else has made a modification to the underlying data. In such scenarios, use the CompareAllValues option.

The reason I mention the ObjectDataSource's ConflictDetection property is because the signature for the Delete method depends on its setting. Clearly, the Delete method must accept as many parameters as are needed to uniquely identify the record being deleted. For example, if we wanted to create a method to delete a product from the Products table then the method, at a minimum, would need to accept a ProductID integer input, since each product is uniquely identified by a ProductID. When using theOverwriteChanges mode, the Delete method only needs to accept this minimal set of input(s). If, however, you use the CompareAllValues option, the Delete method signature must accept not only the primary key field(s), but also the other fields defined in the GridView.

This may sound a bit confusing at this point, and understandably so. Things will become clearer, though, with a concrete example. The Order Details table has a composite primary key, one that is made up of two fields: ProductID and OrderID. The GridView shows three fields: OrderID, Quantity, and UnitPrice. Now, if we wanted to perform a delete with the ObjectDataSource's ConflictDetection property set to OverwriteChanges, the Delete method would need to look like:

' Visual Basic .NETPublic Shared Sub DeleteMethod(ByVal original_OrderID As Integer, _ ByVal original_ProductID As Integer) ...End Sub// C#public static void DeleteMethod(int original_OrderID, int original_ProductID){ ...}

If, however, the ConflictDetection property was set to OverwriteChanges, the Delete method would need to look like:

' Visual Basic .NETPublic Shared Sub DeleteMethod(ByVal original_OrderID As Integer, _ ByVal original_ProductID As Integer, _ ByVal original_Quantity as Integer, _ ByVal original_UnitPrice as Decimal) ...End Sub// C#public static void DeleteMethod(int original_OrderID, int original_ProductID, int original_Quantity, decimal original_UnitPrice){ ...}

For this demo I used the OverwriteChanges approach. The following, then, shows the Delete method in the OrderDetailDAL class:

The DeleteOrderDetail Method (Visual Basic)

Public Class OrderDetailDAL ... Public Shared Sub DeleteOrderDetail(ByVal original_OrderID _ As Integer, ByVal original_ProductID As Integer) ' deletes a specified Order Details record ' from the Northwind Products table Dim sql As String = _ "DELETE FROM [Order Details] WHERE OrderID = " & _ "@OrderID AND ProductID = @ProductID" Using myConnection As New _SqlConnection(ConfigurationManager.ConnectionStrings("NWConnectionString").ConnectionString) Dim myCommand As New SqlCommand(sql, myConnection) myCommand.Parameters.Add(New SqlParameter("@OrderID", _ original_OrderID)) myCommand.Parameters.Add(New SqlParameter("@ProductID", _ original_ProductID)) myConnection.Open() myCommand.ExecuteNonQuery() myConnection.Close() End Using End SubEnd ClassThe DeleteOrderDetail Method (C#)public class OrderDetailDAL{ ... public static void DeleteOrderDetail(int original_OrderID, int original_ProductID) { // deletes a specified Order Details record // from the Northwind Products table string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "@OrderID AND ProductID = @ProductID"; using (SqlConnection myConnection = new SqlConnection( ConfigurationManager.ConnectionStrings["NWConnectionString"].ConnectionString)) { SqlCommand myCommand = new SqlCommand(sql, myConnection); myCommand.Parameters.Add(new SqlParameter("@OrderID", original_OrderID)); myCommand.Parameters.Add(new SqlParameter("@ProductID", original_ProductID)); myConnection.Open(); myCommand.ExecuteNonQuery(); myConnection.Close();

Page 5: Usar Vb Datagrid

} }}

Configuring the ObjectDataSource for deletion is a trifle different than with the SqlDataSource. After selecting the class to use, you can specify a method to delete data by browsing to the Delete tab and selecting the method name in the drop-down list, as shown in Figure 37.

Figure 37

Once you have configured the ObjectDataSource for deletion, the steps of configuring the GridView for deletion are the same as with the SqlDataSource, as is the end result. The following shows the declarative syntax of the ASP.NET page:

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <div> <h2>You Can Delete Order Detail Information for Orders that Have Included Shipments of the Selected Product </h2> <asp:ObjectDataSource ID="productListingDataSource" Runat="server" TypeName="ProductDAL" SelectMethod="GetProducts"> </asp:ObjectDataSource> <asp:DropDownList ID="productSelector" Runat="server" DataSourceID="productListingDataSource" DataTextField="ProductName" DataValueField="ProductID" AutoPostBack="True"> </asp:DropDownList>&nbsp;&nbsp; <asp:ObjectDataSource ID="orderDetailsForProduct" Runat="server" SelectMethod="GetOrderDetailsByProductID" TypeName="OrderDetailDAL" DeleteMethod="DeleteOrderDetail"> <DeleteParameters> <asp:Parameter Type="Int32" Name="original_OrderID"></asp:Parameter> <asp:Parameter Type="Int32" Name="original_ProductID"></asp:Parameter> </DeleteParameters> <SelectParameters> <asp:ControlParameter Name="productID" Type="Int32" ControlID="productSelector" PropertyName="SelectedValue"></asp:ControlParameter> </SelectParameters> </asp:ObjectDataSource> <asp:GridView ID="orderDetailsGridView" Runat="server" AutoGenerateColumns="False" DataSourceID="orderDetailsForProduct" DataKeyNames="OrderID,ProductID" BorderColor="Tan" CellPadding="2" BackColor="LightGoldenrodYellow" BorderWidth="1px" ForeColor="Black" GridLines="None"> <FooterStyle BackColor="Tan"></FooterStyle> <PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle> <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle> <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle> <Columns> <asp:CommandField DeleteText="Delete Order Line Item" ShowDeleteButton="True"></asp:CommandField> <asp:BoundField HeaderText="Order ID" DataField="OrderID" SortExpression="OrderID"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Quantity" DataField="Quantity" SortExpression="Quantity" DataFormatString="{0:d}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> </Columns> <SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle> </asp:GridView>&nbsp;</div> </div> </form></body></html>

Utilizing Client-Side Script to Confirm Deletions

The two deletion examples we just examined do not provide confirmation upon delete. That is, as soon as an end user clicked the Delete button, a postback occurred and the record was deleted. To help prevent accidental deletions, it's good practice to provide the end user with a confirmation of some sort, verifying that they do, indeed, want to delete the record before doing so. One common confirmation technique is to use a client-side confirm messagebox. The confirm messagebox is one that is displayed with an OK and Cancel button that causes the form submission to be short circuited if the user clicks Cancel.

The ASP.NET 2.0 Button, LinkButton, and ImageButton Web controls all contain an OnClientClick property in which you can specify client-side JavaScript that should be executed when the Button is clicked. If you use JavaScript like the following:

return confirm(msg);

The user will be shown a confirm messagebox upon clicking the button (where msg is the string you want to appear in the confirm messagebox). If the user clicks the confirm messagebox's Cancel button the form will not be posted back; clicking the OK button will submit the form.

Unfortunately, the GridView's CommandField does not include an OnClientClick property. However, any Button in a GridView with a CommandName of Delete, when clicked, will cause the GridView to delete the associated record. Therefore we can create our own Delete button by adding a TemplateField that contains a Button (or LinkButton orImageButton) with a CommandName of Delete. This added Button, then, can have an appropriate OnClientClick property value. The following shows how to modify theGridView to provide a confirmation messagebox when the Delete button is clicked:

Page 6: Usar Vb Datagrid

<asp:GridView ID="orderDetailsGridView" Runat="server" DataSourceID="orderDetailsForProduct" AutoGenerateColumns="False" DataKeyNames="OrderID,ProductID" BorderColor="Tan" CellPadding="2" BackColor="LightGoldenrodYellow" BorderWidth="1px" ForeColor="Black" GridLines="None"> <FooterStyle BackColor="Tan"></FooterStyle> <PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle> <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle> <AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle> <Columns> <asp:TemplateField><ItemTemplate> <asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this record?');" CommandName="Delete">Delete Order Line Item</asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="Order ID" DataField="OrderID" SortExpression="OrderID"> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Quantity" DataField="Quantity" SortExpression="Quantity" DataFormatString="{0:d}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> <asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" SortExpression="UnitPrice" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right"></ItemStyle> </asp:BoundField> </Columns> <SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle></asp:GridView>

Note that the TemplateField has a LinkButton with its CommandName property set to Delete and its OnClientClick property set to return confirm('Are you sure you want to delete this record?');. As Figure 38 shows, now when a Delete button is clicked a confirm messagebox is displayed and the record is only deleted if the messagebox's OK button is clicked.

Página de inicio Biblioteca Aprender Descargas Sop orte técnico Comunidad

Iniciar sesión   | España - Español   |   

MSDN Library

Desarrollo Web

Page 7: Usar Vb Datagrid

ASP.NET

ASP.NET 4

Páginas web ASP.NET

Redirigir a los usuarios a otra página

Envío entre páginas en las páginas Web ASP.NET

Cómo: Redirigir los usuarios a otra página

Cómo: Enviar páginas web ASP.NET a una página diferente

Cómo: Determinar el modo en que se invocaron las páginas web ASP.NET

Cómo: Pasar valores entre páginas Web ASP.NET

Contenido de la comunidad

Agregue ejemplos de código y sugerencias para mejorar este tema.

Más...

Este artículo se tradujo de forma manual. Mueva el puntero sobre las frases del artículo para ver el texto original.

Traducción

Original

Cómo: Pasar valores entre páginas Web ASP.NET.NET Framework 4

Otras versiones

Page 8: Usar Vb Datagrid

Personas que lo han encontrado útil: 2 de 5 - Valorar este tema

Si en la aplicación se redirige (navega) de una página Web ASP.NET a otra, a menudo deseará pasar información de la página de origen a la de destino.  Por ejemplo, imagine que tiene una página cuyos usuarios pueden seleccionar los elementos que desean adquirir. Cuando los usuarios envían la página, desea llamar a otra página que procese la información introducida por el usuario.

Hay un proyecto de aplicación web de Visual Studio con código fuente disponible para este tema: Descargar.

Nota

Para obtener información sobre como navegar de una página a otra en un sitio web ASP.NET, vea Cómo: Redirigir los usuarios a otra página.

La información se puede pasar entre páginas de varias formas, algunas de las cuales dependen de cómo se lleve a cabo la redirección. Las siguientes opciones están disponibles incluso si la página de origen está en una aplicación web ASP.NET diferente que la página de destino o si la página de origen no es una página web ASP.NET:

Usar una cadena de consulta.

Obtener información de HTTP POST de la página de origen.

Las siguientes opciones solo están disponibles si las páginas de origen y de destino están en la misma aplicación web ASP.NET.

Usar estado de sesión.

Crear propiedades públicas en la página de origen y obtener acceso a los valores de propiedad en la página de destino.

Obtener información de los controles en la página de destino desde los controles en la página de origen.

Usar una cadena de consulta

Cuando se usa un hipervínculo o Response.Redirect para navegar de una página a otra, se puede agregar información en una cadena de consulta al final de la dirección URL.

 Nota

Cuando utilice cadenas de consulta no pase nunca datos confidenciales, ya que los usuarios podrán verlos y modificarlos fácilmente, lo que representa un riesgo de seguridad potencial.

Para obtener más información, vea QueryString.

Para utilizar una cadena de consulta para pasar información

1. En la página de origen, cuando especifique la dirección URL de la página de destino, incluya la información que desee pasar en forma de pares clave-valor al final de la dirección URL.  Un signo de interrogación (?) precede al primer par y los pares subsiguientes an precedidos de un carácter Y comercial (&), como se muestra en el siguiente ejemplo:

2. http://contoso.com/products.aspx?field1=value13. http://contoso.com/products.aspx?field1=value1&field2=value24. En la página de destino, obtenga acceso a los valores de la cadena de consulta mediante la propiedad QueryString del objeto HttpRequest, tal y como se muestra en el siguiente ejemplo:

VB

Dim s As Strings = Request.QueryString("field1")

C#

String s = Request.QueryString["field1"];

Obtener información de HTTP POST de la página de origen

Cuando la página de origen utiliza la acción HTTP POST para navegar hasta la página de destino, se pueden recuperar los valores expuestos de la colección de objetos  Formen la página de destino. Tenga en cuenta que sólo puede obtener los valores expuestos; no puede leer los valores de los controles arbitrarios de la página.

Page 9: Usar Vb Datagrid

Para obtener los valores de controles de la página de origen en otra aplicación

1. En la página de origen, incluya un elemento form que contenga elementos HTML (por ejemplo, input o textarea) o controles de servidor ASP.NET (por ejemplo, controles DropDownList o TextBox) que exponen los valores cuando se envía el formulario.

2. En la página de destino, lea la colección de objetos Form, que devuelve un diccionario de pares nombre-valor, con un par para cada valor expuesto.

En el ejemplo de código siguiente se muestra el identificador y el valor de cada uno de los controles expuestos en la página de origen, así como los valores expuestos en una etiqueta denominada  Label1.

 Nota

Entre la información expuesta en las páginas Web ASP.NET se encuentran los valores de los campos ocultos, como __VIEWSTATE, __EVENTTARGET y__EVENTARGUMENT, que se utilizan para el procesamiento interno de la página. En el ejemplo de código siguiente se excluyen los valores de los campos expuestos cuyo nombre comienza por doble subrayado (__).

VB

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Me.Load Dim displayValues As New StringBuilder() Dim postedValues As NameValueCollection = Request.Form Dim nextKey As String For i As Integer = 0 To postedValues.AllKeys.Length - 1 nextKey = postedValues.AllKeys(i) If nextKey.Substring(0, 2) <> "__" Then displayValues.Append("<br>") displayValues.Append(nextKey) displayValues.Append(" = ") displayValues.Append(postedValues(i)) End If Next Label1.Text = displayValues.ToString()End Sub

C#

void Page_Load(object sender, EventArgs e){ System.Text.StringBuilder displayValues = new System.Text.StringBuilder(); System.Collections.Specialized.NameValueCollection postedValues = Request.Form; String nextKey; for(int i = 0; i < postedValues.AllKeys.Length; i++) { nextKey = postedValues.AllKeys[i]; if(nextKey.Substring(0, 2) != "__") { displayValues.Append("<br>"); displayValues.Append(nextKey); displayValues.Append(" = "); displayValues.Append(postedValues[i]); } } Label1.Text = displayValues.ToString();}

Usar estado de sesión

La información del estado de sesión está disponible para todas las páginas web ASP.NET de la aplicación actual. Sin embargo, el estado de sesión consume memoria del servidor y la información permanece almacenada hasta que expira la sesión, lo que puede suponer una sobrecarga mayor que la deseada simplemente para pasar información a la página siguiente.  Para obtener más información, vea Cómo: Guardar valores en un estado de sesión y Cómo: Leer los valores de un estado de sesión.

Page 10: Usar Vb Datagrid

Para usar el estado de sesión para pasar información

1. En la página de origen, guarde la información que desee pasar en el estado de sesión, tal y como se muestra en el siguiente ejemplo:

VB

Session("field1") = "value1"

C#

Session["field1"] = "value1";

2. En la página de destino, lea la información guardada del estado de sesión, tal y como se muestra en el siguiente ejemplo:

C#

VB

string field1 = (string)(Session["field1"]);

Obtener los valores de las propiedades públicas de la página de origen

Si va a diseñar la página de origen expresamente para compartir información con las páginas de destino y ambas son páginas web ASP.NET de la misma aplicación web, puede agregar a la página de origen propiedades públicas que exponen la información que desea que compartan las páginas. A continuación, podrá leer los valores de las propiedades en las páginas de destino.

Esta estrategia funciona en dos casos:

Cuando la página de origen envía mensajes cruzados a la página de destino. Para obtener más información, vea Cómo: Enviar páginas web ASP.NET a una página diferente.

Cuando se llama al método Transfer para transferir la ejecución de la página de origen a la página de destino en el servidor.

Para obtener los valores de las propiedades públicas de la página de origen

1. En la página de origen, cree una o varias propiedades públicas y guarde la página.

En el ejemplo de código siguiente se muestra una propiedad denominada CurrentCity que expone el valor de un control TextBox denominado textCity.

VB

Public ReadOnly Property CurrentCity() As String Get Return textCity.Text End GetEnd Property

C#

public String CurrentCity{ get { return textCity.Text; }}

Nota

Las propiedades de la página de origen, que se crean principalmente para exponer valores del envío de información entre páginas, suelen ser propiedades de sólo lectura. Aunque la página de origen puede contener propiedades públicas de lectura y escritura, generalmente no tiene sentido establecer una propiedad de la página de origen desde la propiedad de la página de destino ya que no se conservará el valor.

2. En la página de destino, agregue una directiva de página @   PreviousPageType  que apunte a la página de origen.

En el ejemplo de código siguiente se muestra una directiva PreviousPageType que hace referencia a una página de origen denominada SourcePage.aspx.

Page 11: Usar Vb Datagrid

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>

La directiva PreviousPageType hace que la propiedad PreviousPage de la página se asigne como tipo a la clase de la página de origen.

3. En el código de la página de destino, utilice los miembros con establecimiento inflexible de tipos de la propiedad PreviousPage para leer las propiedades del código fuente.

En el ejemplo de código siguiente se lee el valor de la propiedad CurrentCity definido en la página de origen.

VB

Label1.Text = PreviousPage.CurrentCity

C#

Label1.Text = PreviousPage.CurrentCity;

Obtener información de los controles de la página de origen en la misma aplicación

Si las páginas de origen y destino son páginas Web ASP.NET y pertenecen a la misma aplicación Web, puede leer los valores de los controles en la página de origen mientras está en la página de destino.  Esta estrategia se puede utilizar si la página de origen no expone propiedades públicas que contengan la información necesaria.

Para obtener los valores de los controles de la página de origen en la misma aplicación

En la página de destino, obtenga una referencia a la página de origen mediante la propiedad PreviousPage de la página de destino y, a continuación, llame al métodoFindControl para obtener una referencia al control deseado.

En el ejemplo de código siguiente se obtiene el valor del control TextBox1 de la página de origen y se muestra en el control denominado Label1:

VB

If Not Page.PreviousPage Is Nothing Then Dim SourceTextBox As TextBox SourceTextBox = CType(PreviousPage.FindControl("TextBox1"), _ TextBox) If Not SourceTextBox Is Nothing Then Label1.Text = SourceTextBox.Text End IfEnd If

C#

if (PreviousPage != null){ TextBox SourceTextBox = (TextBox) PreviousPage.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; }}

El método FindControl busca controles en el contenedor de nomenclatura actual. Si el control que está buscando está dentro de otro control, primero debe obtener una referencia al contenedor y, a continuación, buscar en dicho contenedor el control que desea obtener. Un ejemplo típico de esta situación es el caso en que la página anterior es una página maestra y el control que se busca está dentro de un control ContentPlaceHolder. El siguiente ejemplo es similar al anterior, salvo en que se supone que TextBox1 se encuentra en un control ContentPlaceHolder denominado ContentPlaceHolder1:

VB

If Not Page.PreviousPage Is Nothing Then Dim placeHolder As Control = PreviousPage.Controls(0).FindControl("ContentPlaceHolder1") Dim SourceTextBox As TextBox = CType(placeHolder.FindControl("TextBox1"), TextBox) If Not SourceTextBox Is Nothing Then Label1.Text = SourceTextBox.Text End IfEnd If

C#

Page 12: Usar Vb Datagrid

if (PreviousPage != null){ Control placeHolder = PreviousPage.Controls[0].FindControl("ContentPlaceHolder1") TextBox SourceTextBox = (TextBox)placeHolder.FindControl("TextBox1"); if (SourceTextBox != null) { Label1.Text = SourceTextBox.Text; }}

Para obtener información sobre cómo obtener una referencia a un control cuando no se dispone de ninguna referencia al contenedor de nomenclatura, vea Cómo: Tener acceso a los controles de servidor por identificador.

Vea también

Tareas

Cómo: Determinar el modo en que se invocaron las páginas web ASP.NET

Conceptos

Redirigir a los usuarios a otra página

Envío entre páginas en las páginas Web ASP.NET

Información general sobre la administración de estados de ASP.NET

¿Le ha resultado útil?  Sí  No

Contenido de la comunidad Agregar

 Preguntas más frecuentes

© 2012 Microsoft. Reservados todos los derechos.

Términos de uso | Marcas Registradas | Privacidad | Administre su perfil | MSDN Flash en Español | Contacto | Aviso legal |Comentario del sitio  

The following code example excludes the values of posted fields that are named with a leading double underscore (__).