通过ScriptControl Descriptor将控件ID传递给javascript

时间:2009-11-24 09:14:08

标签: asp.net javascript ajax

我在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添加的前缀。

如何解决?

1 个答案:

答案 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 };          
}