Obscure comma problem

    Date: 03/14/05 (Asp Dot Net)    Keywords: asp, java, web

    Here is my obscure problem. I've got a page where a some checkboxes get added dynamically by javascript like so.

    [input type="checkbox" name="extra" value="uno" /] Uno
    [input type="checkbox" name="extra" value="dos" /] Dos
    [input type="checkbox" name="extra" value="tres" /] Tres
    I can then retrive them in my aspx.cs code like so:
    string extras = HttpContext.Current.Request.Form["extras"].Split(",");
    // extras[0] == "uno"
    // extras[1] == "dos"
    // extras[2] == "tres"
    This works as expected and desired unless the javascript winds up adding checkboxes like:
    [input type="checkbox" name="extra" value="first item" /]First Item
    [input type="checkbox" name="extra" value="Second item, with a comma" /]Second item, with a comma
    [input type="checkbox" name="extra" value="third item" /]third item
    In which case we get:
    string extras = HttpContext.Current.Request.Form["extras"].Split(",");
    // extras[0] == "first item"
    // extras[1] == "second item"
    // extras[2] == "with a comma"
    // extras[3] == "third item"
    Which is not what is desired. So my question is if there is a way to properly retrieve these values using ASP.NET or do I need to rewrite the javascript code so the name attributes are different and an another hidden control tells me what the names to look for are?
    Solution is here:

    public static ICollection FormVars(System.Web.HttpRequest request, string varName) {
    	StreamReader sr = new StreamReader(request.InputStream);
    	string post = sr.ReadToEnd();
    	sr.Close();
    	ArrayList values = new ArrayList();
    
    	string[] labelValues = post.Split('&');
    	foreach(string lv in labelValues) {
    		if (lv.StartsWith(varName)) {
    			string val = lv.Substring(varName.Length + 1); // varName=
    			values.Add(System.Web.HttpUtility.UrlDecode(val));
    		}
    	}
    	return values;
    }
    
    
    
    ICollection extras = Utils.FormVars(HttpContext.Current.Request, "extra");

    Source: http://www.livejournal.com/community/aspdotnet/28923.html

« pdf to database? || Question »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home