2012-04-14 51 views
0

我在我的頁面中有3個用戶控件。此用戶控件之一是關於fileUploading,其次是關於fileUploade Keyword。當文件上傳文件時,上傳usercontrol返回插入的記錄的Id。現在我想要在fileUploaded關鍵字中使用此id,但是當插入標籤以便在文件上傳用戶控件中顯示id時,請右鍵單擊id forexample 1或2或..但在關鍵字usercontrol中只顯示0值。我使用實體框架工作。我如何在關鍵字用戶控件中訪問此ID。 謝謝。如何在用戶控件之間傳遞ASP.NET的值C#

回答

0

您可以使用委託傳遞ID回父頁面。當id傳遞迴父頁面時,在父頁面上運行一個函數,該函數調用關鍵字usercontrol上的公共函數。

所以在你上傳用戶控件,首先創建一個父頁面將要監聽的委託:

public delegate void PassIDToParent(string ID); 
public event PassIDToParent PassID; 

接下來,火代表一旦你的ID返回:

protected void Upload() //--- this is your current upload function 
{ 
    //--- other upload code 
    string ReturnedID = "1"; //--- whatever is returned 
    PassID(ReturnedID); 
} 

現在你需要在父頁面上設置監聽器。一旦偵聽器執行,它將執行「PassID」功能。在這個函數中,你將會調用關鍵字usercontrol的公共函數「LoadID」。因此,父頁上,這樣做:

protected void Page_Load(object sender, EventArgs e) 
{ 
    //--- setup the listener to receive the uploaded id 
    UploadUserControlID.PassID += new UploadUserControlID_ClassName.PassIDToParent(PassID); 
} 
protected void PassID(string id) 
{ 
    //--- this method is called from the listener above and it passes the id 
    KeywordUserControlID.LoadID(id); 
} 

併爲您的關鍵字用戶控件的代碼應該是這樣的:

public void LoadID(string id) 
{ 
    //--- do whatever you need to with the id 
} 

下面是關於使用代表的更多的一些信息:http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx

希望有幫助!祝你好運!不要忘記標記爲答案!