ASP.NET 3.5中的ListView控件和DataPager控件(二)

ListView中添加和编辑数据
ListView
通过EditItemTemplateInsertItemTemplate来提供编辑数据和添加数据的功能。这个功能的使用非常类似于GridView的编辑特性的使用,只不过它用的都是自定义模板。

<asp:ListView ID="lvItems" runat="server" 
              DataSourceID
="Data" 
              ItemContainerID
="layoutTemplate" 
              DataKeyNames
="Pk"
              InsertItemPosition
="None"               
>

    
<Layouttemplate>                
        
<div id="layoutTemplate" runat="server" />                        
    
</Layouttemplate>        
        
    
<ItemTemplate>
        
<div class="itemdisplay">
        
<b><% Eval("Sku") %></b><br />
        <%
Eval("Abstract") %></div>
        
        
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
        
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
    
</ItemTemplate>
    
<AlternatingItemTemplate >
        
<div class="itemdisplayalternate">
        
<b><% Eval("Sku") %></b><br />
        <%
Eval("Abstract") %></div>
        
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Edit" />
        
<asp:Button ID="Button2" runat="server" CommandName="Delete" Text="Delete" />
    
</AlternatingItemTemplate>
    
<EditItemTemplate>
        
<div class="gridalternate">
        Sku: 
<asp:TextBox runat="server" ID="txtSku" Text='<%# Bind("Sku") %>'></asp:TextBox>
        
<br />
        Abstract: 
<asp:TextBox  runat="server" id="txtAbstract" Text='<%# Bind("Abstract") %>'></asp:TextBox>
        
<br />
        
<asp:Button ID="Button3" runat="server" CommandName="Update" Text="Update" />
        
<asp:Button ID="Button4" runat="server"
                    CommandName
="Cancel" Text="Cancel" /><br />
         
</div>
    
</EditItemTemplate>
    
<InsertItemTemplate>
        
<div style="background:Yellow">
        
<asp:TextBox runat="server" ID="txtSku" Text='<%# Bind("Sku") %>'></asp:TextBox>
        
<br />
        
<asp:TextBox  runat="server" id="txtAbstract" Text='<%# Bind("Abstract") %>'></asp:TextBox>
        
<br />
        
</div>
        
<asp:Button ID="Button3" runat="server" CommandName="Inser" Text="Insert" />
        
<asp:Button ID="Button4" runat="server"
                    CommandName
="Cancel" Text="Cancel" /><br />
    
</InsertItemTemplate>            
</asp:ListView>


在本例中我使用的是SqlDataSource(我比较懒),SqlDataSource中的InsertUpdate语句是你必须要提供的。 InsertItemTemplateListView中新增的非常受欢迎的模板,我们可以把它的UI设置成与编辑模板相一致。我们还可以通过InsertItemPosition属性来指定插入模板的位置,它可以是FirstItemLastItemNone。一般来说,应该把它设置为None,然后通过某个按钮来设置插入模板的显示位置(FirstItemLastItem)。示例代码如下:

protected void btnAddItem_Click(object sender, EventArgs e)
{
    
this.lvItems.InsertItemPosition = InsertItemPosition.FirstItem;
}

protected void lvItems_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    
if (e.CommandName == "Update")
    
{
        TextBox tb = e.Item.FindControl("txtSku") 
as TextBox;
        
this.lvItems.InsertItemPosition = InsertItemPosition.None;
        Response.Write(tb.Text);
    }
    
if (e.CommandName == "Cancel")
    
{
        
this.lvItems.InsertItemPosition = InsertItemPosition.None;
    }   
    
}


你可以在OnItemCommand中写上自己的逻辑,使得一旦执行了UpdateCancel命令就设置InsertItemPositionNone


分组
大概介绍一下,最后生成的HTML代码会先用GroupTemplate分组,然后再以LayoutTemplate做容器包裹起来。

    <Layouttemplate>                
        
<div id="groupContainer" runat="server" >                                
        
</div>
    
</Layouttemplate> 
    
<GroupTemplate>        
        
<div class="blockheader" style="height:23px;padding:7px">Group Header:</div>
        
<div id="layoutTemplate" runat="server" />                        
    
</GroupTemplate> 


你可以设置ListViewGroupItemCount属性,来指定每组显示多少条记录。


总结
ListView
ASP.NET中新增的一个非常酷的控件。 在本文中我已经介绍过了,相对于GridView来说它有着更为丰富的布局手段,你可以在它的模板内写任何HTML标记或者控件。如果你使用过RepeaterGridView的话,那么你将会轻松的上手ListView,不过很明显地,你也将要手写更多的HTML标记。 但是,它也将会给我们带来更多的布局上的自由度,同时也具有编辑、插入等特性。这就是ASP.NET 3.5给我们带来的非常棒的控件。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值