Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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#_C# - Fatal编程技术网

自动生成的复选框-检查是否选中C#

自动生成的复选框-检查是否选中C#,c#,C#,我需要获取仅选中的复选框的值和ID index.aspx <asp:Button ID="Button1" runat="server" Text="Download Files" OnClick="LoadFiles" /> <asp:Literal runat="server" ID="SubCategory" /> 在这一点上我有点困惑。我已经根据您的要求创建了一个示例页面。我希望它能帮助您 标记: 服务器端: using System; using Syst

我需要获取仅选中的复选框的值和ID

index.aspx

<asp:Button ID="Button1" runat="server" Text="Download Files" OnClick="LoadFiles" />
<asp:Literal runat="server" ID="SubCategory" />

在这一点上我有点困惑。

我已经根据您的要求创建了一个示例页面。我希望它能帮助您 标记:


服务器端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class SOA : System.Web.UI.Page {
    private List<String> GetNames() {
        return Enumerable.Range(1, 10).Select(x => string.Format("Checkbox_{0}", x)).ToList();
    }
    protected void Page_Load(object sender, EventArgs e) {
        CreateCheckboxes();
        if(IsPostBack)
            ReadCheckboxes();
    }

    private void ReadCheckboxes() {
        var selectedName = GetNames().Where(x => Request.Form[x] == "checked");
    }

    private void CreateCheckboxes() {
        foreach(var name in GetNames()) {
            var checkbox = new HtmlInputCheckBox();
            checkbox.ID = name;
            checkbox.Value = "checked";
            checkbox.Name = name;
            checkboxesHolder.Controls.Add(checkbox);
            checkboxesHolder.Controls.Add(new LiteralControl(name));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
公共部分类SOA:System.Web.UI.Page{
私有列表GetNames(){
返回Enumerable.Range(1,10)。选择(x=>string.Format(“复选框{0}”,x)).ToList();
}
受保护的无效页面加载(对象发送方、事件参数e){
创建复选框();
如果(iPostBack)
readcheckbox();
}
私有void读复选框(){
var selectedName=GetNames()。其中(x=>Request.Form[x]==“选中”);
}
私有void createCheckBox(){
foreach(GetNames()中的变量名){
var checkbox=新的HtmlInputCheckBox();
checkbox.ID=名称;
checkbox.Value=“checked”;
复选框。名称=名称;
选中boxesholder.Controls.Add(复选框);
checkboxesHolder.Controls.Add(newliteralcontrol(name));
}
}
}

您是否正在回发时生成复选框?很抱歉,我没有听清楚。我需要检查一下。很抱歉我以前没做过。lol。如果你有任何这样的例子,那么我可以试一试。@akorenchikov在他的回答中展示了这一点。如果选中了复选框,那么它将发送复选框的值,因此你可以在回发期间检查Request.Form.Keys中是否显示了必要的键。我看不到这一点。我正在通过字符串生成器创建一个复选框,然后将其放入主页上的ASP:LITERAL中。。。。
string values = "";

foreach (ListItem objItem in chkTopics.Items)
{
    if (objItem.Selected)
    {
        values += objItem.Value + ":";
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SOA.aspx.cs" Inherits="SOA" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="checkboxesHolder" runat="server">
        </div>
        <input type="submit" value="Submit" />
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class SOA : System.Web.UI.Page {
    private List<String> GetNames() {
        return Enumerable.Range(1, 10).Select(x => string.Format("Checkbox_{0}", x)).ToList();
    }
    protected void Page_Load(object sender, EventArgs e) {
        CreateCheckboxes();
        if(IsPostBack)
            ReadCheckboxes();
    }

    private void ReadCheckboxes() {
        var selectedName = GetNames().Where(x => Request.Form[x] == "checked");
    }

    private void CreateCheckboxes() {
        foreach(var name in GetNames()) {
            var checkbox = new HtmlInputCheckBox();
            checkbox.ID = name;
            checkbox.Value = "checked";
            checkbox.Name = name;
            checkboxesHolder.Controls.Add(checkbox);
            checkboxesHolder.Controls.Add(new LiteralControl(name));
        }
    }
}