第一次按键单击时隐藏字段值为空,但第二次单击修复时有值吗? [英] Hidden field value is null on first button click but have value on second click fix?

查看:68
本文介绍了第一次按键单击时隐藏字段值为空,但第二次单击修复时有值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii有!!我一直在这种情况下,我有一个asp.net网页,其中我有隐藏字段的支付网关,如txnid和哈希值字段现在,当我点击我的提交按钮,它应该提供txnid和哈希码,但它没有提供任何东西我以前通过在asp.net中设置警报框来查看输出,但我发现在第一次点击按钮这些字段是空的,在第二次点击按钮这些字段有值,一切正常。请帮助!!!



aspx代码: -

Hii There !! i have been in this situation i have an asp.net webpage in which i have hidden fields for payment gateway like txnid and hash value fields now when i click on my submit button it supposed to provide the txnid and hash code but its not providing anything i used to see the output by having alert boxes in asp.net but what i found is on first click of button these fields were empty and on second click of button these fields have values and everything is working fine..please help !!!

aspx code:-

<form class="RegForm"  runat="server" method="post" name="Registration.aspx" id="form1" accept-charset="utf-8" >
        <div id ="frmError" runat="server">
     <span style="color:red">Please fill all mandatory fields.</span>
     <br/>
     <br/>
     </div>


     <!---important field dont remove -->
       <input type="hidden" runat="server" id="key" name="key"  />
     <input type="hidden" runat="server" id="hash" name="hash"   />
           <input type="hidden" runat="server" id="txnid" name="txnid" />
            <input type="hidden" runat="server" id="enforce_paymethod" name="enforce_paymethod" />
            <!---important field dont remove --><pre></form>



.cs代码




.cs code

