我正在为 Umbraco 创建一个自定义数据类型。这种数据类型只是从数据库中读取记录并将它们呈现在 html 表中,没有重复器。在每一行中,最初有两个按钮,一个用于编辑记录,一个用于删除记录。
单击编辑时,该行将进入编辑模式,其中使用文本框代替文字,并且按钮不是“编辑”和“删除”,而是“保存”和“取消”。单击这些按钮中的任何一个似乎都不起作用。他们应该发射的命令不是发射。
我读过关于必须在页面加载之前创建这些按钮的信息,我这样做了。我对每种类型都有一个列表(保存、取消)。那是在 Init 上生成的,ids 是基于记录的 ids 的。这里也分配了命令和命令参数。
但是,这些命令似乎仍然没有触发。
代码
public class ProductManagerDataEditor : System.Web.UI.UpdatePanel, umbraco.interfaces.IDataEditor
{
private umbraco.interfaces.IData _data;
//Properties
private List<Record> Records { get; set; }
private int EditRecordID = 0;
//Editor Controls
TextBox tb_EditName;
TextBox tb_EditPrice;
Button btn_Add;
List<Button> EditButtons;
List<Button> DeleteButtons;
List<Button> SaveButtons;
List<Button> CancelButtons;
/// <summary>
/// Constructor.
/// Reads in curent data along with configuration settings for this data type.
/// </summary>
/// <param name="Data"></param>
/// <param name="Configuration"></param>
public ProductManagerDataEditor(umbraco.interfaces.IData Data, SortedList<String, String> Configuration)
{
_data = Data;
}
public virtual bool TreatAsRichTextEditor
{
get { return false; }
}
public bool ShowLabel
{
get { return true; }
}
public Control Editor { get { return this; } }
public void Save()
{
}
/// <summary>
/// On Init Method
/// </summary>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
/// <summary>
/// On Load Method
/// </summary>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
LoadRecords();
GenerateControls();
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
RenderTableContents(writer);
}
/// <summary>
/// Generates the buttons to be used
/// </summary>
protected void GenerateControls()
{
SaveButtons = new List<Button>();
CancelButtons = new List<Button>();
DeleteButtons = new List<Button>();
EditButtons = new List<Button>();
//Other Buttons
foreach (Record r in Records)
{
//Save Button
Button btn_Save = new Button();
btn_Save.CssClass = "btn-save";
btn_Save.ID = "btnSave_" + r.ID;
btn_Save.Text = "Save";
btn_Save.Command += new CommandEventHandler(Save);
btn_Save.CommandArgument = r.ID.ToString();
//Cancel Edting Button
Button btn_Cancel = new Button();
btn_Cancel.CssClass = "btn-cancel";
btn_Cancel.ID = "btnCancel";
btn_Cancel.Text = "Cancel";
btn_Cancel.Click += new EventHandler(CancelEdit);
//Delete Button
Button btn_Del = new Button();
btn_Del.CssClass = "btn-delete";
btn_Del.ID = "btnDelete_" + r.ID;
btn_Del.Text = "Delete";
btn_Del.Command += new CommandEventHandler(Delete);
btn_Del.CommandArgument = r.ID.ToString();
//Edit Button
Button btn_Edit = new Button();
btn_Edit.CssClass = "btn-edit";
btn_Edit.ID = "btnEdit_" + r.ID;
btn_Edit.Text = "Edit";
btn_Edit.Command += new CommandEventHandler(EditRecord);
btn_Edit.CommandArgument = r.ID.ToString();
SaveButtons.Add(btn_Save);
CancelButtons.Add(btn_Cancel);
DeleteButtons.Add(btn_Del);
EditButtons.Add(btn_Edit);
}
}
/// <summary>
/// Loads the records from the database
/// </summary>
protected void LoadRecords()
{
Records = //Loaded from db...
}
/// <summary>
/// Renders The Table of Records
/// </summary>
protected void RenderTableContents(HtmlTextWriter w)
{
bool editMode = false;
w.Write("<table class='product-manager' cellspacing='0' cellpadding='3px'>");
w.Write("<tr class='head'>");
w.Write("<th>Name</th>");
w.Write("<th>Price</th>");
w.Write("<th></th>");
w.Write("<th></th>");
w.Write("</tr>");
int buttonIndex = 0;
foreach (Record r in Records)
{
//If editing this record, display text boxes
if (EditRecordID == r.ID)
{
editMode = true;
tb_EditName = new TextBox { Text = r.Name };
tb_EditPrice = new TextBox { Text = r.Price.ToString() };
w.Write("<tr>");
w.Write("<td>");
tb_EditName.RenderControl(w);
w.Write("</td>");
w.Write("<td>");
tb_EditPrice.RenderControl(w);
w.Write("</td>");
w.Write("<td>");
SaveButtons[buttonIndex].RenderControl(w);
w.Write("</td>");
w.Write("<td>");
CancelButtons[buttonIndex].RenderControl(w);
w.Write("</td>");
w.Write("</tr>");
}
//Not in edit mode
else
{
w.Write("<tr>");
w.Write("<td>" + t.Name + "</td>");
w.Write("<td>£" + t.Price + "</td>");
w.Write("<td>");
EditButtons[buttonIndex].RenderControl(w);
w.Write("</td>");
w.Write("<td>");
DeleteButtons[buttonIndex].RenderControl(w);
w.Write("</td>");
w.Write("</tr>");
}
buttonIndex++;
}
w.Write("</table>");
}
#region Events
/// <summary>
/// Sets the id of the row to edit
/// </summary>
protected void EditRecord(object sender, CommandEventArgs e)
{
EditRecordID = e.CommandArgument.ToString();
}
/// <summary>
/// Cancels editing mode
/// </summary>
protected void CancelEdit(object sender, EventArgs e)
{
EditRecordID = 0;
}
/// <summary>
/// Deletes Selected Record
/// </summary>
protected void DeleteRecord(object sender, CommandEventArgs e)
{
//TODO
}
/// <summary>
/// Updates Record Details
/// </summary>
protected void Save(object sender, CommandEventArgs e)
{
//Update the Record
}
#endregion
}