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

selected value do not set on multicolumn combobox

3 Answers 148 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
atif iqbal
Top achievements
Rank 1
atif iqbal asked on 05 Mar 2010, 09:52 AM
I make a multi column combo box. if we type in that combobox then only matched values will return. So far so good but when i want to select some specific element on button click event by setting selectedvalue property then it does not show that value in the combo box. my html and c# code is given below:

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadComboBox ID="RadComboBox1" runat="server" Skin="Vista" Height="190px"  
        Width="420px" MarkFirstMatch="true" EnableLoadOnDemand="true"
        HighlightTemplatedItems="true" OnClientItemsRequested="UpdateItemCountField"
        OnDataBound="RadComboBox1_DataBound" OnItemDataBound="RadComboBox1_ItemDataBound"
        OnItemsRequested="RadComboBox1_ItemsRequested"
        ontextchanged="RadComboBox1_TextChanged" OnClientKeyPressing="HandkeKeyPress" >
         <HeaderTemplate>
            <ul>
                <li class="col1">Name</li>
                <li class="col2">Number</li>
                <%--<li class="col3">Title</li>--%>
            </ul>
        </HeaderTemplate>
        <ItemTemplate>
            <ul>
                <li class="col1">
                    <%# DataBinder.Eval(Container.DataItem, "Name") %></li>
                <li class="col2">
                    <%# DataBinder.Eval(Container.DataItem, "Number") %></li>
                <%--<li class="col3">
                    <%# DataBinder.Eval(Container.DataItem, "ContactTitle") %></li>--%>
            </ul>
        </ItemTemplate>
        <FooterTemplate>
            A total of
            <asp:Literal runat="server" ID="RadComboItemsCount" />
            items
        </FooterTemplate>
    </telerik:RadComboBox>

and c# code is:

 private DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        DoDataBind();
    }   

    public void DoDataBind()
    {
        DataTable dt = CreateData();       
        RadComboBox1.DataSource = dt.DefaultView;
        RadComboBox1.DataBind();

    }
    protected void RadComboBox1_DataBound(object sender, EventArgs e)
    {
    
        ((Literal)RadComboBox1.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(RadComboBox1.Items.Count);
    }
    private DataTable CreateData()
    {
        table = new DataTable();
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Number", typeof(int));
        table.Rows.Add(new object[] { "one", "1" });
        table.Rows.Add(new object[] { "two", "2" });
        table.Rows.Add(new object[] { "three", "3" });
        table.Rows.Add(new object[] { "four", "4" });
        table.Rows.Add(new object[] { "five", "5" });
        table.Rows.Add(new object[] { "six", "6" });
        return table;
    }
    protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Number", typeof(int));
        DataRow[] foundRows;
        foundRows = table.Select("Name LIKE '" + e.Text + "%'");
        foreach (DataRow dr in foundRows)
        {

            dt.ImportRow(dr);
        }
        RadComboBox1.DataSource = dt;
        RadComboBox1.DataBind();
    }

    protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        e.Item.Text = ((DataRowView)e.Item.DataItem)["Name"].ToString() + " (" +((DataRowView)e.Item.DataItem)["Number"].ToString() +")";
        e.Item.Value = ((DataRowView)e.Item.DataItem)["Number"].ToString();
    }
    protected void RadComboBox1_TextChanged(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadComboBox1.SelectedValue = 2.ToString();
    }

3 Answers, 1 is accepted

Sort by
0
robertw102
Top achievements
Rank 1
answered on 05 Mar 2010, 08:06 PM
Maybe you should enclose your DoDataBind() function in an if(!Page.IsPostBack) statement to prevent the value that was set in the button click from being lost during a postback. I'm guessing that's the reason it's not showing.

Tell me if that works.
0
atif iqbal
Top achievements
Rank 1
answered on 08 Mar 2010, 11:29 AM
No, this is not the reason. I found it myself just also set the text of combo box in the button click event as given below:

RadComboBox1.SelectedValue = 2.ToString();

RadComboBox1.Text = RadComboBox1.SelectedItem.Text;

0
Simon
Telerik team
answered on 09 Mar 2010, 06:09 PM
Hello atif iqbal,

Load On Demand Items are not persisted across postbacks. So when you type something in the RadComboBox and load new Items on postback they will not be available. What is more in your case, you are binding the RadComboBox on each page load, so on postback it will have other Items loaded in it in the Button Click event handler.

Please provide more details about your requirement so that we provide you with a more specific answer.

All the best,
Simon
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
ComboBox
Asked by
atif iqbal
Top achievements
Rank 1
Answers by
robertw102
Top achievements
Rank 1
atif iqbal
Top achievements
Rank 1
Simon
Telerik team
Share this question
or