Subscribe

RSS Feed (xml)

Submitting Data to Another Page Using ASP.NET

Submit the data directly from the client using a form post with the action attribute
specifying the target Web page.The ASPX page is as follows:
<form method=”post” action=”0104asp.asp”>
Price: <Input type=”text” name=”Price” value=”22.50” /><br>
Quantity: <Input type=”text” name=”Quantity” value=”4” /><br>

<Input type=”submit” value=”Submit Entire Form” />
</form>
Submit the data through the query string using a redirect on the server.You do this after
the client has posted back to the server with the data to be appended to the query
string.
The ASPX page is as follows:
<form runat=”server”>
Price: <asp:TextBox Runat=”server” ID=”Price” Text=”22.50” /><br>
Quantity: <asp:TextBox Runat=”server” ID=”Quantity” Text=”4” /><br>
<asp:Button Runat=”server” ID=”SubmitButton”
Text=”Postback and Redirect” OnClick=”SubmitButton_Click”/><br>
<asp:Button Runat=”server” ID=”WebRequestButton”
Text=”Submit by Web Request” OnClick=”WebRequestButton_Click”/>
<asp:Label Runat=”server” ID=”WebResponseLabel”/>
</form>
In <script runat=”server” /> block or codebehind:
Private Sub SubmitButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Response.Redirect(“0104asp.asp?Price=” & Price.Text & _
“&Quantity=” & Quantity.Text)
End Sub


You create a Web request to post to the target Web page from within the postback and
then display this information to the client, all within the same client-server round trip. In
<script runat=”server” /> block or codebehind:
Private Sub WebRequestButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim RequestUrl As String =
Request.Url.GetLeftPart(System.UriPartial.Authority) & _
Request.ApplicationPath & “/0104asp.asp”
Dim Post As String = “Price=” & Price.Text & _
“&Quantity=” & Quantity.Text
Dim Writer As StreamWriter = Nothing
Dim WebRequestObject As HttpWebRequest
Dim sr As StreamReader
Dim WebResponseObject As HttpWebResponse
Try
WebRequestObject = CType(WebRequest.Create(RequestUrl), HttpWebRequest)
WebRequestObject.Method = “POST”
WebRequestObject.ContentType = “application/x-www-form-urlencoded”

WebRequestObject.ContentLength = Post.Length
Writer = New StreamWriter(WebRequestObject.GetRequestStream())
Writer.Write(Post)
Writer.Close()
WebResponseObject = CType(WebRequestObject.GetResponse(),
HttpWebResponse)
sr = New StreamReader(WebResponseObject.GetResponseStream)
WebResponseLabel.Text = sr.ReadToEnd
Finally
Try
sr.Close()
Catch
End Try
Try
WebResponseObject.Close()
WebRequestObject.Abort()
Catch
End Try
End Try
End Sub

There are three ways to submit data to another Web page:
1. Form post from the client
2. Postback to the server followed by redirect
3. Postback to the server and create an HttpWebRequestThe first method involves a form post from the client.The form has the action attribute
set to the target Web page.When the client clicks the Submit button, the entire form
is posted to the target Web page directly from the client.The disadvantage to this
method is that the data cannot be manipulated on the server before being posted to the
target page.

The second method involves posting back to the server. During the postback event,
the selected data is validated and then attached to the query string of the target Web
page.The target Web page then extracts the data from the query string.The disadvantage
of this method is that the data is visible in the query string and has size restrictions.
The third method involves creating an HttpWebRequest to submit the data to the target
Web page and then receive the response immediately.This all occurs within the single
postback, thus eliminating the redirect. The response from the target Web page can then be parsed and displayed to the
client.

No comments:

Variety in the Web World