views:

127

answers:

1

I create a control in 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:

 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?

A: 

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 };          
}
Canavar
ClientID is read only.
markiz
no, not for setting, view my edited answer.
Canavar
IS it possible to get ID before the control is added to the page? (because I add controls to the page in "reverse order": child->parent->it's parent->... so this control is added to the page after I already don't have the reference to the control)
markiz