我在CreateChildControls()中创建一个控件:
HtmlGenericControl mycontrol= HtmlGenericControl("li");
mycontrol.ID = "controlID";
controlId = mycontrol.ID;
protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Project.TEditor", this.ClientID);
descriptor.AddProperty("controlId", controlId);
return new ScriptDescriptor[] { descriptor };
}
然后在javascript中我尝试使用ID属性找到此控件:
alert($get(this.get_controlId()));
我得到null,因为实际的控件ID有一个由asp添加的前缀。
如何解决?
答案 0 :(得分:0)
尝试使用 mycontrol.ClientID 而不是 mycontrol.ID 进行客户端参考:
// At CreateChildControls :
HtmlGenericControl mycontrol= HtmlGenericControl("li");
mycontrol.ID = "controlID";
// After you add your control to page (Page.Controls.Add(myControl)) :
controlId = mycontrol.ClientID;
protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
{
ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Project.TEditor", this.ClientID);
descriptor.AddProperty("controlId", controlId);
return new ScriptDescriptor[] { descriptor };
}