gpt4 book ai didi

c# - 动态创建的命令按钮不触发命令

转载 作者:太空宇宙 更新时间:2023-11-03 13:56:26 26 4
gpt4 key购买 nike

我正在为 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>&pound;" + 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

}

最佳答案

我试过你的代码,显然有问题的代码是渲染。 UpdatePanel 根本没有将按钮视为触发器。

解决方案是将控件添加到 asp:Table 中的面板,并让 UpdatePanel 进行渲染,因此跳过渲染代码。

这是您需要更改的内容(这仅用于编辑按钮,但您可以为所有按钮添加代码):

protected void GenerateControls()
{
var table = new Table();
var headerRow = new TableHeaderRow();
var headerCell = new TableHeaderCell();
headerCell.Text = "Name";
headerRow.Cells.Add(headerCell);
headerCell = new TableHeaderCell();
headerCell.Text = "Price";
headerRow.Cells.Add(headerCell);
headerCell = new TableHeaderCell();
headerRow.Cells.Add(headerCell);
headerCell = new TableHeaderCell();
headerRow.Cells.Add(headerCell);
table.Rows.Add(headerRow);

//Other Buttons
foreach (Record r in Records)
{
var row = new TableRow();

var cell = new TableCell();
//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();
cell.Controls.Add(btn_Edit);
row.Cells.Add(cell);
table.Rows.Add(row);
}

ContentTemplateContainer.Controls.Add(table);
}

顺便说一句,这看起来像一个网格。为什么不使用 GridViewListView

关于c# - 动态创建的命令按钮不触发命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12026679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com