【问题标题】:How to generate Checkboxlist dynamically using ASP.NET Literal如何使用 ASP.NET Literal 动态生成 Checkboxlist
【发布时间】:2015-12-09 03:37:19
【问题描述】:

我正在尝试使用此代码动态生成复选框列表:

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;
    }

但它不能正常工作“在下图中解释”,我认为那是因为 ASP.NET 标记而不是 HTML 标记,但我不确定……所以,你能帮我解决吗这个问题?

如下图所示,asp.net 标签没有转换为 html 标签?

http://i.stack.imgur.com/ufSq3.png

【问题讨论】:

    标签: html asp.net dynamic webforms literals


    【解决方案1】:

    你做不到。但是您可以实例化一个新控件,然后将其附加到页面控件属性中:

    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);
    }
    

    或者对每个控件使用该控件的RenderControl方法,但不推荐。

    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();
    

    【讨论】:

    • 你的解决方案是完美的答案,请检查我的答案
    • 不确定这是否是一个很好的问题答案,但在这个答案中有几行有用的、有启发性的代码,我很感激。谢谢;)
    【解决方案2】:

    我也通过动态创建 div 解决了这个问题,然后将复选框列表添加到该 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 2019-01-25
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多