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

Change items in ComboBox after button click

2 Answers 322 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
JP
Top achievements
Rank 1
JP asked on 11 May 2012, 09:36 AM
Hi,

normally you have to populate the ComboBox in OnInit so the items are persisted after postback. But I need to populate the combobox after a button was clicked. Initially the comboBox could be empty, but after the button click I want to populate the combobox in a way that the items are persisted between postbacks.

How could I achieve this?

Thanks!

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 May 2012, 01:36 PM
Hi,

I suppose that you want to populate the RadComboBox in the onClick event of Button. The RadComboBoxItems are persisting in my side after postbacks. Here is the full code that I tried.

ASPX:
<telerik:RadComboBox ID="RadComboBox1" runat="server" >
</telerik:RadComboBox>
<asp:Button ID="Button1" runat="server" onclick="button1_onClick" />

C#:
protected void button1_onClick(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT Text,Value FROM Table", con);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        RadComboBox1.DataTextField = "Text";
        RadComboBox1.DataValueField = "Value";
        RadComboBox1.DataSource = dt;
        RadComboBox1.DataBind();
    }

Please elaborate your scenario and provide your full code if it doesn't help.

Regards,
Princy.
0
Accepted
Ivana
Telerik team
answered on 15 May 2012, 01:51 PM
Hello Jan,

Try the following:
<telerik:RadButton runat="server" ID="RadButton2"  Text="Postback"/>
<telerik:RadButton runat="server" ID="RadButton1" OnClick="RadButton1_Click" Text="Populate Combo"/>
<telerik:RadComboBox runat="server" ID="RadComboBox1">
</telerik:RadComboBox>
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Populated"] == "populated")
    {
        RadComboBox1.Items.Add(new RadComboBoxItem("111", "111"));
        RadComboBox1.Items.Add(new RadComboBoxItem("222", "222"));
        RadComboBox1.Items.Add(new RadComboBoxItem("333", "333"));
    }
}
 
protected void RadButton1_Click(object sender, EventArgs e)
{
    if (Session["Populated"] == null)
    {
        RadComboBox1.Items.Add(new RadComboBoxItem("111", "111"));
        RadComboBox1.Items.Add(new RadComboBoxItem("222", "222"));
        RadComboBox1.Items.Add(new RadComboBoxItem("333", "333"));
        Session["Populated"] = "populated";
    }
}

I hope it will help.

Kind regards,
Ivana
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
ComboBox
Asked by
JP
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ivana
Telerik team
Share this question
or