- 爱易网页
-
ASP.NET教程
- 关于datagrid的使用以及动态修改,以及使用存储过程的介绍
日期:2013-10-07 浏览次数:20460 次
1.关于Datagrid,Datalist等数据绑定控件
由于vs.net是Codebehind的, 而quickstart 上的例子是代码与页面混合的,所以许多朋友有些疑惑。特别是有关template中如何触发事件的问题。
是这样,许多server control都有一个commandname属性
就是在.aspx中设置你那个edit的commandname,在.cs中用e.CommandName 得到
如
///aspx页面
<asp:DataList id=usersList runat="server" >
<template name="ItemTemplate">
<asp:ImageButton id=ImageButton1 runat="server" ImageUrl="../images/edit.gif" AlternateText="编辑条目" CommandName="edit"></asp:ImageButton>
<asp:ImageButton id=ImageButton2 runat="server" ImageUrl="../images/delete.gif" AlternateText="删除条目" CommandName="delete"></asp:ImageButton>
<asp:Label id=lblRole runat="server" Text="<%# Container.DataItem%>" CssClass="Normal"></asp:Label>
</template>
<template name="EditItemTemplate">
<span class="Normal">email (cookie auth) or DOMAIN\username (Windows auth)</span><br />
<asp:Textbox id=userName width="200" cssclass="NormalTextBox" Text="<%# Container.DataItem %>" runat="server" />
<asp:LinkButton Text="Apply" CommandName="apply" cssclass="CommandButton" runat="server" ID=ApplyButton/>
</template>
</asp:DataList>
///cs文件
protected void UsersList_ItemCommand (Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "edit") {
// Set editable list item index if "edit" button clicked next to the item
usersList.EditItemIndex = e.Item.ItemIndex;
// Repopulate the datalist control
BindData();
}
else if (e.CommandName == "apply") {
// Obtain the current portal settings
PortalConfigurationData portalConfig = PortalConfiguration.LoadSettings(PortalConfiguration.ConfigFilePath);
// Update user entry
portalConfig.SecurityRoles[roleIndex].Users[e.Item.ItemIndex] = ((TextBox) e.Item.FindControl("userName")).Text;
// Persist Settings back to disk
PortalConfiguration.PersistSettings(portalConfig);
// Disable editable list item access
usersList.EditItemIndex = -1;
// Repopulate the datalist control