分享
 
 
 

MSDN DataGrid嵌套DataList (saucer(思归)版)

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassedititemtemplatetopic.asp

<%@ Page Language="C#" AutoEventWireup="True" %>

<%@ Import Namespace="System.Data" %>

<html>

<head>

<script runat="server">

// The Cart and CartView objects temporarily store the data source

// for the DataList control while the page is being processed.

DataTable Cart = new DataTable();

DataView CartView;

void Page_Load(Object sender, EventArgs e)

{

// 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 DataList 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 (!IsPostBack)

{

string[] slist = new string[]{"a","b","c"};

DataGrid1.DataSource = slist;

DataGrid1.DataBind();

}

}

void BindList(DataList ItemsList)

{

// Set the data source and bind to the DataList control.

ItemsList.DataSource = CartView;

ItemsList.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)

{

// Create the sample data.

DataRow dr;

// Define the columns of the table.

Cart.Columns.Add(new DataColumn("Qty", typeof(Int32)));

Cart.Columns.Add(new DataColumn("Item", typeof(String)));

Cart.Columns.Add(new DataColumn("Price", typeof(Double)));

// Store the table in session state to persist its values

// between posts to the server.

Session["ShoppingCart"] = Cart;

// Populate the DataTable with sample data.

for (int i = 1; i <= 5; i++)

{

dr = Cart.NewRow();

if (i % 2 != 0)

{

dr[0] = 2;

}

else

{

dr[0] = 1;

}

dr[1] = "Item " + i.ToString();

dr[2] = (1.23 * (i + 1));

Cart.Rows.Add(dr);

}

}

else

{

// Retrieve the sample data from session state.

Cart = (DataTable)Session["ShoppingCart"];

}

// Create a DataView and specify the field to sort by.

CartView = new DataView(Cart);

CartView.Sort="Item";

return;

}

void Edit_Command(Object sender, DataListCommandEventArgs e)

{

// Set the EditItemIndex property to the index of the item clicked

// in the DataList control to enable editing for that item. Be sure

// to rebind the DataList to the data source to refresh the control.

DataList ItemsList = (DataList)sender;

ItemsList.EditItemIndex = e.Item.ItemIndex;

BindList(ItemsList);

}

void Cancel_Command(Object sender, DataListCommandEventArgs e)

{

// Set the EditItemIndex property to -1 to exit editing mode. Be sure

// to rebind the DataList to the data source to refresh the control.

DataList ItemsList = (DataList)sender;

ItemsList.EditItemIndex = -1;

BindList(ItemsList);

}

void Delete_Command(Object sender, DataListCommandEventArgs e)

{

// Retrieve the name of the item to remove.

String item = ((Label)e.Item.FindControl("ItemLabel")).Text;

// Filter the CartView for the selected item and remove it from

// the data source.

CartView.RowFilter = "Item='" + item + "'";

if (CartView.Count > 0)

{

CartView.Delete(0);

}

CartView.RowFilter = "";

// Set the EditItemIndex property to -1 to exit editing mode. Be sure

// to rebind the DataList to the data source to refresh the control.

DataList ItemsList = (DataList)sender;

ItemsList.EditItemIndex = -1;

BindList(ItemsList);

}

void Update_Command(Object sender, DataListCommandEventArgs e)

{

// Retrieve the updated values from the selected item.

String item = ((Label)e.Item.FindControl("ItemLabel")).Text;

String qty = ((TextBox)e.Item.FindControl("QtyTextBox")).Text;

String price = ((TextBox)e.Item.FindControl("PriceTextBox")).Text;

// 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.

// Filter the CartView for the selected item and remove it from

// the data source.

CartView.RowFilter = "Item='" + item + "'";

if (CartView.Count > 0)

{

CartView.Delete(0);

}

CartView.RowFilter = "";

// ***************************************************************

// Insert data validation code here. Make sure to validate the

// values entered by the user before converting to the appropriate

// data types and updating the data source.

// ***************************************************************

// Add a new entry to replace the previous item.

DataRow dr = Cart.NewRow();

dr[0] = qty;

dr[1] = item;

// If necessary, remove the '$' character from the price before

// converting the price to a Double.

if(price[0] == '$')

{

dr[2] = Convert.ToDouble(price.Substring(1));

}

else

{

dr[2] = Convert.ToDouble(price);

}

Cart.Rows.Add(dr);

// Set the EditItemIndex property to -1 to exit editing mode.

// Be sure to rebind the DataList to the data source to refresh

// the control.

DataList ItemsList = (DataList)sender;

ItemsList.EditItemIndex = -1;

BindList(ItemsList);

}

</script>

</head>

<body>

<form runat=server ID="Form1">

<h3>DataList Edit Example</h3>

Click <b>Edit</b> to edit the values of the item.

<br><br>

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="false">

<Columns>

<asp:TemplateColumn HeaderText="Count">

<ItemTemplate>

<%#Container.ItemIndex+1%>

</ItemTemplate>

</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Test">

<ItemTemplate>

<asp:DataList id="ItemsList01" DataSource='<%#CartView%>'

GridLines="Both"

RepeatColumns="3"

RepeatDirection="Horizontal"

CellPadding="3"

CellSpacing="0"

OnEditCommand="Edit_Command"

OnUpdateCommand="Update_Command"

OnDeleteCommand="Delete_Command"

OnCancelCommand="Cancel_Command"

runat="server">

<HeaderStyle BackColor="#aaaadd">

</HeaderStyle>

<AlternatingItemStyle BackColor="Gainsboro">

</AlternatingItemStyle>

<EditItemStyle BackColor="yellow">

</EditItemStyle>

<HeaderTemplate>

Items

</HeaderTemplate>

<ItemTemplate>

Item:

<%# DataBinder.Eval(Container.DataItem, "Item") %>

<br>

Quantity:

<%# DataBinder.Eval(Container.DataItem, "Qty") %>

<br>

Price:

<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>

<br>

<asp:LinkButton id="EditButton"

Text="Edit"

CommandName="Edit"

runat="server"/>

</ItemTemplate>

<EditItemTemplate>

Item:

<asp:Label id="ItemLabel"

Text='<%# DataBinder.Eval(Container.DataItem, "Item") %>'

runat="server"/>

<br>

Quantity:

<asp:TextBox id="QtyTextBox"

Text='<%# DataBinder.Eval(Container.DataItem, "Qty") %>'

runat="server"/>

<br>

Price:

<asp:TextBox id="PriceTextBox"

Text='<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>'

runat="server"/>

<br>

<asp:LinkButton id="UpdateButton"

Text="Update"

CommandName="Update"

runat="server"/>

<asp:LinkButton id="DeleteButton"

Text="Delete"

CommandName="Delete"

runat="server"/>

<asp:LinkButton id="CancelButton"

Text="Cancel"

CommandName="Cancel"

runat="server"/>

</EditItemTemplate>

</asp:DataList>

</ItemTemplate>

</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

<asp:Button runat="server" Text="Refresh" ID="Button1" NAME="Button1"/>

</form>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有