Steven
Steven

Reputation: 13769

Send POST To Different Website (ASP.NET 3.5)

What is the VB.NET (or C#) code to prepare POST data and send to a different website than specified in the current <form ...> section?

EDIT: Every algorithm I've found online using HttpWebRequest gets the returning page from the other site and outputs it with Response.Write. I want to navigate to the new page posting the data and leaving the current page completely.

EDIT2: (more specific description of what I want) The code below displays a line chart. When the user clicks on the label for a data point, the page refreshes (performs a postback) and displaying the x-value of the clicked point.

Instead, upon clicking a label, I want to send other_id="#VALX" as the only POST data to other_results.aspx as if the user had typed the corresponding id into the the TextBox on other_search.aspx and clicked Submit.

How can I do that?

<%@ Page Language="VB" AutoEventWireup="true" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<%@ Import Namespace="System.Data.OracleClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Chart1_Click(sender As Object, e As System.Web.UI.WebControls.ImageMapEventArgs)
        Response.Write(e.PostBackValue)
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>My Title</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Chart ID="Chart1" runat="server" OnClick="Chart1_Click"
            DataSourceID="myDataSource">
            <Series>
                <asp:Series Name="Series1" ChartType="Line"
                    IsValueShownAsLabel="true" XValueMember="id"
                    YValueMembers="value" PostBackValue="#VALX" />
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1" />
            </ChartAreas>
        </asp:Chart>
        <asp:SqlDataSource ID="myDataSource" runat="server"
            ConnectionString="<%$ ConnectionStrings:myConnStr %>"
            ProviderName="<%$ ConnectionStrings:myConnStr.ProviderName %>"
            SelectCommand="SELECT id, value FROM myTable"
            DataSourceMode="DataReader"/>
    </div>
    </form>
</body>
</html>

Upvotes: 0

Views: 2608

Answers (2)

David
David

Reputation: 73594

<form action="someotherwebsite/someotherform">
.....
</form>

In other words, just create a standard html form. Don't include the 'runat="server"' in the Form tag, and it will be treated as a standard html form. You CAN have more than one Form tag in an apsx page. You can only have one with runat="Server" set, but you can have other forms on your page, so long as they are not nested.

Edit

However, if you need to do some server-side processing of your own and you want to send the post data in addtition to the work YOU need to do server-side, then you can call the other site's form using the WebRequest class (as @Chris Pebble said, when he beat me to the second part of my intended answer.)

Upvotes: 1

Chris Van Opstal
Chris Van Opstal

Reputation: 37587

If you need to send a POST request from your C#/VB.NET page to another website you want to take a look at the HttpWebRequest Class.

Upvotes: 3

Related Questions