This is a migrated thread and some comments may be shown as answers.

[Solved] Combobox GetScriptDescriptors and GetScriptReferences

7 Answers 205 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
lemon
Top achievements
Rank 1
lemon asked on 01 Dec 2009, 08:41 AM
Hello,

I created a customer control. Based on RadCombobox.

For this control I have to used your workaround http://www.telerik.com/help/aspnet-ajax/back-button-combo-selected-value.html

I add a HiddenControl to my customer class.

Additional I added a js script with GetScriptDescriptors and GetScriptReferences
to implement the js workaround logic.

But it doesn't work.

I add the js on this way:

protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()   
 {  
   var desc = base.GetScriptDescriptors().Single() as ScriptControlDescriptor;   
   desc.Type = "MyCombobox";   
   desc.AddProperty("HiddenFieldId", this.hiddenValueField.ClientID);  
  //Note: If I use here AddElementProperty the property in js will be null  
   yield return desc;   
 }  
 
protected override IEnumerable<ScriptReference> GetScriptReferences()   
{  
   return base.GetScriptReferences().Concat(this.GetMyScriptReferences());   
}  
 
MyCombobox.js  
...   
   
OnLoad:   
function() {   
 this.add_selectedIndexChanged(this._selectItemHandler);   
 var savedValue = $get(this._hiddenFieldId).value;   
 if (savedValue != "" && this.findItemByValue(savedValue)) {   
 this.findItemByValue(savedValue).select();   
}  
 
},  
...  
 

 

 


In OnLoad this._hiddenFieldId has the correct id, but $get or $find returns NULL.

What is wrong? Have I implement the methods GetScriptDescriptors() and GetScriptReferences() on the right way?

Thanks for support

lemon

 

7 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 01 Dec 2009, 03:55 PM
Hello lemon,

Please make sure that the HiddenField is actually rendered on the page.

You can most easily verify this by inspecting the rendered source of the page and searching for an input type hidden with the same id as comboBox._hiddenFieldId.

Kind regards,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
lemon
Top achievements
Rank 1
answered on 01 Dec 2009, 04:20 PM
Hello,

thanks for replay.

I verify it and the hiddenfieldid is given. so it seems that the hiddenfield will be rendered correct.

best regards
lemon
0
Simon
Telerik team
answered on 01 Dec 2009, 04:58 PM
Hi lemon,

Did you found in the page rendered HTML an input type hidden with the same ID as the _hiddedFieldId field of the RadComboBox? 

Greetings,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
lemon
Top achievements
Rank 1
answered on 01 Dec 2009, 06:49 PM
Hello,

yes, I found it

0
lemon
Top achievements
Rank 1
answered on 02 Dec 2009, 08:33 AM
Hi,

I have create a testproject.
Here the code:

using System;  
using System.Collections.Generic;  
using System.Web;  
using Telerik.Web.UI;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
 
using System.Linq;  
 
 
namespace MyComboboxProject  
{  
    [ToolboxData("<{0}:ComboboxWithWorkaround runat=\"server\"> </{0}:ComboboxWithWorkaround>")]  
    public class ComboboxWithWorkaround: RadComboBox  
    {  
        HiddenField hiddenValueField;  
        public ComboboxWithWorkaround()  
            : base()  
        {  
              
        }  
 
        protected override void OnInit(EventArgs e)  
        {  
            base.OnInit(e);  
            hiddenValueField = new HiddenField();  
            this.Controls.Add(hiddenValueField);  
 
        }  
 
        private IEnumerable<ScriptReference> GetMyScriptReferences()  
        {  
            ScriptReference reference = new ScriptReference();  
            reference.Assembly = "MyComboboxProject";  
            reference.Name = "MyComboboxProject.ComboboxWithWorkaround.js";  
            return new ScriptReference[] { reference };  
        }  
 
        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()  
        {  
            ScriptControlDescriptor desc = base.GetScriptDescriptors().Single() as ScriptControlDescriptor;  
            desc.Type = "MyComboboxProject.ComboboxWithWorkaround";  
            desc.AddProperty("HiddenValueField", this.hiddenValueField.ClientID);  
 
            yield return desc;  
        }  
 
        protected override IEnumerable<ScriptReference> GetScriptReferences()  
        {  
            return base.GetScriptReferences().Concat(this.GetMyScriptReferences());  
        }  
 
    }  
}  
 

the js

/// <reference name="ScriptReferences/jquery-1.3.2-vsdoc.js"/>  
Type.registerNamespace("MyComboboxProject");  
 
MyComboboxProject.ComboboxWithWorkaround = function(element) {  
MyComboboxProject.ComboboxWithWorkaround.initializeBase(this, [element]);  
    this._hiddenValueField = null;  
};  
 
