How use selectIndexChanged even of dynmically created combox.

4 posts, 0 answers
  1. Manish
    Manish avatar
    64 posts
    Member since:
    Jul 2011

    Posted 17 Mar 2012 Link to this post

    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
                    
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Mar 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Manish
    Manish avatar
    64 posts
    Member since:
    Jul 2011

    Posted 19 Mar 2012 Link to this post

    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.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Mar 2012 Link to this post

    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.


Back to Top