Post a form from a code behind page

lippert used Ask the Experts™
This is either stupidly simple and I'm exactly that or it can't be done!
I'm working in VS2008 with ASP.NET 3.5 in a VB code behind page. Let's say I'm running some VB code and decide I just want to post the form. Right then, right there just post the form. I'm stuck in VB for this exercise, so would appreciate any code references be done in VB.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ashley BryantSenior Software Engineer

Do you mean you want to send a POST to a form that's on a different page/site?  Assuming you're wanting to post something to another form you would do it like this.  Example taken from this site and translated to VB.

Private Sub OnPostInfoClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strId As String = UserId_TextBox.Text
        Dim strName As String = Name_TextBox.Text
        Dim encoding As ASCIIEncoding = New ASCIIEncoding
        Dim postData As String = ("userid=" + strId)
        postData = (postData + ("&username=" + strName))
        Dim data() As Byte = encoding.GetBytes(postData)
        ' Prepare web request...
        Dim myRequest As HttpWebRequest = CType(WebRequest.Create("http://localhost/MyIdentity/Default.aspx"),HttpWebRequest)
        myRequest.Method = "POST"
        myRequest.ContentType = "application/x-www-form-urlencoded"
        myRequest.ContentLength = data.Length
        Dim newStream As Stream = myRequest.GetRequestStream
        ' Send the data.
        newStream.Write(data, 0, data.Length)
    End Sub

Open in new window



No, I want to post the form I'm in. Say have a two page web site, Page1.aspx and Page2.aspx. Page1.aspx has one form, form_1.  Now I have code running on the code behind page of Page1.aspx (Page1.aspx.vb). In the middle of that VB code on the code behind page I need to POST a vaule from form_1 to Page2.aspx. How do I do that.

For example:
--- code snippet from a code behind page (Page1.aspx.vb)
Do Until x = 20
    if x = 3 then
      <Here's where I want to issue some command to SUBMIT form_1>
     <in javaScript it would be  <script language=javascript> form_1.submit() </script> >
     End If

Sounds like passing a variable between 2 forms...

If I'm right, then:
1. Use a query string - Response.Redirect(form2.aspx?a=1)
2. Use session - session["a"]=1; response.redirect(form2.aspx), and in the form load of. Form2, get the variable from session

-there are other means but these are the easiest and most common



Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial