|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionASP.NET 3.5 introduces a new data binding control named the ListView. ASP.NET already has a lot data bind control; it should be more than 10. But the good news is, ListView can literally replace all other data binding controls in ASP.NET. ListView control makes data binding easier than previous controls. It has included styling with CSS, flexible pagination, and sorting, inserting, deleting, and updating features. Complete ListView
In this article I will describe features in ListView step by step with related code review
//very simple databinding in ListView <LayoutTemplate> <table border="0" cellpadding="1"> <tr style="background-color:#E5E5FE"> <th align="left"><asp:LinkButton ID="lnkId" runat="server">Id</asp:LinkButton></th> <th align="left"><asp:LinkButton ID="lnkName" runat="server">Name</asp:LinkButton></th> <th align="left"><asp:LinkButton ID="lnkType" runat="server">Type</asp:LinkButton></th> <th></th> </tr> <tr id="itemPlaceholder" runat="server"></tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td> <td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+" "+Eval("LastName") %></asp:Label></td> <td><asp:Label runat="server" ID="lblType"><%#Eval("ContactType") %></asp:Label></td> <td></td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr style="background-color:#EFEFEF"> <td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td> <td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+" "+Eval("LastName") %></asp:Label></td> <td><asp:Label runat="server" ID="lblType"><%#Eval("ContactType") %></asp:Label></td> <td></td> </tr> </AlternatingItemTemplate>
<asp:DataPager ID="ItemDataPager" runat="server" PageSize="5"> <Fields> <asp:NumericPagerField ButtonCount="2" /> </Fields> </asp:DataPager>
// to allow sort if click on the header of the table make table header controls clikable //and give commandname and commandargument <tr style="background-color:#E5E5FE"> <th align="left"><asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="ID">Id</asp:LinkButton></th> <th align="left"><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="FirstName">Name</asp:LinkButton></th> <th align="left"><asp:LinkButton ID="lnkType" runat="server" CommandName="Sort" CommandArgument="ContactType">Type</asp:LinkButton></th> <th></th> </tr>
<InsertItemTemplate> <tr runat="server"> <td></td> <td> <asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>' Width="100px">First Name</asp:TextBox> <asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>' Width="100px">Last Name</asp:TextBox> </td> <td><asp:TextBox ID="txtCtype" runat="server" Text='<%#Eval("ContactType") %>' Width="100px">Contact Type</asp:TextBox></td> <td><asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /></td> </tr> </InsertItemTemplate>
if (e.CommandName == "Insert") { TextBox txtFname = (TextBox)e.Item.FindControl("txtFname"); TextBox txtLname = (TextBox)e.Item.FindControl("txtLname"); TextBox txtCtype = (TextBox)e.Item.FindControl("txtCtype"); string insertCommand = "Insert into [Contacts] ([FirstName],[LastName],[ContactType]) Values('" + txtFname.Text + "', '" + txtLname.Text + "', '" + txtCtype.Text + "');"; SqlDataSource1.InsertCommand = insertCommand; }
History
|
|||||||||||||||||||||||||||||||||||||||||||