我有一個UserControl
,它允許用戶上傳文件並將它們顯示在GridView
中。在父頁面上,我有一個jQuery選項卡控件,我在其中動態添加了我的UserControl
(在不同的選項卡上)的2個實例。第二個實例工作正常,所以我知道控制工程。但是,當我嘗試使用第一個實例上載文件時,第二個實例正在被引用......所以所有屬性值,控件名稱等都指向第二個實例。在一個頁面上的多個usercontrol實例,但只有最後一個控件被引用
這是怎麼了加載在父頁面代碼隱藏控件:
protected void Page_Load(object sender, EventArgs e)
{
MyControl ucAttachments1 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx");
ucAttachments1.ID = "ucAttachments1";
ucAttachments1.Directory = "/uploads/documents";
ucAttachments1.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
phAttachments1.Controls.Add(ucAttachments1);
MyControl ucAttachments2 = (MyControl)Page.LoadControl("~/controls/mycontrol.ascx");
ucAttachments2.ID = "ucAttachments2";
ucAttachments2.Directory = "/uploads/drawings";
ucAttachments2.DataChanged += new MyControl.DataChangedEventHandler(DoSomething);
phAttachmetns2.Controls.Add(ucAttachments2);
}
在HTML
:
<div id="tabContainer">
<div id="files">
<asp:PlaceHolder id="phAttachments1" runat="server" />
</div>
<div id="drawings">
<asp:PlaceHolder id="phAttachments2" runat="server" />
</div>
</div>
用戶控件代碼片段:
private string directory;
override protected void OnLoad(EventArgs e)
{
PopulateAttachmentGridview();
}
protected btnUpload_Click(object sender, EventArgs e)
{
UploadFile(directory);
}
public string Directory
{
get { return directory; }
set { directory = value; }
}
如何確保我的usercontrols被正確引用?
我認爲,我們需要看到更多的代碼 – cbp 2011-06-01 04:17:29
什麼是DoSomething的方法 – 2011-06-01 04:57:49