C# 从ITemplate类引用页/控件
我有一个用户控件,其中我有一个RadGrid,它有动态TemplateField,因为它们是动态的,所以我使用从ITemplate继承的自定义类。代码如下:C# 从ITemplate类引用页/控件,c#,asp.net,C#,Asp.net,我有一个用户控件,其中我有一个RadGrid,它有动态TemplateField,因为它们是动态的,所以我使用从ITemplate继承的自定义类。代码如下: public class ApplicationHeaderTemplateItem : ITemplate { private string colName; public ApplicationHeaderTemplateItem(string cName) {
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)
{
}
}
正如您所看到的,我的textbox有一个TextChanged事件。我的问题是,该方法是在ApplicationHeaderTemplateItem类的上下文中创建的,因此我无法访问用户控件中的任何控件。有什么方法可以做到这一点吗?使用
sender
参数获取文本框,那么您已经在那里了,因为每个控件都有一个
那太好了,但是因为我需要得到用户控件对象,所以它不起作用。即:应用程序应用程序=(应用程序)((控制)发送方)。页面;告诉我无法将类型“System.Web.UI.Page”转换为“Project.JobControls.Applications”,因此Project.JobControls.Applications
是此文本框中的UserControl
吗?试试(Project.JobControls.Applications)txt.NamingContainer代码>是的,这就是文本框所在的位置,它包含我要调用的方法
void txt_TextChanged(object sender, EventArgs e)
{
Control txt = (Control) sender;
Page page = txt.Page;
}