PDA

View Full Version : [CLOSED] Default Item Selection in Combo Box



shaileshsakaria
Oct 01, 2013, 6:49 AM
I have combo box having display value and value field.

Out of N items, I would like to get selected default value based on some transaction perform in CS - code behind.

Can any one help on this?


<ext:Store ID="StoreSt" runat="server" ClientIDMode="Static">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="StateID">
<Fields>
<ext:ModelField Name="StateID" />
<ext:ModelField Name="StateName" />
</Fields>
</ext:Model>
</Model>
</ext:Store>

<ext:ComboBox ID="cmbTest" runat="server" ClientIDMode="Static" ValueField="StateID" DisplayField="StateName"
Selectable="true" MultiSelect="false" StoreID="StoreSt" EmptyText="Select State" SelectOnFocus="true">

</ext:ComboBox>

Say State Jammy is lying somewhere, when page gets load, it should be default selected,

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!X.IsAjaxRequest)
{
AssignMaxLength();

StoreStateMst.DataSource = EasyPayManager.StateMst.ToList();
StoreStateMst.DataBind();
StoreSt.DataSource = EasyPayManager.StateMst.ToList();
StoreSt.DataBind();
/* Logic to get state selected */
string state = "Jammu";
int stateID = 5;
cmbTest.SetValue(state);
// alos have tried with below
//cmbTest.SetValue(stateID);
// cmbTest.SetValueAndFireSelect("Jammu");

}
}
catch (Exception ex)
{
throw;
}
}

Please help us out on this,.

Thank You.

Regards,

Daniil
Oct 01, 2013, 8:22 AM
Hi @shaileshsakaria,

Please edit the post wrapping the code in [CODE] tags. This is mentioned in these guidelines.
Forum Guidelines For Posting New Topics (http://forums.ext.net/showthread.php?3440)
More Information Required (http://forums.ext.net/showthread.php?10205)

shaileshsakaria
Oct 01, 2013, 10:21 AM
Hi,

Here is the code



<ext:Store ID="StoreSt" runat="server" ClientIDMode="Static">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="StateID">
<Fields>
<ext:ModelField Name="StateID" />
<ext:ModelField Name="StateName" />
</Fields>
</ext:Model>
</Model>
</ext:Store>

<ext:ComboBox ID="cmbTest" runat="server" ClientIDMode="Static" ValueField="StateID" DisplayField="StateName"
Selectable="true" MultiSelect="false" StoreID="StoreSt" EmptyText="Select State" SelectOnFocus="true">

</ext:ComboBox>

Say State Jammy is lying somewhere, when page gets load, it should be default selected,

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!X.IsAjaxRequest)
{
AssignMaxLength();

StoreStateMst.DataSource = EasyPayManager.StateMst.ToList();
StoreStateMst.DataBind();
StoreSt.DataSource = EasyPayManager.StateMst.ToList();
StoreSt.DataBind();
/* Logic to get state selected */
string state = "Jammu";
int stateID = 5;
cmbTest.SetValue(state);
// alos have tried with below
//cmbTest.SetValue(stateID);
// cmbTest.SetValueAndFireSelect("Jammu");

}
}
catch (Exception ex)
{
throw;
}
}


Thanks

tMp
Oct 01, 2013, 10:43 AM
Maybe related to this: http://forums.ext.net/showthread.php?20264-CLOSED-ComboBox-default-selected-value

