locked
HOWTO create ASP.NET C# controls on server side for posted data that had no runat=server on the client. RRS feed

  • Question

  • User-2022643801 posted

    HOWTO create ASP.NET C# controls on server side for posted data that had no runat=server on the client.

    Please help,

    I have processes needing to post files to a ASP.NET application. These processes must post without runat=server as such:

    <form ~~~~ to some ASP.NET/C#/aspx page>
      <input type=file name=MY_FILE ~~~>
    </form>

    On the ASP.NET page, in the Page_Load I would code somethign like this:

            byte[] zbyteLIST_FILE = new byte[fileLIST_FILE.PostedFile.ContentLength];
            fileLIST_FILE.PostedFile.InputStream.Read
            (
              zbyteLIST_FILE, 0, fileLIST_FILE.PostedFile.ContentLength
            );
            ASCIIEncoding ASCIIEncoding_Temp = new ASCIIEncoding();
            string csFILE_DATA = ASCIIEncoding_Temp.GetString(zbyteLIST_FILE);

    Here is the trick, the control "fileLIST_FILE" does not exist because the HTML does not have runat=server on it.

    If the client did have runat=server the designer.cs would have somethign liek this:

            System.Web.UI.HtmlControls.HtmlInputFile fileLIST_FILE;

    With all that said, I want to make "fileLIST_FILE" on the fly inside Page_Load fromt he posted name of "MY_FILE".

    Please do not focus on the fact that this is a file upload. I would like to know how to do this for any posted FORM data.
     

    Thursday, February 4, 2010 2:40 PM

Answers

  • User472117617 posted

    Not even with a type cast.  Unless HttpFileInput derives from string.  Nope, not then either - that's a sealed class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2010 12:42 PM

All replies

  • User472117617 posted
            if(Request["formInputElementName"] != null)
                Response.Write(Request["formInputElementName"]);


     

    Thursday, February 4, 2010 2:46 PM
  • User-2022643801 posted

    Thanks for the reply, but that did not help.

     

    I want to make a HttpContol derrived class be "bound" to the Form's data. I gave the HtmlInputFile example, as it seemed simple.

     

    I n Page_Load I want to do somethign like this:

    Page_Load()

    {

      HtmlInputFile HtmlInputFile_Temp = new HtmlInputFile();

    // Do something so HtmlInputFile_Temp is bound to Request["MY_FILE"], but there is no HTML with runat=server for "MY_FILE".

     

    }

     

    Thursday, February 4, 2010 3:12 PM
  • User-16411453 posted

    So you want to upload a webform, and then display it in a preview?

    Sunday, February 7, 2010 4:30 PM
  • User472117617 posted

    Why can't you use the ASP.Net FileUploadControl?   

    Sunday, February 7, 2010 5:16 PM
  • User-2022643801 posted

    So you want to upload a webform, and then display it in a preview?

    No.

    Monday, February 8, 2010 9:16 AM
  • User-2022643801 posted

    Why can't you use the ASP.Net FileUploadControl?   

     

    I only choose the INPUT=FILE as an example.

     

    OK, here is how ASP.NET works.

     

    1) You make and .ASPX file with a RUNAT=SERVER for some field as such:

     

    ~~~~ html html html ~~~~

    <form ~~~~>

       <textarea id=txtaMY_CONTROL runat=server>~~~</textarea>

    ~~~~`

    </form>

     

    2) ASP.NET/Visual Studio makes a ASPX.designer.cs file with the control:

    ~~~~

    /// <summary>
            /// txtaMY_CONTROL control.
            /// </summary>
            /// <remarks>
            /// Auto-generated field.
            /// To modify move field declaration from designer file to code-behind file.
            /// </remarks>
            protected global::System.Web.UI.HtmlControls.HtmlTextArea txtaMY_CONTROL;

     3) In the ASPX.cs file one can now use the data member "txtaMY_CONTROL" in the code's Page_Load method like this:

    ~~~ Page_Load(~~~)

    {

    ~~~~ txtMY_CONTROL.InnerText = "Hello!!!"

     

    ~~~}

     

    ===================================================

     

    That all said. I want to remove the runat=server and instead use this format:

    <textarea name=txtaMY_CONTROL>~~~</textarea>

    Please do not ask why.

    This approach would of course mean the ASPX.designer.cs would go away.

    Then I want to some how in the Page_Load still be able to cast a data member called txtMY_CONTROL.

     

    The "Request["txtMY_CONTROL"] is nice, but I do not have a variable of the type I previously had with runat=server.

     

     

     

    Monday, February 8, 2010 9:33 AM
  • User472117617 posted

    Well, if you want to have a server control you use runat="server"  If you don't want server control you use Request["inputControlName"].  No other option. 

    Monday, February 8, 2010 10:29 AM
  • User-2022643801 posted

    Well, if you want to have a server control you use runat="server"  If you don't want server control you use Request["inputControlName"].  No other option. 

     

     

    Not even with a type cast?

     

    HttpFileInput HttpFileInput_MyVar = (HttpFileInput) Request["fileMyTemp"];

    ?

    Monday, February 8, 2010 11:48 AM
  • User472117617 posted

    Not even with a type cast.  Unless HttpFileInput derives from string.  Nope, not then either - that's a sealed class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2010 12:42 PM
  • User-16411453 posted

    Thanks for schooling me on how asp.net works. I'll file that in my lesson book.


    Tuesday, February 9, 2010 10:03 PM
  • User-2022643801 posted

    Thank you for the reply. Well, that explains things.  

    Wednesday, February 10, 2010 9:00 AM