Admin panel to edit weight
This article shows how to create an admin panel to edit the maximum weight of each delivery method.
The admin panels can be added from back office Settings > System settings > Areas. Select an area (Sales in this case) and click Panels.

Then click New panel. Two .ascx user controls can be specified for each panel. For this example, we create the two .ascx controls as shown in the below image.

The content panel is displayed in the Sales area:

The admin panel is accessible from back office Settings > System settings > Areas > select Sales > Panels:

SampleDeliveryCostAdmin user control
This control is created in the wwwroot\Site\ECommerce\Panels\ folder, and named SampleDeliveryCostAdmin.ascx.
The .ascx file is a simple grid containing the delivery method name and weight, and a save button.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SampleDeliveryCostAdmin.ascx.cs" Inherits="Site.ECommerce.Panels.SampleDeliveryCostAdmin" %>
<style type="text/css">
table.common { border-spacing: 0; border-collapse: collapse; empty-cells: show; line-height: 1.2;}
table.common th, table.common td { padding: .5em .5em .4em .5em; border-color: #fff; border-style: solid; border-width: 1px 0; }
table.common th + th, table.common td + td, table.common tbody th + td { border-left-width: 1px; }
table.common thead th { background-color: #d0d0d0; }
table.common tbody th, table.common tbody td { background-color: #eee; }
table.common th[scope=row] { border-right-width: 1px; white-space: nowrap; }
</style>
<div>
<div style="float:left">
<asp:Repeater ID="RepeaterDeliveryMethods" runat="server">
<HeaderTemplate>
<table class="common">
<thead>
<tr>
<th scope="col">
Delivery method
</th>
<th scope="col">
Weight upto (grms)
</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="DeliveryMethodName" Text='<%# Eval("DeliveryMethodName") %>' runat="server" /> </td>
<td><asp:TextBox ID="WeightUpto" Text='<%# Eval("WeightUpto") %>' runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
<div>
<asp:Button ID="Save" Text="Save" runat="server" OnClick="OnSave" />
</div>
</div>
In the code behind file, we load the grid from the DeliveryMethodWeightLimits class, and when the save button is clicked we use the save method defined in it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Litium.Studio.KC.Samples.DeliveryCost;
namespace Site.ECommerce.Panels
{
/// <summary>
/// Admin panel of delivery cost selection.
/// </summary>
public partial class SampleDeliveryCostAdmin : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//there can be new delivery methods added since last updated., therfore refresh.
DeliveryMethodWeightLimits.Instance.Refresh();
var deliveryMethodWieghts = from d in DeliveryMethodWeightLimits.Instance
select new DeliveryMethodWeight(){ DeliveryMethodName = d.Key,
WeightUpto = d.Value };
RepeaterDeliveryMethods.DataSource = deliveryMethodWieghts;
RepeaterDeliveryMethods.DataBind();
}
}
protected void OnSave(object sender, EventArgs e)
{
var deliveryMethodWeights = new Dictionary<string, int>();
foreach (RepeaterItem item in RepeaterDeliveryMethods.Items)
{
var deliveryMethodNameLabel = item.FindControl("DeliveryMethodName") as Label;
var weightUptoTextBox = item.FindControl("WeightUpto") as TextBox;
int deliveryMethodWeight = DeliveryMethodWeightLimits.DefaultMaxWeight;
int.TryParse(weightUptoTextBox.Text, out deliveryMethodWeight);
if (deliveryMethodNameLabel != null && weightUptoTextBox != null)
{
DeliveryMethodWeightLimits.Instance.SetMaxWeight(deliveryMethodNameLabel.Text, deliveryMethodWeight);
}
}
DeliveryMethodWeightLimits.Instance.Save();
}
/// <summary>
/// Class to databind the delivery method weight grid in the UI control.
/// </summary>
public class DeliveryMethodWeight
{
/// <summary>
/// The name of the delivery method.
/// </summary>
public string DeliveryMethodName { get; set; }
/// <summary>
/// Max weight.
/// </summary>
public int WeightUpto { get; set; }
}
}
}