I had the same problem at first. Here is my thread (http://forums.ext.net/showthread.php?25478-ComboBox-setValue-on-page-load-results-in-identical-value-amp-text-instead-of-a-value-text-pair).

shaileshsakaria
Oct 01, 2013, 3:19 PM
Hi tMp,

Thanks for your reply.

I have tried with following code


cbCountries.SelectedItems.Add(new Ext.Net.ListItem { Value = "4", Mode = ParameterMode.Raw });

Its working , but problem is combobox became multiselect automatically after applying this, eventhough I didn't set anywhere multiselect property.

Do you have solution of this?

Thanks

Baidaly
Oct 01, 2013, 10:11 PM
Hello!

Couldn't reproduce your issue. I set default value and my combobox remained single-selectable.


<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
private object TestData
{
get
{
return new object[]
{
new object[] { "AL", "Alabama", "The Heart of Dixie" },
new object[] { "AK", "Alaska", "The Land of the Midnight Sun" },
new object[] { "AZ", "Arizona", "The Grand Canyon State" },
new object[] { "AR", "Arkansas", "The Natural State" },
new object[] { "CA", "California", "The Golden State" },
new object[] { "CO", "Colorado", "The Mountain State" },
new object[] { "CT", "Connecticut", "The Constitution State" },
new object[] { "DE", "Delaware", "The First State" },
new object[] { "DC", "District of Columbia", "The Nation's Capital" },
new object[] { "FL", "Florida", "The Sunshine State" },
new object[] { "GA", "Georgia", "The Peach State" },
new object[] { "HI", "Hawaii", "The Aloha State" },
new object[] { "ID", "Idaho", "Famous Potatoes" },
new object[] { "IL", "Illinois", "The Prairie State" },
new object[] { "IN", "Indiana", "The Hospitality State" },
new object[] { "IA", "Iowa", "The Corn State" },
new object[] { "KS", "Kansas", "The Sunflower State" }
};
}
}

protected void Page_Load(object sender, EventArgs e)
{
Combobox1.SelectedItems.Add(new Ext.Net.ListItem("AL"));
}
</script>

<!DOCTYPE html>

<html>
<head runat="server">

</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

<ext:ComboBox
runat="server"
ValueField="abbr"
DisplayField="name"
Width="320"
ID="Combobox1"
LabelWidth="130"
QueryMode="Local">
<Store>
<ext:Store runat="server" Data="<%# TestData %>" AutoDataBind="true">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="abbr" />
<ext:ModelField Name="name" />
<ext:ModelField Name="slogan" />
</Fields>
</ext:Model>
</Model>

<Reader>
<ext:ArrayReader />
</Reader>
</ext:Store>
</Store>
</ext:ComboBox>
</form>
</body>
</html>

shaileshsakaria
Oct 11, 2013, 10:44 AM
Hi

Can you try with this code. In this code combobox becomes multiselect automatically.



<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
private object TestData
{
get
{
return new object[]
{
new object[] { 1, "Alabama", "The Heart of Dixie" },
new object[] { 2, "Alaska", "The Land of the Midnight Sun" },
new object[] { 3, "Arizona", "The Grand Canyon State" },
//new object[] { "AR", "Arkansas", "The Natural State" },
//new object[] { "CA", "California", "The Golden State" },
//new object[] { "CO", "Colorado", "The Mountain State" },
//new object[] { "CT", "Connecticut", "The Constitution State" },
//new object[] { "DE", "Delaware", "The First State" },
//new object[] { "DC", "District of Columbia", "The Nation's Capital" },
//new object[] { "FL", "Florida", "The Sunshine State" },
//new object[] { "GA", "Georgia", "The Peach State" },
//new object[] { "HI", "Hawaii", "The Aloha State" },
//new object[] { "ID", "Idaho", "Famous Potatoes" },
//new object[] { "IL", "Illinois", "The Prairie State" },
//new object[] { "IN", "Indiana", "The Hospitality State" },
//new object[] { "IA", "Iowa", "The Corn State" },
//new object[] { "KS", "Kansas", "The Sunflower State" }
};
}
}

protected void Page_Load(object sender, EventArgs e)
{

Bind();
if(ddlCombo.SelectedItem.Text == null)
ddlCombo.SelectedItems.Add(new Ext.Net.ListItem("Alabama", 1));
}

private void Bind()
{
List<Data1> datalist = new List<Data1>();
datalist.Add(new Data1 { id = 1, Name = "Alabama" });
datalist.Add(new Data1 { id = 2, Name = "Alaska" });
datalist.Add(new Data1 { id = 3, Name = "Arizona" });
this.matchCombo.DataSource = datalist.ToList();
this.matchCombo.DataBind();
}
public class Data1
{
public int id { get; set; }
public string Name { get; set; }
}
</script>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />

<ext:Container ID="container11" runat="server">
<Items>
<ext:ComboBox
ID="ddlCombo"
runat="server"
FieldLabel="Select name"
DisplayField="Name"
ValueField="matchId"
EmptyText="Select a Name"
Width="300"
AnchorHorizontal="90%"
SelectOnFocus="true">
<Store>
<ext:Store ID="matchCombo" runat="server">
<Model>
<ext:Model ID="Model2" runat="server">
<Fields>
<ext:ModelField Name="id" Mapping="id" Type="Int" />
<ext:ModelField Name="Name" Mapping="Name" Type="String" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>

</ext:ComboBox>
</Items>
</ext:Container>
</form>
</body>
</html>

Daniil
Oct 11, 2013, 11:21 AM
Thank you for the test case.

Please use the following Page_Load.

protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Bind();
ddlCombo.SelectedItems.Add(new Ext.Net.ListItem("Alabama", 1));
}
}
or

protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Bind();
ddlCombo.SelectedItem.Value = "1";
ddlCombo.SelectedItem.Mode = ParameterMode.Raw;
}
}