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

dynamically add attribute code behind

11 Answers 810 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
moegal
Top achievements
Rank 1
moegal asked on 11 Oct 2012, 10:46 PM
How can I add a custom data attribute to a combo box that is dynamically created and bound to a DataTable?


protected
RadComboBox AddProductsCombo(int catID, int prodID)
   {
       RadComboBox RadComboBox1 = new RadComboBox();
       RadComboBox1.ID = "RadComboBox1";
       RadComboBox1.AutoPostBack = true;
       RadComboBox1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(RadComboBox1_SelectedIndexChanged);
       PlaceHolder1.Controls.Add(RadComboBox1);
 
       MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQLConn"].ConnectionString);
       conn.Open();
       string sqlSelect1 = "SELECT p.prodID AS Id, p.prodName As Name, tsp.siteproductTypeID As Attr, p.catID  FROM products AS p INNER JOIN tblsiteproducts AS tsp ON tsp.prodID = p.prodID WHERE tsp.isActive = 1 AND tsp.siteID = 1 AND p.catID = ?catID AND isAddon = 0 ORDER BY prodSort ASC";
       MySqlDataAdapter adapter1 = new MySqlDataAdapter();
       MySqlCommand cmd1 = new MySqlCommand(sqlSelect1, conn);
       adapter1.SelectCommand = cmd1;
       cmd1.Parameters.Add("?catID", MySqlDbType.Int32).Value = catID;
 
       DataTable dt1 = new DataTable();
       adapter1.Fill(dt1);
 
       RadComboBox1.DataTextField = "Name";
       RadComboBox1.DataValueField = "Id";
       RadComboBox1.DataSource = dt1;
       RadComboBox1.DataBind();
       RadComboBox1.SelectedValue = prodID.ToString();
 
       return RadComboBox1;
   }

11 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 12 Oct 2012, 01:48 PM
Hello Marty,

Here you could find our help article, demonstrating in details how to add dynamically custom attributes from code behind.

Greetings,
Nencho
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.
0
moegal
Top achievements
Rank 1
answered on 12 Oct 2012, 02:02 PM
Nencho,

thanks for the fast reply. I started there before I went to the forum. And I searched the forum. If you could help some more that would be great.

I understand I would need to add the attribute name and then the value, but I am not getting it.
Can I add the attribute and then bind it to the data source?

RadComboBox1.DataTextField = "prodName";
RadComboBox1.DataValueField = "Id";

??
RadComboBox1.Attributes.AddAttributes = "Attr";

Thanks, Marty
0
moegal
Top achievements
Rank 1
answered on 13 Oct 2012, 12:46 AM
I am getting warmer but I could still use some help.

I can assign a value to my custom attribute like this:

RadComboBox1.DataTextField = "prodName";
     RadComboBox1.DataValueField = "Id";
     RadComboBox1.Attributes["test"] = "Attr";

But the string "Attr" is getting assigned to the custom attribute and not the value from the database.

Any help would be appreciated.
Marty
0
Accepted
Nencho
Telerik team
answered on 15 Oct 2012, 03:09 PM
Hello Marty,

With setting a fixed value to the newly added attribute, you will assign that value to each item. In order to assign the corresponding value to each item's attribute, you should loop trough the DataRows of the DataTable(dt1). Please consider the following approach so you could achieve the desired functionality.
foreach (DataRow dataRow in dt1.Rows)
      {
          RadComboBoxItem item = new RadComboBoxItem();
 
          item.Text = (string)dataRow["Name"];
          item.Value = dataRow["Id"].ToString();
 
          decimal testValue = (decimal)dataRow["Test"];
 
 
          item.Attributes.Add("Test", testValue.ToString());
          RadComboBox1.Items.Add(item);
 
          item.DataBind();
      }

Regards,
Nencho
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.
0
moegal
Top achievements
Rank 1
answered on 15 Oct 2012, 03:57 PM
Nencho,

Thanks. I suspected as much. I ending up doing a server call to each Dataset directly and reading the attribute server side, but this would be better for my client side code.

RadComboBox recepientBox = (RadComboBox)PlaceHolder1.FindControl("prodID");
        DataTable table = (DataTable)recepientBox.DataSource;
        string textfield = table.Rows[recepientBox.SelectedIndex]["Test"].ToString();

This might be a great feature to add, data binding directly to attributes!!!

Marty
0
Kalina
Telerik team
answered on 17 Oct 2012, 03:37 PM
Hi moegal,

