[英]Passing control's ID to javascript via ScriptControlDescriptor
I create a control in CreateChildControls(): 我在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 };
}
Then in javascript i try to find this control using the ID property: 然后在javascript中,我尝试使用ID属性找到此控件:
alert($get(this.get_controlId()));
I get null because the actual control ID has a prefix that was added by asp. 我得到空值,因为实际的控件ID具有由ASP添加的前缀。
How to solve that? 怎么解决呢?
Try mycontrol.ClientID instead of mycontrol.ID for client side reference : 尝试使用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 };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.