Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net动态向li添加检查表_C#_Asp.net - Fatal编程技术网

C# asp.net动态向li添加检查表

C# asp.net动态向li添加检查表,c#,asp.net,C#,Asp.net,我有一个divactivitydiv 我想将li添加到该div中,并在li中添加复选框 我试过这个 List<string> comps = getCompainNames(); for (int i = 0; i < comps.Count(); i++) { HtmlGenericControl newLi = new HtmlGenericControl("li");

我有一个div
activitydiv

我想将
li
添加到该div中,并在
li
中添加
复选框

我试过这个

 List<string> comps = getCompainNames();
                for (int i = 0; i < comps.Count(); i++) {
                    HtmlGenericControl newLi = new HtmlGenericControl("li");
                    newLi.InnerText = comps[i];
                    campaignDiv.Controls.Add(newLi);
                }
List comps=getCompainNames();
对于(int i=0;i
但我真的不知道如何添加复选框,你能帮我吗

我更喜欢输入型复选框而不是asp.net复选框控制器您应该使用

aspx


您应该能够在不使用任何ASP.NET复选框控件的情况下执行以下操作:

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="campaignDiv" runat="server">

    </div>
    </form>
</body>
</html>

Default.aspx.cs

string html = "<ul>";
html = html + CreateLiCheckbox("MyCheckbox1");
html = html + CreateLiCheckbox("MyCheckbox2");
html = html + CreateLiCheckbox("MyCheckbox3");
html = html + "</ul>";
campaignDiv.InnerHtml = html;

private string CreateLiCheckbox(string checkBoxText)
{
    return string.Format("<li><input value=\"{0}\" type=\"checkbox\">{0}</li>", checkBoxText);
}
string html=“
    ”; html=html+CreateLiCheckbox(“MyCheckbox1”); html=html+CreateLiCheckbox(“MyCheckbox2”); html=html+CreateLiCheckbox(“MyCheckbox3”); html=html+“
”; activitydiv.InnerHtml=html; 私有字符串CreateLiCheckbox(字符串checkBoxText) { 返回string.Format(“
  • {0}
  • ”,复选框文本); }
    在本例中,您添加了不带值的复选框,对吗?请你加上这个值好吗?该值与复选框旁边显示的测试值相同。换句话说,值应该是
    MyCheckBox1
    MyCheckBox2
    MyCheckBox3
    @MarcoDinatsoli——我更新了我的答案!
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="campaignDiv" runat="server">
    
        </div>
        </form>
    </body>
    </html>
    
    string html = "<ul>";
    html = html + CreateLiCheckbox("MyCheckbox1");
    html = html + CreateLiCheckbox("MyCheckbox2");
    html = html + CreateLiCheckbox("MyCheckbox3");
    html = html + "</ul>";
    campaignDiv.InnerHtml = html;
    
    private string CreateLiCheckbox(string checkBoxText)
    {
        return string.Format("<li><input value=\"{0}\" type=\"checkbox\">{0}</li>", checkBoxText);
    }