2011-06-01 66 views
0

我有一個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被正確引用?

+0

我認爲,我們需要看到更多的代碼 – cbp 2011-06-01 04:17:29

+0

什麼是DoSomething的方法 – 2011-06-01 04:57:49

回答

1

檢查呈現給客戶端的實際HTML和JavaScript,以確保沒有與控件滑動通過裂縫相關的重複ID。

+0

代碼我以前檢查了所有控件ID,以確保沒有DUP的,但不是的JavaScript。問題是我有重複的JavaScript代碼,並沒有正確的對話框,從而觸發錯誤的按鈕。謝謝! – trbldintyrd 2011-06-01 20:18:56

1

我覺得這是問題

MyControl ucAttachments1 = (MyControl) Page.LoadControl("~/controls/mycontrol.ascx"); 

MyControl ucAttachments2 = (MyControl)Page.LoadControl("~/controls/mycontrol.ascx"); 

你引用的控制,以兩個不同的變量相同的實例。所以現在你有兩個不同的同一個實例的引用,現在,因爲你最後設置了「ucAttachments2」的屬性,所以發生了什麼是你的第二個控制屬性正在設置到實例..因此,每當你嘗試訪問該實例通過使用「ucAttachments1」或「ucAttachments2」),您將獲得第二個控件的屬性。

嘗試做:

MyControl ucAttachments1 = new 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 = new 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); 
相關問題