PDA

View Full Version : عمل نكردن رويداد كليك باتن وقتي متد فرم post باشد



rana-writes
شنبه 24 تیر 1391, 10:07 صبح
سلام من يه پيج دارم براي ارسال اطلاعات توي درگاه بانك ملي
با گذاشتن runat="server" تونستم به بخشهاي html دسترسي داشته باشم توي cs .منتهي وقتي باتن ميندازم تو صفحه عملا اصلا توي رويداد كليك باتن هيچ كاري انجام نميده.
حتي مجبور شدم كدهاي ارسال رو توي PageLoad بندازم تا از اونجا مستقيم به بانك ارسال بشه
كدها رو ميذارم ببينين



<form id="pardakhtsim" runat="server" method='post' action='https://Damoon.bankmelli-iran.com/MerchantsIntegrationTestController'>
<input id="HiddenValue" type="hidden" value="Initial Value" runat="server" />
<input type='hidden' runat="server" name='x_login' id='x_login' />
<input type='hidden' runat="server" name='x_amount' id='x_amount' />
<input type='hidden' runat="server" name='x_description' id='x_description' />
<input type='hidden' runat="server" name='x_invoice_num' id='x_invoice_num' />
<input type='hidden' runat="server" name='x_fp_sequence' id='x_fp_sequence' />
<input type='hidden' runat="server" name='x_fp_timestamp' id='x_fp_timestamp' />
<input type='hidden' runat="server" name='x_fp_hash' id='x_fp_hash' />
<input type='hidden' runat="server" name='x_currency_code' id="x_currency_code" />
<input type='hidden' runat="server" name='x_test_request' id='x_test_request' />
</form>


حالا وقتي توي رويداد كليك باتن هر چيزي بنويسم، اصلا انگار نه انگار، منتقل ميشه به صفحه تاييديه بانك


string loginID = "TestMer";
string transactionKey = "tstkey";
string amount = "1000";
string description = "Sample Transaction";
string label = "Submit Payment"; // The is the label on the 'submit' button
string testMode = "true";
string currency = "Rial";

// If an amount or description were posted to this page, the defaults are overidden
if (Request.Form["amount"] != null)
{ amount = Request.Form["amount"]; }
if (Request.Form["description"] != null)
{ description = Request.Form["description"]; }

// also check to see if the amount or description were sent using the GET method
if (Request.QueryString["amount"] != null)
{ amount = Request.QueryString["amount"]; }
if (Request.QueryString["description"] != null)
{ description = Request.QueryString["description"]; }

string invoice = DateTime.Now.ToString("yyyyMMddhhmmss");

Random random = new Random();
string sequence = (random.Next(0, 1000)).ToString();

string timeStamp = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();

//x_login^x_fp_sequence^x_fp_timestamp^x_amount^x_cu rrency_code
string fingerprint = HMAC_MD5(transactionKey, loginID + "^" + sequence + "^" + timeStamp + "^" + amount + "^" + currency );

//Print the Amount and Description to the page by placing them in the Spans
amountSpan.InnerHtml = amount;
descriptionSpan.InnerHtml = description;

//Update the fields in the actual form
x_login.Value = loginID;
x_amount.Value = amount;
x_description.Value = description;
// buttonLabel.Value = label;
x_test_request.Value = testMode;
x_invoice_num.Value = invoice;
x_fp_sequence.Value = sequence;
x_fp_timestamp.Value = timeStamp;
x_fp_hash.Value = fingerprint;
x_currency_code.Value = currency;

string PostData = "x_description=Sample Transaction&x_login=TestMer70&x_fp_sequence="+ sequence +"&x_fp_timestamp="+timeStamp+"&x_amount=1000&x_cu rrency_code=Rial&x_fp_hash="+ fingerprint;


يه حركت ديگه هم رفتم كه بجاي اينكه توي html مقادير رو پر كنم توي همون cs بهشون مقدار بدم اينطوري:

System.Web.HttpContext.Current.Response.Write(stri ng.Format("<input type='hidden' name=\"x_fp_timestamp\" value=\"{0}\"/>", timeStamp));


