Jan 31, 2012, 7:23 AM
Last edited by Daniil; Jan 31, 2012 at 7:26 AM.
<ext:ResourcePlaceHolder runat="server" Mode="ScriptFiles" />
<script type="text/javascript">
Ext.form.field.ComboBox.override({
hiddenName : "",
fieldSubTpl: [
'<div class="{hiddenDataCls}" role="presentation"></div>',
'<input id="{id}" type="{type}" ',
'<tpl if="size">size="{size}" </tpl>',
'<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
'<tpl if="name">name="{name}"</tpl>',
'class="{fieldCls} {typeCls}" autocomplete="off" />',
'<div id="{cmpId}-triggerWrap" class="{triggerWrapCls}" role="presentation">',
'{triggerEl}',
'<div class="{clearCls}" role="presentation"></div>',
'</div>',
{
compiled: true,
disableFormats: true
}
],
setHiddenValue: function(values){
var me = this,
name = me.hiddenName,
i;
if (!me.hiddenDataEl || !name) {
return;
}
values = Ext.Array.from(values);
var dom = me.hiddenDataEl.dom,
childNodes = dom.childNodes,
input = childNodes[0],
valueCount = values.length,
childrenCount = childNodes.length;
if (!input && valueCount > 0) {
me.hiddenDataEl.update(Ext.DomHelper.markup({
tag: 'input',
type: 'hidden',
name: name
}));
childrenCount = 1;
input = dom.firstChild;
}
while (childrenCount > valueCount) {
dom.removeChild(childNodes[0]);
-- childrenCount;
}
while (childrenCount < valueCount) {
dom.appendChild(input.cloneNode(true));
++ childrenCount;
}
for (i = 0; i < valueCount; i++) {
childNodes[i].value = values[i];
}
}
});
</script>
Here is the full example.<%@ Page Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
protected void GetValue(object sender, DirectEventArgs e)
{
string msg;
if (this.ComboBox1.SelectedItem != null)
{
string v = this.ComboBox1.SelectedItem.Value;
string t = this.ComboBox1.SelectedItem.Text;
msg = string.Format("Text: {0}<br/>Value: {1}", t, v);
}
else
{
msg = "The ComboBox is empty.";
}
X.Msg.Alert("ComboBox", msg).Show();
}
protected void SetItem(object sender, DirectEventArgs e)
{
this.ComboBox1.Value = "2";
//or
//this.ComboBox1.Text = "2";
//or
//this.ComboBox1.SetValue("2");
}
protected void SetText(object sender, DirectEventArgs e)
{
this.ComboBox1.Value = "Hello World!";
//or
//this.ComboBox1.Text = "Hello World!";
//or
//this.ComboBox1.SetValue("Hello World!");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.NET v2 Example</title>
<ext:ResourcePlaceHolder runat="server" Mode="ScriptFiles" />
<script type="text/javascript">
Ext.form.field.ComboBox.override({
hiddenName : "",
fieldSubTpl: [
'<div class="{hiddenDataCls}" role="presentation"></div>',
'<input id="{id}" type="{type}" ',
'<tpl if="size">size="{size}" </tpl>',
'<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
'<tpl if="name">name="{name}"</tpl>',
'class="{fieldCls} {typeCls}" autocomplete="off" />',
'<div id="{cmpId}-triggerWrap" class="{triggerWrapCls}" role="presentation">',
'{triggerEl}',
'<div class="{clearCls}" role="presentation"></div>',
'</div>',
{
compiled: true,
disableFormats: true
}
],
setHiddenValue: function(values){
var me = this,
name = me.hiddenName,
i;
if (!me.hiddenDataEl || !name) {
return;
}
values = Ext.Array.from(values);
var dom = me.hiddenDataEl.dom,
childNodes = dom.childNodes,
input = childNodes[0],
valueCount = values.length,
childrenCount = childNodes.length;
if (!input && valueCount > 0) {
me.hiddenDataEl.update(Ext.DomHelper.markup({
tag: 'input',
type: 'hidden',
name: name
}));
childrenCount = 1;
input = dom.firstChild;
}
while (childrenCount > valueCount) {
dom.removeChild(childNodes[0]);
-- childrenCount;
}
while (childrenCount < valueCount) {
dom.appendChild(input.cloneNode(true));
++ childrenCount;
}
for (i = 0; i < valueCount; i++) {
childNodes[i].value = values[i];
}
}
});
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />
<ext:ComboBox ID="ComboBox1" runat="server" LazyMode="Instance">
<Items>
<ext:ListItem Text="Item 1" Value="1" />
<ext:ListItem Text="Item 2" Value="2" />
</Items>
<SelectedItems>
<ext:ListItem Index="0" />
</SelectedItems>
</ext:ComboBox>
<ext:Button runat="server" Text="Get value" OnDirectClick="GetValue" />
<ext:Button runat="server" Text="Set item" OnDirectClick="SetItem" />
<ext:Button runat="server" Text="Set text" OnDirectClick="SetText" />
</form>
</body>
</html>