编辑(更简要):
我有一个控件“配置”,左侧是树视图,右侧是我添加控件的面板列表。这些元素放在一个表格内,并由一个ajax面板包围。
假设我有一个带有两个元素car1和car2的树节点汽车。当我单击一个项目时,我在隐藏字段中保存所选树节点的ID和类型,以便能够在Page_Init上加载正确的用户控件
protected void Page_Init(object sender, EventArgs e)
{
var type = this.Request.Form["ctl00$MainContent$ctl00$typeId"];
var id = this.Request.Form["ctl00$MainContent$ctl00$entityId"];
if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(id))
{
this.LoadUserControl(type, id);
}
}
private void LoadUserControl(string type, string id)
{
if (Convert.ToInt32(type) != 0)
{
switch (Convert.ToInt32(type))
{
case (int)SkTopics.Product:
this.AddUserControl("../Modules/Product.ascx", this.Product, type, id);
break;
}
}
}
private void AddUserControl(string controlName, Panel panel, string type, string id)
{
// Init new user cotntrol
control = (BaseControl)this.LoadControl(controlName);
control.LoadEntityItem(Convert.ToInt32(id));
// Setting the user control ID
var replace = controlName.Replace("../Modules/", string.Empty);
string userControlId = replace.Split('.')[0] + id;
var targetControl = panel.FindControl(userControlId);
if (object.Equals(targetControl, null))
{
control.ID = userControlId;
control.DataBind();
panel.Controls.Add(control);
}
}
在LoadEntityItem上,从数据库中收集此汽车的数据,并将值设置为文本框的文本。当我现在切换到第二辆车时,会显示新值。
问题:我更改了文本框内的值,然后按“保存”按钮调用用户控件的保存方法。从现在开始,当我再次切换到汽车1时,仍然显示汽车2的值,尽管car1的数据已经填满。看到car1的数据我必须切换到完全不同类型的控制然后再回到car1。
另一个问题是,我在用户控件中有一个二进制图像,我在init上填充了Data。当我转向下一辆车时,我没有配置图像,我将图像数据设置为空,但我显示的是前一辆车的图像。 Onyl如果我设置了不同的图像数据,则会显示新图像。
我注意到了其他一些事情: 在用户控件内部,我调用了一个javascript函数,但只有在单击“保存”按钮时才会调用此函数。树节点的切换和保存按钮单击?
之间似乎有所不同ScriptManager.RegisterStartupScript(this, this.GetType(), "InitScrewPointsForDragging", "InitScrewPointsForDragging();", true);
我希望这比以前更简短。