Академический Документы
Профессиональный Документы
Культура Документы
<html>
<script runat="server">
' The Cart and CartView objects temporarily store the data source
' for the DataGrid control while the page is being processed.
Dim Cart As DataTable = New DataTable()
Dim CartView As DataView
' With a database, use an select query to retrieve the data. Because
' the data source in this example is an in-memory DataTable, retrieve
' the data from session state if it exists; otherwise create the data
' source.
GetSource()
' The DataGrid control maintains state between posts to the server;
' it only needs to be bound to a data source the first time the page
' is loaded or when the data source is updated.
If Not IsPostBack Then
BindGrid()
End If
End Sub
' Set the EditItemIndex property to the index of the item clicked
' in the DataGrid control to enable editing for that item. Be sure
' to rebind the DateGrid to the data source to refresh the control.
ItemsGrid.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
End Sub
' Retrieve the text boxes that contain the values to update.
' For bound columns, the edited value is stored in a TextBox.
' The TextBox is the 0th control in a cell's Controls collection.
' Each cell in the Cells collection of a DataGrid item represents
' a column in the DataGrid control.
Dim qtyText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
Dim priceText As TextBox = CType(e.Item.Cells(4).Controls(0), TextBox)
Dim dr As DataRow
' With a database, use an update command to update the data.
' Because the data source in this example is an in-memory
' DataTable, delete the old row and replace it with a new one.
' Remove the old entry and clear the row filter.
CartView.RowFilter = "Item='" & item & "'"
If CartView.Count > 0 Then
CartView.Delete(0)
End If
CartView.RowFilter = ""
' ***************************************************************
' Insert data validation code here. Be sure to validate the
' values entered by the user before converting to the appropriate
' data types and updating the data source.
' ***************************************************************
' If necessary, remove the '$' character from the price before
' converting it to a Double.
If price.Chars(0) = "$" Then
dr(2) = Convert.ToDouble(price.Substring(1))
Else
dr(2) = Convert.ToDouble(price)
End If
Cart.Rows.Add(dr)
End Sub
Sub BindGrid()
' Set the data source and bind to the Data Grid control.
ItemsGrid.DataSource = CartView
ItemsGrid.DataBind()
End Sub
Sub GetSource()
' For this example, the data source is a DataTable that is stored
' in session state. If the data source does not exist, create it;
' otherwise, load the data.
If Session("ShoppingCart") Is Nothing Then
For i = 1 To 9
dr = Cart.NewRow()
If (i Mod 2) <> 0 Then
dr(0) = 2
Else
dr(0) = 1
End If
Next i
Else
End If
Return
End Sub
Case "Delete"
DeleteItem(e)
Case Else
' Do nothing.
End Select
End Sub
' e.Item is the table row where the command is raised. For bound
' columns, the value is stored in the Text property of a TableCell.
Dim itemCell As TableCell = e.Item.Cells(2)
Dim item As String = itemCell.Text
CartView.Delete(0)
End If
CartView.RowFilter = ""
End Sub
</script>
<body>
<form runat="server">
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel"
OnUpdateCommand="ItemsGrid_Update"
OnItemCommand="ItemsGrid_Command"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit item">
<ItemStyle Wrap="False">
</ItemStyle>
<HeaderStyle Wrap="False">
</HeaderStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn
HeaderText="Delete item"
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>
<asp:BoundColumn HeaderText="Item"
ReadOnly="True"
DataField="Item"/>
<asp:BoundColumn HeaderText="Quantity"
DataField="Qty"/>
<asp:BoundColumn HeaderText="Price"
DataField="Price"
DataFormatString="{0:c}"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
JScript
public class EditCommandColumn extends DataGridColumn
C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
// The Cart and CartView objects temporarily store the data source
// for the DataGrid control while the page is being processed.
DataTable Cart = new DataTable();
DataView CartView;
void Page_Load(Object sender, EventArgs e)
{
BindGrid();
// ***************************************************************
// Insert data validation code here. Be sure to validate the
// values entered by the user before converting to the appropriate
// data types and updating the data source.
// ***************************************************************
Cart.Rows.Add(dr);
void BindGrid()
{
// Set the data source and bind to the Data Grid control.
ItemsGrid.DataSource = CartView;
ItemsGrid.DataBind();
void GetSource()
{
// For this example, the data source is a DataTable that is stored
// in session state. If the data source does not exist, create it;
// otherwise, load the data.
if (Session["ShoppingCart"] == null)
{
else
{
return;
switch(((LinkButton)e.CommandSource).CommandName)
{
case "Delete":
DeleteItem(e);
break;
default:
// Do nothing.
break;
void DeleteItem(DataGridCommandEventArgs e)
{
// e.Item is the table row where the command is raised. For bound
// columns, the value is stored in the Text property of a TableCell.
TableCell itemCell = e.Item.Cells[2];
string item = itemCell.Text;
</script>
<body>
<form runat="server">
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
OnEditCommand="ItemsGrid_Edit"
OnCancelCommand="ItemsGrid_Cancel"
OnUpdateCommand="ItemsGrid_Update"
OnItemCommand="ItemsGrid_Command"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit item">
<ItemStyle Wrap="False">
</ItemStyle>
<HeaderStyle Wrap="False">
</HeaderStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn
HeaderText="Delete item"
ButtonType="LinkButton"
Text="Delete"
CommandName="Delete"/>
<asp:BoundColumn HeaderText="Item"
ReadOnly="True"
DataField="Item"/>
<asp:BoundColumn HeaderText="Quantity"
DataField="Qty"/>
<asp:BoundColumn HeaderText="Price"
DataField="Price"
DataFormatString="{0:c}"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<html>
<head>
<script runat="server">
Function CreateDataSource() As ICollection
For i = 0 to 2
dr = dt.NewRow()
dr(0) = i
dr(1) = CDbl(i) * 1.23
dt.Rows.Add(dr)
Next i
End Function
MyDataGrid.DataSource = CreateDataSource()
MyDataGrid.DataBind()
End Sub
</script>
</head>
<body>
<form runat="server">
<h3>HyperLinkColumn Example<h3>
<asp:DataGrid id="MyDataGrid"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#aaaadd"/>
<Columns>
<asp:HyperLinkColumn
HeaderText="Select an Item"
DataNavigateUrlField="IntegerValue"
DataNavigateUrlFormatString="detailspage.aspx?id={0}"
DataTextField="PriceValue"
DataTextFormatString="{0:c}"
Target="_blank"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<script runat="server">
End Sub
</script>
</head>
<body>
<asp:Label id="Label1"
runat="server"/>
</body>
</html>
C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<script runat="server">
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dr[0] = i;
dr[1] = (Double)i * 1.23;
dt.Rows.Add(dr);
}
</script>
</head>
<body>
<form runat="server">
<h3>HyperLinkColumn Example<h3>
<asp:DataGrid id="MyDataGrid"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
AutoGenerateColumns="false"
runat="server">
<HeaderStyle BackColor="#aaaadd"/>
<Columns>
<asp:HyperLinkColumn
HeaderText="Select an Item"
DataNavigateUrlField="IntegerValue"
DataNavigateUrlFormatString="detailspage.aspx?id={0}"
DataTextField="PriceValue"
DataTextFormatString="{0:c}"
Target="_blank"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
<script runat="server">
</script>
</head>
<body>
<asp:Label id="Label1"
runat="server"/>
</body>
</html>
<html>
<script runat="server">
If IsNothing(Session("StoreData")) Then
Dim dr As DataRow
Dim i As Integer
Session("StoreData") = Store
dr(0) = "0.0%"
dr(1) = "Item " & i.ToString()
dr(2) = (1.23 * (i + 1)).ToString()
Store.Rows.Add(dr)
Next i
Else
Store = Session("StoreData")
End If
End Sub
MyDataGrid.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
' Get the text box that contains the price to edit.
' For bound columns the edited value is stored in a text box.
' The text box is the first control in the Controls collection.
Dim priceText As TextBox = e.Item.Cells(3).Controls(0)
' Get the check box that indicates whether to include tax from the
' TemplateColumn. Notice that in this case, the check box control is
' second control in the Controls collection.
Dim taxCheck As CheckBox = e.Item.Cells(2).Controls(1)
Dim dr As DataRow
' With a database, use an update command. Since the data source is
' an in-memory DataTable, delete the old row and replace it with a new
one.
If taxCheck.Checked Then
dr(0) = "8.6%"
Else
dr(0) = "0.0%"
End If
dr(1) = item
dr(2) = price
Store.Rows.Add(dr)
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Sub BindGrid()
MyDataGrid.DataSource = StoreView
MyDataGrid.DataBind()
End Sub
</script>
<body>
<form runat="server">
<h3>TemplateColumn Example</h3>
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
HeaderText="Edit Controls"/>
<asp:BoundColumn HeaderText="Description"
ReadOnly="true"
DataField="Item"/>
<asp:TemplateColumn>
<HeaderTemplate>
<b> Tax </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox
Text="Taxable"
runat="server"/>
</EditItemTemplate>
<FooterTemplate>
<asp:HyperLink id="HyperLink1"
Text="Microsoft"
NavigateUrl="http://www.microsoft.com"
runat="server"/>
</FooterTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Price"
DataField="Price"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
C++
public ref class TemplateColumn : public DataGridColumn
<html>
<script runat="server">
Session["StoreData"] = Store;
dr[0] = "0.0%";
dr[1] = "Item " + i.ToString();
dr[2] = (1.23 * (i + 1)).ToString();
Store.Rows.Add(dr);
}
}
else
Store = (DataTable)Session["StoreData"];
if(!IsPostBack)
BindGrid();
DataRow dr;
if (taxCheck.Checked)
dr[0] = "8.6%";
else
dr[0] = "0.0%";
dr[1] = item;
dr[2] = price;
Store.Rows.Add(dr);
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
void BindGrid()
{
MyDataGrid.DataSource = StoreView;
MyDataGrid.DataBind();
}
</script>
<body>
<form runat="server">
<h3>TemplateColumn Example</h3>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
ItemStyle-Wrap="false"
HeaderText="Edit Controls"/>
<asp:BoundColumn HeaderText="Description"
ReadOnly="true"
DataField="Item"/>
<asp:TemplateColumn>
<HeaderTemplate>
<b> Tax </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label
Text='<%# DataBinder.Eval(Container.DataItem, "Tax") %>'
runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox
Text="Taxable"
runat="server"/>
</EditItemTemplate>
<FooterTemplate>
<asp:HyperLink id="HyperLink1"
Text="Microsoft"
NavigateUrl="http://www.microsoft.com"
runat="server"/>
</FooterTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Price"
DataField="Price"/>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>