开发者

ASP.NET doing a double post form, once to page other to url

开发者 https://www.devze.com 2023-01-21 03:53 出处:网络
Hey i am using a 3rd Party Newsletter Tool which at the end gives me the html for the form to place on my site.

Hey i am using a 3rd Party Newsletter Tool which at the end gives me the html for the form to place on my site.

Basically looks like this

<form action="http://link/subscriber/subscribe.html" method="post">
    <input name="subscribeBoxId" type="hidden" value="XXXX"/>
    <input name="subscribeBoxTitle" type="hidden" value="Subscribe Box"/>
    <input name="isExternal" type="hidden" value="true"/>
    <input name="externalPublicationId" type="hidden" value="XXXX"/>
    <b>Your details: <br /><br />
    <input name="sf_name.firstName_required" value="true" type="hidden" /><br />
...
</form>

But I Wish to add my own validation before I submit it to that form on my ASP.NET page and t开发者_JAVA百科hen submit it to that URL, how can this be achieved cheers !


The easiest way is to add a function call on the clicking of the "Submit" button, something like

OnClick="return MyValidationFunction"

If you return "true" from that function, your post will occur, if you return false it will not post.

Extra detail

Per the request in comments, below is a very crude example of something that could be done. Basically you can do any validation you want. You could also do this with ASP.NET validators if you really want to, but you will need to modify the inputs to b runat="server" for them to work the best.

function MyValidationFunction()
{
   var input1 = document.getElementById('myInput');
   if (input1.value == "")
      return false
   else
      return true
}


lots of ways to do this. the javascript way like @Mitchel Sellers is one.

you could create your own form, do normal asp.net validation on it, when you're happy, create your own http post through the WebRequest class, and fire it off the values to the target site.

yet another way would be to alter the action target on the form, first time, have it submit to your page, when you're happy with the input, alter it to submit to the target site.

as requested, example on how to use post. take the below with a pinch of salt, I cant run this to test it myself, but you should be able to get the general idea from the below


NameValueCollection nameValues = new NameValueCollection();

nameValues.Add("subscribeBoxId", "a");
nameValues.Add("subscribeBoxTitle", "b");
nameValues.Add("isExternal", "c");
nameValues.Add("externalPublicationId", "d");
nameValues.Add("sf_name.firstName_required", "e");

postValues(new Uri("http://somewhere/somePage.aspx"), nameValues);


private static string postValues(Uri target, NameValueCollection nameValues)
{
    string resultData = null;

    HttpServerUtility urlEncoder = new HttpServerUtility();

    string postData = string.Join("&", nameValues.Keys.Cast().Select(key => string.Format("{0}={1}", key, urlEncoder.UrlEncode(nameValues[key]))));


    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(target);
    httpRequest.Method = "POST";
    httpRequest.ContentLength = postData.Length;
    httpRequest.ContentType = "application/x-www-form-urlencoded";

    StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream());
    myWriter.Write(postData);
    myWriter.Close();


    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

    using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
    {
        resultData = reader.ReadToEnd();
        reader.Close();
    }

    return resultData;
}

0

精彩评论

暂无评论...
验证码 换一张
取 消