Regex
Regex

Reputation: 13

How to generate Checkboxlist dynamically using ASP.NET Literal

I'm trying to generate checkboxlist dynamically using this code:

string IDToEdit = Request.QueryString["id"].ToString();

        List<DAL.EF.Dental_Price_Lists> lstOfCategDental = BAL.Helpers.Prices.GetDentalCategories();
        string HTMLTag = "";
        string HTMLTag2 = "";
        int count = 1;
        foreach (var x in lstOfCategDental)
        {
            HTMLTag += string.Format("<a href='#tab{0}'>{1}</a>", count, x.Category);
            List<DAL.EF.Dental_Price_Lists> priceList = BAL.Helpers.Prices.GetDentalPrices(x.Category);

            HTMLTag2 += string.Format("<div id='tab{0}' class='tab'>", count);
            HTMLTag2 += string.Format("    <asp:CheckBoxList ID='chkListDental{0}' runat='server'>", count);

            foreach (var price in priceList)
            {
                HTMLTag2 += string.Format("          <asp:ListItem Value='{0}' price='{1}' Text='{2}'></asp:ListItem>", price.ID.ToString(), price.Price.ToString(), price.Type);
            } 

            HTMLTag2 += "                  </asp:CheckBoxList>";
            HTMLTag2 += "              </div>";

            count++;
        }

        ltrCategories.Text = HTMLTag;
        ltrChkListDental.Text = HTMLTag2;
    }

but it didn't work correctly "explained in the below image", i think thats because the ASP.NET tag instead of the HTML tag but i'm not sure of that ... so, can you help me solve this issue?

As you see in the following image the asp.net tags didn't converted to html tags?

https://i.sstatic.net/ufSq3.png

Upvotes: 0

Views: 1496

Answers (2)

Regex
Regex

Reputation: 13

I solved it by creating the div dynamically too then add the checkboxlist to that div.

string IDToEdit = Request.QueryString["id"].ToString();

        List<DAL.EF.Dental_Price_Lists> lstOfCategDental = BAL.Helpers.Prices.GetDentalCategories();
        string HTMLTag = "";
        int count = 1;

        foreach (var x in lstOfCategDental)
        {
            var checkboxList = new CheckBoxList();
            List<DAL.EF.Dental_Price_Lists> priceList = BAL.Helpers.Prices.GetDentalPrices(x.Category);

            HTMLTag += string.Format("<a href='#tab{0}'>{1}</a>", count, x.Category);
            checkboxList.ID = string.Format("chkListDental{0}", count);

            HtmlGenericControl divControl = new HtmlGenericControl("div");

            // Set the properties of the new HtmlGenericControl control.
            divControl.ID = "tab" + count;
            divControl.Attributes.Add("class", "tab"); 

            // Add the new HtmlGenericControl to the Controls collection of the
            // PlaceHolder control. 
            divPlaceHolder.Controls.Add(divControl);

            foreach (var price in priceList)
            {
                var listItem = new ListItem(price.Type);

                listItem.Value = price.ID.ToString();
                checkboxList.Attributes.Add("price", price.Price.ToString());
                checkboxList.Items.Add(listItem);
            }

            count++;
            divControl.Controls.Add(checkboxList);

        }

        ltrCategories.Text = HTMLTag;

Upvotes: 0

Sagi
Sagi

Reputation: 9294

You can not do it. But you can instantiate a new control and then append it the the page controls property:

foreach (var x in lstOfCategDental) {
    List<DAL.EF.Dental_Price_Lists> priceList = BAL.Helpers.Prices.GetDentalPrices(x.Category);
    var checkboxList = new CheckBoxList();
    checkboxList.ID = string.Format("chkListDental{0}", count);

    foreach (var price in priceList) {
        var listItem = new ListItem(price.Type);

        listItem.Value =  price.ID.ToString();
        checkboxList.Attributes.Add("price", price.Price.ToString());
        checkboxList.Items.Add(listItem);
    }

    count++;
    Page.Controls.Add(checkboxList);
}

or use the RenderControl method of the control for each of the controls, but it is not recommended.

StringBuilder sb = new StringBuilder();
StringWriter stWriter = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stWriter);
ControlToRender.RenderControl(htmlWriter);
.
.
.

foreach (var x in lstOfCategDental) {
    List<DAL.EF.Dental_Price_Lists> priceList = BAL.Helpers.Prices.GetDentalPrices(x.Category);
    var checkboxList = new CheckBoxList();
    checkboxList.ID = string.Format("chkListDental{0}", count);

    foreach (var price in priceList) {
        var listItem = new ListItem(price.Type);

        listItem.Value =  price.ID.ToString();
        checkboxList.Attributes.Add("price", price.Price.ToString());
        checkboxList.Items.Add(listItem);
    }

    count++;
    checkboxList.RenderControl(htmlWriter);
}

ltrChkListDental.Text = sb.ToString();

Upvotes: 1

Related Questions