MyComboboxProject.ComboboxWithWorkaround.prototype =  
{  
    initialize: function() {  
        this._events = new Sys.EventHandlerList();  
        MyComboboxProject.ComboboxWithWorkaround.callBaseMethod(this, 'initialize');  
 
        this._loadHandler = Function.createDelegate(this, this.OnLoad);  
        Sys.Application.add_load(this._loadHandler);  
 
        this._selectItemHandler = Function.createDelegate(this, this.OnSelectedItemChangedTest);  
    },  
 
    OnLoad: function() {  
    debugger  
        this.add_selectedIndexChanged(this._selectItemHandler);  
        var savedValue = $get(this._hiddenValueField).value;//note $get and $find is null  
        if (savedValue != "" && this.findItemByValue(savedValue)) {  
            this.findItemByValue(savedValue).select();  
        }  
    },  
 
    OnSelectedItemChangedTest: function(sender, e) {  
        $get(this._hiddenValueField).value = e.get_item().get_value();  
    },  
      
    get_HiddenValueField: function() {  
    return this._hiddenValueField;  
    },  
 
    set_HiddenValueField: function(value) {  
    this._hiddenValueField = value;  
    }  
}  
 
MyComboboxProject.ComboboxWithWorkaround.registerClass('MyComboboxProject.ComboboxWithWorkaround', Telerik.Web.UI.RadComboBox);  
 
if (typeof (Sys) != 'undefined') Sys.Application.notifyScriptLoaded();  
 
Sys.Application.notifyScriptLoaded();  
 

now my customer combobox control, which based on the ComboboWithWorkaround-Control
using System;  
using System.Collections.Generic;  
using System.Web;  
using Telerik.Web.UI;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
 
using System.Linq;  
 
 
namespace MyComboboxProject  
{  
    [ToolboxData("<{0}:MyCustomerCombobox runat=\"server\"> </{0}:MyCustomerCombobox>")]  
    public class MyCustomerCombobox : ComboboxWithWorkaround  
    {  
        public MyCustomerCombobox()  
            : base()  
        {  
 
        }  
 
         
 
        private IEnumerable<ScriptReference> GetMyScriptReferences()  
        {  
            ScriptReference reference = new ScriptReference();  
            reference.Assembly = "MyComboboxProject";  
            reference.Name = "MyComboboxProject.MyCustomerCombobox.js";  
            return new ScriptReference[] { reference };  
        }  
 
        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors()  
        {  
            ScriptControlDescriptor desc = base.GetScriptDescriptors().Single() as ScriptControlDescriptor;  
            desc.Type = "MyComboboxProject.MyCustomerCombobox";  
            yield return desc;  
        }  
 
        protected override IEnumerable<ScriptReference> GetScriptReferences()  
        {  
            return base.GetScriptReferences().Concat(this.GetMyScriptReferences());  
        }  
 
    }  
}  
 
and the js
/// <reference name="ScriptReferences/jquery-1.3.2-vsdoc.js"/>  
Type.registerNamespace("MyComboboxProject");  
 
MyComboboxProject.MyCustomerCombobox = function(element) {  
    MyComboboxProject.MyCustomerCombobox.initializeBase(this, [element]);  
    this._hiddenValueField = null;  
};  
 
MyComboboxProject.MyCustomerCombobox.prototype =  
{  
    initialize: function() {  
    //todo  
}  
    //todo  
 
      
}  
 
MyComboboxProject.MyCustomerCombobox.registerClass('MyComboboxProject.MyCustomerCombobox', MyComboboxProject.ComboboxWithWorkaround);  
 
if (typeof (Sys) != 'undefined') Sys.Application.notifyScriptLoaded();  
 
Sys.Application.notifyScriptLoaded();  
 

add these lines to your AssemblyInfo.cs

[assembly: WebResource("MyComboboxProject.ComboboxWithWorkaround.js", "application/x-javascript")]  
[assembly: WebResource("MyComboboxProject.MyCustomerCombobox.js", "application/x-javascript")] 

best regards
lemon
0
lemon
Top achievements
Rank 1
answered on 03 Dec 2009, 02:06 PM
Hello,

no help, no support for these problem?

regards
lemon
0
Accepted
Simon
Telerik team
answered on 08 Dec 2009, 05:01 PM
Hello lemon,

Thank you for providing the code.

RadComboBox renders controls added in the Controls collections of its Items only. If you add a control in RadComboBox's Control collection it will not be rendered.

This causes the JavaScript error you are receiving as the client-side script cannot find the hidden field element.

You need to explicitly render the HiddenField control. This you could most conveniently do by overriding the RenderEndTag method of RadComboBox as shown below:

public override void RenderEndTag(HtmlTextWriter writer)
{
    writer.AddAttribute(HtmlTextWriterAttribute.Id, hiddenValueField.ClientID);
        writer.AddAttribute(HtmlTextWriterAttribute.Name, hiddenValueField.ClientID);
 
        writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
 
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();
 
    base.RenderEndTag(writer);
}

The rest of your code you can leave as it is now.

Best wishes,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
lemon
Top achievements
Rank 1
Answers by
Simon
Telerik team
lemon
Top achievements
Rank 1
Share this question
or