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

How use selectIndexChanged even of dynmically created combox.

3 Answers 96 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Manish
Top achievements
Rank 2
Manish asked on 17 Mar 2012, 01:38 PM
Hi,

I am creating Combobox dynamically and i want to use it's SelectedIndexChanged of it so please help me to do this.
                RadComboBox rdcmb = new RadComboBox();
                rdcmb.ID = "rcbContact" + i;
                rdcmb.DataSource = dt;
                rdcmb.DataTextField = "ContactType";
                rdcmb.DataValueField = "ContactTypeID";
                rdcmb.DataBind();

Now i want to use here OnselectIndexChange of this control like :

rdcmb.OnselectIndexChanged= funtion();

Thanx
Manish
                

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Mar 2012, 06:21 AM
Hello Manish,

You can set AutoPostBack property of combobox and attach event handler as shown below.
C#:
RadComboBox rdcmb = new RadComboBox();
  rdcmb.ID = "rcbContact" + i;
  rdcmb.DataSource = dt;
  rdcmb.DataTextField = "ContactType";
  rdcmb.AutoPostBack = true;
  rdcmb.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
  rdcmb.DataValueField = "ContactTypeID";
  rdcmb.DataBind();
 void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
 {//handle the event here
 }

Thanks,
Shinu.
0
Manish
Top achievements
Rank 2
answered on 19 Mar 2012, 07:49 AM
Hi  Shinu,

protected void btnAddDropDown_Click1(object sender, EventArgs e)
        {
            DataTable dt = ObjGetData.LookupContacts();
    
            for (int i = 1; i <= Convert.ToInt32(Session["rcbCounter"].ToString()); i++)
            {
                RadComboBox rdcmb = new RadComboBox();
                rdcmb.ID = "rcbContact" + i;
                rdcmb.DataSource = dt;
                rdcmb.DataTextField = "ContactType";
                rdcmb.DataValueField = "ContactTypeID";
                rdcmb.AutoPostBack = true;
                rdcmb.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
                
                rdcmb.DataBind();
                Divdropdown.Controls.Add(rdcmb);
            }
            Session["rcbCounter"] = Convert.ToInt32(Session["rcbCounter"].ToString()) + 1;
        }
        void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["Tempury"] = "tried";
        }
I have tried above code but it is not working i have put a break point on combo_SelectedIndexChanged but it is not called on dropdown change.
Please help it is too urgent.
Thanks
Manish.
0
Shinu
Top achievements
Rank 2
answered on 20 Mar 2012, 08:56 AM
Hi Manish,

I tried the similar scenario and its working for me. Please take a look into the following code.

aspx:
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <div runat="server" id="Divdropdown">
    </div>
</div>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
public SqlCommand SqlCommand = new SqlCommand();
public static bool flag = false;
protected void Page_Load(object sender, EventArgs e)
{
    Session["rcbCounter"] = 3;
    if (this.IsPostBack)
    {
        if (flag)
        {
            PersistControls();
        }
    }
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    if (!flag)
    {
        PersistControls();
    }
}
void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    // Session["Tempury"] = "tried";
}
 
private void PersistControls()
{
    string updateQuery = "SELECT * FROM [Products]";
    SqlCommand.CommandText = updateQuery;
    SqlCommand.Connection = conn;
    SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
    DataSet ds = new DataSet();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];
    for (int count = 1; count <= Convert.ToInt32(Session["rcbCounter"].ToString()); count++)
    {
        RadComboBox rdcmb = new RadComboBox();
        rdcmb.ID = "rcbContact" + count;
        rdcmb.DataSource = dt;
        rdcmb.DataTextField = "ProductName";
        rdcmb.DataValueField = "ProductID";
        rdcmb.AutoPostBack = true;
        rdcmb.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
        rdcmb.DataBind();
        Divdropdown.Controls.Add(rdcmb);
    }
    Session["rcbCounter"] = Convert.ToInt32(Session["rcbCounter"].ToString()) + 1;
    flag = true;
}

Do review and let me know if this works for you.

Regards,
-Shinu.


Tags
ComboBox
Asked by
Manish
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Manish
Top achievements
Rank 2
Share this question
or