2013-08-29 41 views
0

我有一個用戶控件,其中我有一個RadGrid具有動態TemplateField的 - 因爲它們是動態的,我使用自己的自定義類繼承自ITemplate。代碼如下:從ITemplate類引用頁面/控件

public class ApplicationHeaderTemplateItem : ITemplate 
    { 
     private string colName; 
     public ApplicationHeaderTemplateItem(string cName) 
     { 
      colName = cName; 
     } 

     public void InstantiateIn(Control container) 
     { 

      HtmlTable tb = new HtmlTable(); 
     HtmlTableRow headerRow = new HtmlTableRow(); 


     //Create the textbox to display the percentage 
     HtmlTableCell percentCell = new HtmlTableCell(); 
     RadNumericTextBox txt = new RadNumericTextBox(); 
     txt.ID = "txt" + colName; 
     txt.DataBinding += txt_DataBinding; 
     txt.AutoPostBack = true; 
     txt.TextChanged += txt_TextChanged; 
     percentCell.Controls.Add(txt); 
     headerRow.Cells.Add(percentCell); 

     --snip-- 
     } 

     void txt_TextChanged(object sender, EventArgs e) 
     { 

     } 

    } 

正如你所看到的,我有一個TextChanged事件爲我的文本框。我的問題是該方法是在ApplicationHeaderTemplateItem類的上下文中創建的,所以我無法訪問我的用戶控件中的任何控件。有什麼辦法可以做到這一點?

回答

0

使用sender參數來獲得您的TextBox,那麼您已經在那裏,因爲每個控件都有一個Page property

void txt_TextChanged(object sender, EventArgs e) 
{ 
    Control txt = (Control) sender; 
    Page page = txt.Page; 
} 
+0

這將是偉大的,但因爲我需要獲得用戶控制對象,它不起作用。 I.e如下: 應用程序app =(Applications)((Control)sender).Page; 給我 不能鍵入「System.Web.UI.Page」轉換爲「Project.JobControls.Applications」 – Chris

+0

所以'Project.JobControls.Applications'是'UserControl'這'TextBox'坐在?嘗試'(Project.JobControls.Applications)txt.NamingContainer;' –

+0

是的,這就是文本框所在的地方,幷包含我想調用的方法 – Chris