具有相同名称属性循环的多个复选框

时间:2014-01-17 21:35:03

标签: c# forms iteration ashx

我有一个html表单,提交给我希望插入/更新数据库的C#ashx处理程序

我用PHP和Coldfusion编写过这个,但我无法弄清楚如何在C#

中做到这一点

HTML表单

     <form id="copyto">
    <input type="hidden" name="operation" value="update" />
    <label><input type="checkbox" name="children[]" checked="checked" value="001">
Andrew Regan</label>

    <label><input type="checkbox" name="children[]" checked="checked" value="101">
Arthur Regan, III</label>

    <input type="checkbox" name="children[]" checked="checked" value="968">
Tim Reagan
    </form>

C#ASHX处理程序

foreach(string key in context.Request.Params["children"]) 
{
    ListDictionary updateParams = new ListDictionary();
    updateParams.Add("rowid", key);
    string sSql = @"insert into temp select * from children where c.id = :rowid";
    dbi.ExecuteNonQuerySql(sSql, updateParams);

}

通常我会迭代PHP中的$ _POST ['children']并执行sql

这究竟是如何翻译的?

编辑

好吧我几乎得到了这个,但是我的迭代器遍历所有的请求集合变量,我希望它只覆盖一个特定的命名变量,在这种情况下是“children”

即localhost / page?operation = update&amp; children = 9&amp; children = 8&amp; children = 17

foreach(string key in context.Request.QueryString) 
{
    ListDictionary updateParams = new ListDictionary();
    updateParams.Add("row_id", context.Request.QueryString[key]);
    string sSql = @"insert into dug  select :row_id from dual";
    dbi.ExecuteNonQuerySql(sSql, updateParams);
}

我希望它忽略除特定var

之外的所有内容

3 个答案:

答案 0 :(得分:5)

如果你正在发帖子。我觉得这样的事情会起作用。

<input type="checkbox" name="children" value="108"/>
<input type="checkbox" name="children" value="109"/>
<input type="checkbox" name="children" value="110"/>
<input type="checkbox" name="children" value="111"/>

当表单被提交时,浏览器将以逗号分隔的所有值发送到服务器

然后在服务器端执行此操作:

var selected = context.Request.Form["children"].Split(',');

Selected将是浏览器传入的每个值的字符串数组。然后,您可以循环遍历它们并执行您需要的任何操作。

希望这会有所帮助。

答案 1 :(得分:0)

我昨天刚刚开始工作。我最终使用了一个隐藏字段,该字段将保存多个已选中的复选框ID。因此,如果该路由适合您,您可以创建一个checkboxlist编辑器模板或控件。这可能有一个脚本,如:

(tempId将保存复选框/复选框列表的常用“name”属性值,并且我们有“somehiddenfield”隐藏字段来保存所选值)

<script>
    $(function () {
        var arrTmp = [];
        //Update array on selection change
        $('input[name="@String.Format("{0}[]", tempId)"]').change(function () {
            arrTmp = [];
            $('input:checked[name="@String.Format("{0}[]", tempId)"]').each(function () { arrTmp.push($(this).val()); });
            $('input[id="somehiddenfield"]').val(arrTmp.join(','));
        });
    });
</script>

然后,在服务器端的回发上,表单集合将只有我们将已检查值写入的隐藏字段。以任何适合你的方式拆分(就像我的例子中的逗号一样),你很高兴。我的服务器端是在MVC中实现的,但对于WebForms,您可以在使用时从Request.Form字典(Request.Form["somehiddenfield"].ToString())甚至Request.Params中提取元素。

答案 2 :(得分:0)

当我拿出赏金当然是-_-

之后
foreach (string k in context.Request.QueryString)
    {       
        if (k.StartsWith("children")){
        foreach (string v in context.Request.QueryString.GetValues(k)){
           ListDictionary updateParamss = new ListDictionary();
            updateParamss.Add("row_id",  v);

               string Sql = @"insert into dug  select :row_id from dual";
                 dbi.ExecuteNonQuerySql(Sql, updateParamss);
                 }
            }


    }
相关问题