Passing control's ID to javascript via ScriptControlDescriptor
I create a control in CreateChildControls():
Htm开发者_如何学编程lGenericControl 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:
alert($get(this.get_controlId()));
I get null because the actual control ID has a prefix that was added by asp.
How to solve that?
Try mycontrol.ClientID instead of mycontrol.ID for client side reference :
// 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 };
}
精彩评论