gpt4 book ai didi

javascript - C# - 从代码隐藏中添加 javascript 函数

转载 作者:行者123 更新时间:2023-12-03 01:41:50 26 4
gpt4 key购买 nike

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

嗨,从上面的代码中,我想在代码后面添加 javascript,如 <script> var naArray = ["01/01/2018","02/01/2018"];</script> 。但是,当我执行时,它变成 <script> var naArray = ["01/01/2018"]; <script>数组中缺少“02/01/2018”。如果有人可以提供帮助,我们将不胜感激。谢谢。

最佳答案

您正在 foreach 循环内为 jquery 变量设置值。它应该在循环之外。尝试按如下方式更新您的代码。

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

关于javascript - C# - 从代码隐藏中添加 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com