編輯(更簡單):動態添加用戶控件之間切換顯示舊值
我有一個控制的「配置」與左邊的樹視圖和麪板上,我加入我的控制權名單。這些元素被放置在一張桌子內並被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時,儘管car1的數據已填滿,但仍然顯示轎廂2的值。看到car1的數據,我必須切換到完全不同類型的控制,然後返回到car1。
另一個問題是,我有一個二進制圖像內的用戶控件哪些數據我填寫init。當我切換到下一輛車時,在那裏我沒有配置圖像,我將圖像數據設置爲空,但是顯示的是前一輛車的圖像。 Onyl如果我設置了不同的圖像數據,則會顯示新圖像。
還有一件我注意到的: 在用戶控件內部,我調用了javascript函數,但只有在單擊保存按鈕時才調用此函數。似乎樹節點的切換和保存按鈕單擊之間有不同?
ScriptManager.RegisterStartupScript(this, this.GetType(), "InitScrewPointsForDragging", "InitScrewPointsForDragging();", true);
我希望這比以前更簡短。
你能否更簡單地解釋你的答案請 –
我更新了我的問題,我希望現在更清楚。如果沒有,請讓我知道,我會再試一次 – Caipigott