ولي بازم فرقي نداره، توي رويداد باتن عملا هيچ كاري انجام نميده. من ميخوام توي اين باتن قبل از ارسال اطلاعات به بانك، اطلاعات پرداخت رو ذخيره كنم.

ممنون ميشم راهنماييم كنين.

alonemm
شنبه 24 تیر 1391, 13:09 عصر
باسلام:

میتونید از کنترل های ASP.net استفاده کنید و سپس در رویداد کلیک دکمه بعد از ذخیره سازی مقادیر تکس باکس ها اقدام به ارسال به سمت صفحه بانک کنید.

به مثال زیر توجه کنید:



WebRequest req = WebRequest.Create("http://www.hamghadam.com/signin");
string postData = "email="+TextBox1.text+"&password="+TextBox2.text;
byte[] send = Encoding.Default.GetBytes(postData);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;
Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();
Literal1.Text = html;



این یک روش بود که به ذهن من رسید میتونید از روش های دیگر برای ارسال پارامترها استفاده کنید.

موفق باشید.

alonemm
شنبه 24 تیر 1391, 16:18 عصر
در سناریو پست قبلیم کاربر در صفحه سایت ما می مونه و ما صفحه مورد نظر رو به صورت مجازی لود میکنیم.

اما در بعضی جاها همانند ارسال اطلاعات به صفحاتی مثل صفحه درگاه پرداخت آنلاین ما نیاز داریم بعد از ذخیره اطلاعات این داده ها رو به صورت Post به URL مربوطه بفرستیم و کاربر رو به این صفحه هدایت کنیم.

برای پیاده سازی این پروژه به شکل زیر عمل میکنیم.


یک کلاس همانند زیر بسازید:


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Collections.Specialized;
using System.Text;

/// <summary>
/// Summary description for HttpHelper
/// </summary>
public static class HttpHelper
{
/// <summary>
/// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
/// </summary>
/// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
/// <param name="data">A collection of data that will be posted to the destination Url.</param>
/// <returns>Returns a string representation of the Posting form.</returns>

private static String PreparePOSTForm(string url, NameValueCollection data)
{
//Set a name for the form
string formID = "PostForm";

//Build the form using the specified data to be posted.
StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"" + formID + "\" name=\"" + formID + "\" action=\"" + url + "\" method=\"POST\">");
foreach (string key in data)
{
strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
}
strForm.Append("</form>");

//Build the JavaScript which will do the Posting operation.
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var v" + formID + " = document." + formID + ";");
strScript.Append("v" + formID + ".submit();");
strScript.Append("</script>");

//Return the form and the script concatenated. (The order is important, Form then JavaScript)
return strForm.ToString() + strScript.ToString();
}
/// <summary>
/// POST data and Redirect to the specified url using the specified page.
/// </summary>
/// <param name="page">The page which will be the referrer page.</param>
/// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param>
/// <param name="data">The data should be posted.</param>
/// <Author>Alone mm</Author>
public static void RedirectAndPOST(Page page, string destinationUrl, NameValueCollection data)
{
//Prepare the Posting form
string strForm = PreparePOSTForm(destinationUrl, data);

//Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
page.Controls.Add(new LiteralControl(strForm));
}
}




یک شی از جنس NameValueCollection میسازیم به نام Data.
سپس این شی رو مقدار دهی میکنیم متناسب با پارامترهایی که میخوایم به صورت POST ارسال کنیم.
از متد ADD شی Data برای اضافه کردن پارامترها استفاده میکنیم پارامتر اول نام و پارامتر دوم مقدار رو وارد میکنیم.
خوب فقط کافیه که در فرممون از کلاس HttpHelper متد RedirectAndPOST رو فراخوانی کنیم.


NameValueCollection data = new NameValueCollection();
data.Add("username1", txtu.Text);
data.Add("password1", txtp.Text);
RedirectAndPOST(this.Page, "http://www.ittehran.com/LOGIN-ADMIN.php", data);




موفق باشید.