Question

I have a html form that submits to a C# ashx handler that i'm hoping will insert/update the database

I've written this in PHP and Coldfusion, but I cannot figure out how to do this in C#

HTML form

     <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 handler

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

}

Typically i would iterate over the $_POST['children'] in php , and execute the sql

How exactly does this translate?

EDIT

ok ive almost gotten this, however my iterator goes over ALL of the request collection variables, i want it to go over only a specific named variable, in this case "children"

i.e localhost/page?operation=update&children=9&children=8&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);
}

i want it to ignore everything but the specific var

Was it helpful?

Solution

If you are doing a post. I think something like this would work.

<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"/>

The browser will send all of the values comma seperated to the server when the form is submited

Then on your server side you can do this:

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

Selected will be an array of strings for each value that was passed in by the browser. You can then loop over them and do whatever you need to.

Hope this helps some.

OTHER TIPS

I was just working on this yesterday. I ended up using a hidden field that will hold the multiple checked checkbox id's. So, if that route works for you, you could create a checkboxlist editor template or control. This could have a script such as:

(tempId will hold the common "name" attribute's value for your checkbox/checkboxlist, and we have the "somehiddenfield" hidden field to hold the selected values)

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

Then, on postback on the server-side the form collection will simply have the hidden field we wrote the checked values into. Split that in whatever way works for you (like comma separated in my example) and you're good to go. My server-side is implemented in MVC but for WebForms you can pull the elements from the Request.Form dictionary (Request.Form["somehiddenfield"].ToString()) or even Request.Params as you are using.

Right after i put out the bounty of course -_-

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


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top