public partial class Registration : System.Web.UI.Page
   {
       public string action1 = string.Empty;
       public string hash1 = string.Empty;
       public string txnid1 = string.Empty;
       public string url = string.Empty;
       static string constring = ConfigurationManager.ConnectionStrings["PalpConString"].ConnectionString;


       protected void Page_Load(object sender, EventArgs e)
       {


           try
           {

               //set merchant key from web.config or AppSettings
               key.Value = ConfigurationManager.AppSettings["MERCHANT_KEY"];
               //DisplayRadioButton();
               if (!IsPostBack)
               {


                   frmError.Visible = false; // error form

               }
               else
               {
                   //frmError.Visible = true;
               }
               if (string.IsNullOrEmpty(Request.Form["hash"]))
               {
                   btnSubmit.Visible = true;
               }
               else
               {
                   btnSubmit.Visible = false;
               }

           }
           catch (Exception ex)
           {
               Response.Write("<span style='color:red'>" + ex.Message + "</span>");

           }
       }
         // Generating Hash Code for PayuMoney
       public string Generatehash512(string text)
       {

           byte[] message = Encoding.UTF8.GetBytes(text);

           UnicodeEncoding UE = new UnicodeEncoding();
           byte[] hashValue;
           SHA512Managed hashString = new SHA512Managed();
           string hex = "";
           hashValue = hashString.ComputeHash(message);
           foreach (byte x in hashValue)
           {
               hex += String.Format("{0:x2}", x);
           }
           return hex; ;

       }

       protected void btnSubmit_Click1(object sender, EventArgs e)
       {
                   RecordInsertion();
           try
           {
                   string[] hashVarsSeq;
                   string hash_string = string.Empty;
               if (string.IsNullOrEmpty(Request.Form["txnid"])) // generating txnid
               {
                   Random rnd = new Random();
                   string strHash = Generatehash512(rnd.ToString() + DateTime.Now);
                   txnid1 = strHash.ToString().Substring(0, 20);
               }
               else
               {
                   txnid1 = Request.Form["txnid"];
               }
               if (string.IsNullOrEmpty(Request.Form["hash"])) // generating hash value
               {
               if (
                   string.IsNullOrEmpty(ConfigurationManager.AppSettings["MERCHANT_KEY"]) ||
                   string.IsNullOrEmpty(txnid1) ||
                   string.IsNullOrEmpty(Request.Form["amount"]) ||
                   string.IsNullOrEmpty(Request.Form["firstname"]) ||
                   string.IsNullOrEmpty(Request.Form["email"]) ||
                   string.IsNullOrEmpty(Request.Form["phone"]) ||
                   string.IsNullOrEmpty(Request.Form["productinfo"]) ||
                   string.IsNullOrEmpty(Request.Form["surl"]) ||
                   string.IsNullOrEmpty(Request.Form["furl"])

                   )
               {  //remove later
                   frmError.Visible = true;
                   return;
               }

               else
               {
                   //remove later
           frmError.Visible = false;
           hashVarsSeq = ConfigurationManager.AppSettings["hashSequence"].Split('|'); // spliting hash sequence from config
           hash_string = "";
           foreach (string hash_var in hashVarsSeq)
           {
               if (hash_var == "key")
               {
                   hash_string = hash_string + ConfigurationManager.AppSettings["MERCHANT_KEY"];
                   hash_string = hash_string + '|';
               }
               else if (hash_var == "txnid")
               {
                   hash_string = hash_string + txnid1;
                   hash_string = hash_string + '|';
               }
               else if (hash_var == "amount")
               {
                   hash_string = hash_string + Convert.ToDecimal(Request.Form[hash_var]).ToString("g29");
                   hash_string = hash_string + '|';
               }
               else
               {

                   hash_string = hash_string + (Request.Form[hash_var] != null ? Request.Form[hash_var] : "");// isset if else
                   hash_string = hash_string + '|';
                           }
                   }

                   hash_string += ConfigurationManager.AppSettings["SALT"];// appending SALT

                   hash1 = Generatehash512(hash_string).ToLower();         //generating hash
                   action1 = ConfigurationManager.AppSettings["PAYU_BASE_URL"] + "/_payment";// setting URL

                   }
               }

               else if (!string.IsNullOrEmpty(Request.Form["hash"]))
               {
                   hash1 = Request.Form["hash"];
                   action1 = ConfigurationManager.AppSettings["PAYU_BASE_URL"] + "/_payment";

               }

               if (!string.IsNullOrEmpty(hash1))
               {
                   hash.Value = hash1;
                   txnid.Value = txnid1;

                   System.Collections.Hashtable data = new System.Collections.Hashtable(); // adding values in gash table for data post
                   data.Add("hash", hash.Value);
                   data.Add("txnid", txnid.Value);
                   data.Add("key", key.Value);
                   string AmountForm = Convert.ToDecimal(amount.Text.Trim()).ToString("g29");// eliminating trailing zeros
                   amount.Text = AmountForm;
                   data.Add("amount", AmountForm);
                   data.Add("firstname", firstname.Text.Trim());
                   data.Add("email", email.Text.Trim());
                   data.Add("phone", phone.Text.Trim());
                   data.Add("productinfo", productinfo.Text.Trim());
                   data.Add("surl", surl.Text.Trim());
                   data.Add("furl", furl.Text.Trim());
                   data.Add("lastname", lastname.Text.Trim());
                   data.Add("curl", "");
                   data.Add("address1", address1.Text.Trim());
                   data.Add("address2", address2.Text.Trim());
                   data.Add("city", city.Text.Trim());
                   data.Add("state", state.Text.Trim());
                   data.Add("country", "");
                   data.Add("zipcode", zipcode.Text.Trim());
                   data.Add("udf1", "");
                   data.Add("udf2", "");
                   data.Add("udf3", "");
                   data.Add("udf4", "");
                   data.Add("udf5", "");
                   data.Add("pg", "");
                   data.Add("service_provider","");
                   string strForm = PreparePOSTForm(action1, data);
                   Page.Controls.Add(new LiteralControl(strForm));

               }

               else
               {
                   //no hash

               }

           }

           catch (Exception ex)
           {
                   Response.Write("<span style='color:red'>" + ex.Message + "</span>");
           }
       }

       private string PreparePOSTForm(string url, System.Collections.Hashtable data)      // post form
       {
                   //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 (System.Collections.DictionaryEntry key in data)
           {

                   strForm.Append("<input type=\"hidden\" name=\"" + key.Key +
                                  "\" value=\"" + key.Value + "\">");
           }


                   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();
       }





我尝试过:



i尝试在更新面板中包含这些字段,但它再次无法正常工作请帮助



What I have tried:

i tried to have these fields inside of update panel but its again not working please help

推荐答案

Hello Cyber​​Saint,



在发布的隐藏字段声明(aspx代码)中,这些没有设定值:



Hello CyberSaint,

In the posted Hidden fields declaration (aspx code), these have no set value:

<input type="hidden" runat="server" id="txnid" name="txnid" ¿Value="InitialValue"? />





这就是为什么当你第一次点击动作按钮时它们是空的。



你必须确保你初始化他们的尝试阅读它们之前的值。如果这些值是由于保密而无法发布的,那么你可以在你的Page_Load事件中设置它们:





That's why they are empty when you first click the action button.

You must make sure you initialize their values before you attempt to read them. If these values are something you cannot post due to secrecy, then you could set them in you Page_Load event like this:

protected void Page_Load(object sender, EventArgs e)
{

// Your code...

if (HidDv.Value == string.Empty) //Do this for each hidden field
            { 
                //Set initial value here.
            }

// Your code...

}





无论哪种方式,您点击相应按钮时隐藏的控件都会设置值。



干杯!



Either way, your hidden controls will have set values by the time you click on the corresponding button.

Cheers!


我想出问题感谢很多


这篇关于第一次按键单击时隐藏字段值为空,但第二次单击修复时有值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