Thank you for your suggestion.

The Attributes collection is intended to be used in various scenarios –  the programmers should be able to specify custom attributes declaratively in the RadComboBoxItem tag or programmatically using the Attributes collection of the RadComboBoxItem.  
Please find more details about Attributes here.

This is the reason why we do not have plans to change the Attributes design and logic.

All the best,
Kalina
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.
0
moegal
Top achievements
Rank 1
answered on 17 Oct 2012, 05:18 PM
thanks
0
moegal
Top achievements
Rank 1
answered on 23 Oct 2012, 02:28 AM
Nenco,

seems I am losing the attributes after a post back even if I completely re add the attributes on every pass?  Any ideas?

I have another function that calls AddQtyCombo and after the  first postback I get null on Quantity_SelectedIndexChanged on string textfield = rcb.SelectedItem.Attributes["I"].ToString();

Thanks,

Marty

public static void AddQtyCombo(int prodID, int id, long price, int sides)
        {
            RadComboBox RadComboBox1 = new RadComboBox();
            RadComboBox1.ID = "prodpriceID";
            RadComboBox1.CssClass = "cField";
            RadComboBox1.ToolTip = "Select A Quantity";
            RadComboBox1.EmptyMessage = "Select Quantity";
            RadComboBox1.AutoPostBack = true;
            RadComboBox1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(Quantity_SelectedIndexChanged);
 
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySQLConn"].ConnectionString);
            conn.Open();
            string sqlSelect1 = @"SELECT Id, Quantity, MIN(Price) AS Price, BasePrice, Weight, Turnaround, sorter FROM table";
 
            MySqlDataAdapter adapter1 = new MySqlDataAdapter();
            MySqlCommand cmd1 = new MySqlCommand(sqlSelect1, conn);
            adapter1.SelectCommand = cmd1;
            cmd1.Parameters.Add("?prodID", MySqlDbType.Int32).Value = prodID;
 
            DataTable dt1 = new DataTable();
            adapter1.Fill(dt1);
            conn.Close();
 
            int counter = 1;
            foreach (DataRow dataRow in dt1.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();
 
                int IdVal = (int)dataRow["Id"];
                item.Attributes.Add("I", IdVal.ToString());
 
                double TurnaroundVal = (double)dataRow["Turnaround"];
                item.Attributes.Add("T", TurnaroundVal.ToString());
 
                double WeightVal = (double)dataRow["Weight"];
                item.Attributes.Add("W", WeightVal.ToString());
 
                double PriceVal = (double)dataRow["Price"];
                item.Attributes.Add("P", PriceVal.ToString());
 
                double BasePriceVal = (double)dataRow["BasePrice"];
                item.Attributes.Add("B", BasePriceVal.ToString());
 
                UInt64 QuantityVal = (UInt64)dataRow["Quantity"];
                item.Attributes.Add("Q", QuantityVal.ToString());
 
                string priceString = string.Format("{0:C}", double.Parse(PriceVal.ToString()));
 
                item.Text = QuantityVal.ToString() + " (" + priceString + ")";
                item.Value = counter.ToString() + "_" + dataRow["Id"].ToString();
 
                RadComboBox1.Items.Add(item);
 
                item.DataBind();
                counter++;
            }
            ph2.Controls.Add(RadComboBox1);           
        }
        protected static void Quantity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox rcb = sender as RadComboBox;
            string textfield = rcb.SelectedItem.Attributes["I"].ToString();
            mylabel.Text = textfield;
        }
0
Nencho
Telerik team
answered on 24 Oct 2012, 02:55 PM
Hello Marty,

I have performed some tests, based on the provided snippet of code, but I was unable to replicate the experienced problem. I have prepared  a sample project for you, in order to demonstrate you the behavior at my end. If you still experience the problem, I would like to ask you to open a support ticket, along with the sample attached and modified in such a manner, so we could replicate the issue at our end.

Regards,
Nencho
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.
0
moegal
Top achievements
Rank 1
answered on 24 Oct 2012, 03:04 PM
thanks, I will check it out
0
Nencho
Telerik team
answered on 25 Oct 2012, 02:00 PM
Hi Marty,

Please take your time and let me know if you have any further questions regarding this issue.


All the best,
Nencho
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
moegal
Top achievements
Rank 1
Answers by
Nencho
Telerik team
moegal
Top achievements
Rank 1
Kalina
Telerik team
Share this question
or