Click here to Skip to main content
15,113,131 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (ViewState["OfferID"].ToString() == "1063" || ViewState["OfferID"].ToString() == "1717" || ViewState["OfferID"].ToString() == "922" || ViewState["OfferID"].ToString() == "3212"
                 || ViewState["OfferID"].ToString() == "1649" || ViewState["OfferID"].ToString() == "699" || ViewState["OfferID"].ToString() == "1952"
                 || ViewState["OfferID"].ToString() == "2222" || ViewState["OfferID"].ToString() == "2223" || ViewState["OfferID"].ToString() == "2279"
                 || ViewState["OfferID"].ToString() == "2340" || ViewState["OfferID"].ToString() == "2341" || ViewState["OfferID"].ToString() == "2342"
               || ViewState["OfferID"].ToString() == "3736" || ViewState["OfferID"].ToString() == "3714" || ViewState["OfferID"].ToString() == "3657" || ViewState["OfferID"].ToString() == "3319" || ViewState["OfferID"].ToString() == "3318" || ViewState["OfferID"].ToString() == "3651" || ViewState["OfferID"].ToString() == "3650" || ViewState["OfferID"].ToString() == "3649" || ViewState["OfferID"].ToString() == "3639" || ViewState["OfferID"].ToString() == "3596" || ViewState["OfferID"].ToString() == "3549" || ViewState["OfferID"].ToString() == "3548" || ViewState["OfferID"].ToString() == "3492" || ViewState["OfferID"].ToString() == "3340" || ViewState["OfferID"].ToString() == "3341" || ViewState["OfferID"].ToString() == "3274" || ViewState["OfferID"].ToString() == "3173" || ViewState["OfferID"].ToString() == "3144" || ViewState["OfferID"].ToString() == "3036" || ViewState["OfferID"].ToString() == "2727" || ViewState["OfferID"].ToString() == "2704" || ViewState["OfferID"].ToString() == "2280" || ViewState["OfferID"].ToString() == "2970" || ViewState["OfferID"].ToString() == "2473" || ViewState["OfferID"].ToString() == "2297" || ViewState["OfferID"].ToString() == "2427" || ViewState["OfferID"].ToString() == "2460")
            {
                trustedFormScript = "<script type='text/javascript'>";
                trustedFormScript = trustedFormScript + "(function() {";
                trustedFormScript = trustedFormScript + "var field = 'xxTrustedFormCertUrl';";
                trustedFormScript = trustedFormScript + "var provideReferrer = false;";
                trustedFormScript = trustedFormScript + "var tf = document.createElement('script');";
                trustedFormScript = trustedFormScript + "tf.type = 'text/javascript'; tf.async = true; ";
                trustedFormScript = trustedFormScript + "tf.src = 'http' + ('https:' == document.location.protocol ? 's' : '') + '://api.trustedform.com/trustedform.js?provide_referrer=' + escape(provideReferrer) + '&field=' + escape(field) + '&l='+new Date().getTime()+Math.random();";
                trustedFormScript = trustedFormScript + "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(tf, s); }";
                trustedFormScript = trustedFormScript + ")();function trustedFormCertUrlCallback(certificateUrl) {";
                trustedFormScript = trustedFormScript + "$.get('savetrustedform.aspx?url=' + certificateUrl + '&oid=" + ViewState["OfferID"].ToString() + "&email=" + emailad + "', function(data) { });}";
                trustedFormScript = trustedFormScript + "</script>";
            }


What I have tried:

Hi Guys, I'am given with this code of ViewState["OfferID"] hardcoded for each and every offer in 'if' block. In the code I've 4 such 'if' statement with multiple Viewstate whose offer id is hardcoded. I'am told to remove these hardcoding and make it general. I browsed for this and found one option to store these offerIds in single viewstate. Is there any other options to try with. I am new in .net development so please share some knowledge of you.
Posted
Updated 17-Nov-16 22:38pm
v2

1 solution

Use the following code to simplify your checks:

C#
string IDs = "1063,1717,922";
       string OfferID = ViewState["OfferID"].ToString();
       if (IDs.Split(',').Contains(OfferID))
       {
           trustedFormScript = "&lt;script type='text/javascript'>";
           trustedFormScript = trustedFormScript + "(function() {";
           trustedFormScript = trustedFormScript + "var field = 'xxTrustedFormCertUrl';";
           trustedFormScript = trustedFormScript + "var provideReferrer = false;";
           trustedFormScript = trustedFormScript + "var tf = document.createElement('script');";
           trustedFormScript = trustedFormScript + "tf.type = 'text/javascript'; tf.async = true; ";
           trustedFormScript = trustedFormScript + "tf.src = 'http' + ('https:' == document.location.protocol ? 's' : '') + '://api.trustedform.com/trustedform.js?provide_referrer=' + escape(provideReferrer) + '&field=' + escape(field) + '&l='+new Date().getTime()+Math.random();";
           trustedFormScript = trustedFormScript + "var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(tf, s); }";
           trustedFormScript = trustedFormScript + ")();function trustedFormCertUrlCallback(certificateUrl) {";
           trustedFormScript = trustedFormScript + "$.get('savetrustedform.aspx?url=' + certificateUrl + '&oid=" + ViewState["OfferID"].ToString() + "&email=" + emailad + "', function(data) { });}";
           trustedFormScript = trustedFormScript + "&lt;/script>";
       }
   
v2
Comments
Amith 12807185 18-Nov-16 5:17am
   
Thanks a lot Puneet. Seems so simple now.
Er. Puneet Goel 18-Nov-16 5:23am
   
Welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900