RAD Combobox html escape

5 posts, 0 answers
  1. Asif
    Asif avatar
    3 posts
    Member since:
    Nov 2010

    Posted 21 Dec 2010 Link to this post

    Hi,
    I am using RAD ComboBox and I am trying to create and bind html tag for example

    dr[

    "categoryName"] = "<span style='padding-left:" + lvl * 10 + "px'>" + child_drs[i]["categoryName"].ToString() + "</span>";

     

    Please find the attached image to check how browser is rendering it. However in firebug it is rendering "<" to "&lt;"

    Please help!!
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Dec 2010 Link to this post

    Hello Asif,

    I suppose you want to set some styles for RadComboBox Items from code behind. Here I am sharing similar code that I tried in my application.

    aspx:
    <telerik:RadComboBox runat="server" ID="RadComboBox4">
         <ItemTemplate>             
            <%# Eval("Name") %>
         </ItemTemplate>
    </telerik:RadComboBox>

    C#:
      protected void Page_Load(object sender, EventArgs e)
         {
            RadComboBox4.DataSource = CreateDataSource();
            RadComboBox4.DataTextField = "Name";
            RadComboBox4.DataValueField = "ID";
            RadComboBox4.DataBind();
         }
    protected DataTable CreateDataSource()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("ID", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
            DataRow dr = dataTable.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "name1";
            dataTable.Rows.Add(dr);
            DataRow dr2 = dataTable.NewRow();
            dr2["ID"] = "2";
            dr2["Name"] = "<b>name1</b>";
            dataTable.Rows.Add(dr2);
            DataRow dr3 = dataTable.NewRow();
            dr3["ID"] = "3";
            dr3["Name"] = "<span style='padding-left:20px'>name3</span>";
            dataTable.Rows.Add(dr3);
            return dataTable;
        }

    Please go through the above code and check whether you missed anything. Please feel to share the comments.

    Shinu.
  3. Asif
    Asif avatar
    3 posts
    Member since:
    Nov 2010

    Posted 22 Dec 2010 Link to this post

    Hi,
    Thanks for the quick response. Please have a look at my code and tell me what exactly thing I am missing. In my code I am trying to achieve If category has sub category then I am pushing sub category a little bit to achieve a tree structure.

    c#
    DataTable dt = new DataTable();
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
        {
           dt.Columns.Add("id", typeof(string));
            dt.Columns.Add("categoryName", typeof(string));

            RadComboBoxItem radComboBoxItem = new RadComboBoxItem("", "0");
            radComboBoxItem.Text = "No Category";
            radComboBoxItem.Value = "0";
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sitefinity"].ToString());
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand("sp_FAQCat", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(ds);
                adapter.Dispose();
                DataRow[] drs = ds.Tables[0].Select("parentCategoryID is NULL", "categoryName");

                for (int i = 0; i < drs.Length; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = drs[i]["ID"].ToString();
                    dr["categoryName"] = drs[i]["categoryName"].ToString();

                    dt.Rows.Add(dr);

                    DataRow[] child_drs = ds.Tables[0].Select("parentCategoryID='" + drs[i]["id"] + "'", "categoryName");
                    if (child_drs.Length > 0)
                        recuresive(drs[i]["ID"].ToString(), 1);
                }

                selectFAQsCat.DataSource = dt;
                selectFAQsCat.DataTextField = "CategoryName";
                selectFAQsCat.DataValueField = "ID";

                selectFAQsCat.DataBind();
                selectFAQsCat.Items.Insert(0, radComboBoxItem);
    }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                //throw;
            }
            finally
            {
                conn.Close();
            }
    }

    protected void recuresive(string parentID, int lvl)
        {
            DataRow[] child_drs = ds.Tables[0].Select("parentCategoryID='" + parentID + "'", "categoryName");
            for (int i = 0; i < child_drs.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = child_drs[i]["ID"].ToString();
                dr["categoryName"] = "<span style='padding-left:" + lvl * 10 + "px'>" + child_drs[i]["categoryName"].ToString() + "</span>";
                dt.Rows.Add(dr);

                DataRow[] granchild_drs = ds.Tables[0].Select("parentCategoryID='" + child_drs[i]["id"] + "'", "categoryName");
                if (granchild_drs.Length > 0)
                    recuresive(child_drs[i]["ID"].ToString(), lvl + 1);
            }

        }


    aspx:

    <telerik:radcombobox runat="server" id="selectFAQsCat">
    </telerik:radcombobox>
  4. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 22 Dec 2010 Link to this post

    Hi Asif,

    There is a bug in ASP.NET which does not HTML encode the '<' character when it is in the value of an attribute of an element (<input> in this case). We will be researching ways of working around it in the future.

    As an alternative please use ItemTemplates.

    I hope this helps.

    All the best,
    Simon
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  5. Asif
    Asif avatar
    3 posts
    Member since:
    Nov 2010

    Posted 22 Dec 2010 Link to this post

    Hi, thanks for the help. But I am using same issue if I use ItemTemplate with the same c# code

    <telerik:radcombobox runat="server" id="selectFAQsCat">
                                                    <ItemTemplate></ItemTemplate>
                                                    </telerik:radcombobox>
Back to Top