Handling Arrays of HTML Input Elements with Request.Form Like PHP

前端 未结 3 1997
温柔的废话
温柔的废话 2021-01-12 03:28

How can I properly receive these Array of Inputs on asp.net?




        
3条回答
  •  半阙折子戏
    2021-01-12 04:10

    You can use a standard string split - this is all php does for you behind the scenes. PHP is not a strongly typed language however which means that it is a LOT easier for them to provide the "appearance" of this functionality.

    The reality is that php is just allowing you to provide comma delimited input and it automatically splits it for you.

    So... if you want to do this in asp.net, you can use the same input name a number of times and it will be returned with the request object as a comma delimited list. I would not recommend using this approach for user entered input, but it will work fine if you are contrrolling the input like combining a list into a hidden input from a jquery script.

    To get your head around what is happening (with php too.. all web dev techs are using the same http rules), just try posting a form with an input (don't set runat server) that is there twice.

    
    
    

    On pageload add this

    if(IsPostBack)
    {
       Response.Write(Request["MyTest"]);
    }
    

    You should see

    MyVal1,MyVal2

    On the screen.

    Now, if you want this into an array, you can:

    string[] myvals = Request["MyTest"].Split(',');
    

    if you want Integers or other datatypes (php doesn't know/care what a datatype is really), you will have to loop through it and parse it into another array/generic list.

    I don't know what your wanting as an end result, but understanding what the browser posts back is the first step and the short answer is...

    Yes, ASP.NET can do this to (just a little more manually).

提交回复
热议问题