0
DBDataSet.tblAdsDataTable dtAds = client.getAdsDate(ref errCode, AdsType, AdsSlot, Country);
        string ss = "";
        string aa = "";
        if (dtAds.Rows.Count > 0)
        {
            foreach (DBDataSet.tblAdsRow dr in dtAds)
            {
                DateTime date = dr.AdsDate;
                string AdsDate = date.ToString("dd/MM/yyyy");

                ss += '"' + AdsDate + '"' +",";  
                aa = ss.TrimEnd(','); // equal to "01/01/2018","02/01/2018"

                string jquery = "var naArray = ["+ aa +"];";;
                ClientScript.RegisterStartupScript(typeof(Page), "a key","<script>" + jquery + "</script>");
            }
        }

Hi, from the code above, i would want to add javascript in code behind like <script> var naArray = ["01/01/2018","02/01/2018"];</script>. However, when i execute, it become <script> var naArray = ["01/01/2018"]; <script> which "02/01/2018" is missing in the array. Appreciate if anyone can help on this. Thanks.

7
  • 1
    I dont see any code that is making up the 02/01/2018 date in your sample above? You have only used AdsDate Commented Jun 12, 2018 at 4:13
  • Hi @PrateekShrivastava, i have edited my code. Commented Jun 12, 2018 at 4:17
  • aa = ss.TrimEnd(','); // equal to "01/01/2018","02/01/2018" Can you explain this code? Commented Jun 12, 2018 at 4:19
  • The date is retrieve from datatable & i want to add each date into array in the javascript Commented Jun 12, 2018 at 4:19
  • @LonelyPlaneteer, aa = ss.TrimEnd(','); if not adding this, it will become "01/01/2018","02/01/2018", mean adding a "," behind Commented Jun 12, 2018 at 4:20

2 Answers 2

1

You are setting value to jquery variable is inside the foreach loop. It should be outside of the loop. Try updating your code as below.

DBDataSet.tblAdsDataTable dtAds = client.getAdsDate(ref errCode, AdsType, AdsSlot, Country);
string ss = "";
string aa = "";
if (dtAds.Rows.Count > 0)
{
    foreach (DBDataSet.tblAdsRow dr in dtAds)
    {
        DateTime date = dr.AdsDate;
        string AdsDate = date.ToString("dd/MM/yyyy");

        ss += '"' + AdsDate + '"' +",";                  
    }
    aa = ss.TrimEnd(','); // equal to "01/01/2018","02/01/2018"
    string jquery = "var naArray = ["+ aa +"];";;
    ClientScript.RegisterStartupScript(typeof(Page), "a key","<script>" + jquery + "</script>");
}
Sign up to request clarification or add additional context in comments.

Comments

1
        string aa = "";

        foreach (DBDataSet.tblAdsRow dr in dtAds)
        {
            DateTime date = dr.AdsDate;
            string AdsDate = date.ToString("dd/MM/yyyy");

            aa += '"' + AdsDate + '"' + ","; // equal to "01/01/2018","02/01/2018"
        }

        aa = aa.TrimEnd(',');

Attach javascript outside your loop. Your value is changing in each iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.