Tuesday, August 28, 2012

DataListControl Continuing..

  public void FillDataList()
        {
            Test1BL oTest1BL = new Test1BL();
            dlistTest.DataSource = oTest1BL.GetDetails();
            dlistTest.DataBind();          
        }

        protected void dlistTest_EditCommand(object source, DataListCommandEventArgs e)
        {          
            dlistTest.EditItemIndex = e.Item.ItemIndex;
            FillDataList();
        }

        protected void dlistTest_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            int ID = Convert.ToInt32(((Label)e.Item.FindControl("lblID")).Text);
            string Name = ((TextBox)e.Item.FindControl("txtName")).Text;
            int Age = Convert.ToInt32(((TextBox)e.Item.FindControl("txtAge")).Text);

            Test1BL oTest1BL = new Test1BL();
            oTest1BL.UpdateData(ID, Name, Age);
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }
protected void dlistTest_CancelCommand(object source, DataListCommandEventArgs e)
        {
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }

        protected void dlistTest_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int ID = Convert.ToInt32(((Label)e.Item.FindControl("lblIDDelete")).Text);
            Test1BL oTest1BL = new Test1BL();
            oTest1BL.DeleteData(ID);
            dlistTest.EditItemIndex = -1;
            FillDataList();
        }

DataList Control ASPX

<asp:DataList ID="dlistTest" runat="server" Style="width: 30%" AlternatingItemStyle-BackColor="AliceBlue" BorderWidth="2px" OnEditCommand="dlistTest_EditCommand"
onupdatecommand="dlistTest_UpdateCommand" oncancelcommand="dlistTest_CancelCommand"
 ondeletecommand="dlistTest_DeleteCommand">
 <HeaderTemplate>
 <table style="width: 100%">
<tr><td style="width: 40%">Name </td><td style="width: 40%">Age</td>
<td style="width: 20%"></td></tr></table>
 </HeaderTemplate>
<ItemTemplate>
 <table style="width: 100%"> <tr> <td style="width: 30%">
 <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name")%>'></asp:Label>
 </td><td style="width: 30%">
 <asp:Label runat="server" ID="lblAge" Text='<%#Eval("Age")%>'></asp:Label></td>
<td style="width: 40%">
<asp:Label runat="server" ID="lblIDDelete" Text='<%#Eval("ID")%>' ></asp:Label>
<asp:LinkButton runat="server" ID="lbtnEdit" CommandName="Edit">Edit</asp:LinkButton>&nbsp;&nbsp;
<asp:LinkButton runat="server" ID="lbtnDelete" CommandName="Delete">Delete</asp:LinkButton>
 </td></tr></table>
</ItemTemplate>
<EditItemTemplate>
 <table style="width: 100%">
 <tr><td style="width: 10%">Name</td>
<td style="width: 20%">
<asp:TextBox runat="server" ID="txtName" Text='<%#Bind("Name")%>' Width="60px"></asp:TextBox>
</td>
<td style="width: 10%">Age </td>
<td style="width: 20%">
<asp:TextBox runat="server" ID="txtAge" Text='<%#Bind("Age")%>' Width="60px"></asp:TextBox>
</td>
<td style="width: 40%">
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID")%>' ></asp:Label></td>
</tr><tr><td></td><td></td>
<td colspan="3">
<asp:Button runat="server" ID="btnUpdate" CommandName="Update" Text="Update" />
 <asp:Button runat="server" ID="btnCancel" CommandName="Cancel" Text="Cancel" />
</td></tr></table>
</EditItemTemplate>
</asp:DataList>