gpt4 book ai didi

javascript - 使用不同的参数从 C# 多次调用 javascript 函数

转载 作者:行者123 更新时间:2023-11-28 04:19:34 25 4
gpt4 key购买 nike

我尝试从 C# 多次调用 javascript 函数,但它只执行一次。

这是我的 C# 代码

 DataTable table = new DataTable();
string data = " * ";
string fromTable = " Decoration ";

table = SqlHelper.LoadMyTableData(data, fromTable);

int i = 0;
foreach (DataRow row in table.Rows)
{
string id = row["DecrationID"].ToString();
string name = row["DecorationName"].ToString();
string details = row["DecorationDetails"].ToString();
string servicesOffered = row["ServicesOffered"].ToString();
string imagePath = row["DecorationImagePath"].ToString();

//ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "Func('" + id + "')", true);
string scriptKey = "text" + id;
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text5", "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "')", true);
i++;
}

JavaScript 函数

<script type="text/javascript" >
function populateDecor(id, name, details, servicesOffered, imagePath) {

alert(name);

var text = "<div class=\"row ae__dec-details__item\">";
text += " <div class=\"col-md-10\">";
text += " <div class=\"media\">";
text += "<div class=\"media-left\"> <a href=\"#\"> <img src=\"" + imagePath + "\" width=\"200\" alt=\"...\" class=\"media-object\" /></a> </div>";
text += "<div class=\"media-body\">";
text += "<h3 class=\"media-heading\">" + name + "</h3>";

text += "<label>" + servicesOffered + "</label>";
text += "<p>" + details + "</p>";
text += "</div> </div> </div>";
text += "<div class=\"col-md-2 ae__dec-details__ca\">";
text += "<a href=\"EventSketch-decorEdit.aspx?dp=" + id + "\" style=\"color:cornflowerblue; background-color:white; margin-bottom:5px\" class=\"ae__btn\">Edit</a>";
text += "<a href=\"EventSketch-decor-confirmPackage.aspx?dp=" + id + "\" class=\"ae__btn\">Select</a>";
text += "</div> </div>";

$(".col-md-12").append(text);

}
</script>

我也尝试过使用不同的脚本键。但仍然只有第一条记录附加在 div 中。

提前致谢。

最佳答案

从您的代码来看,您似乎已将 key 参数作为硬编码传递(如 text5),并且错过了调用函数末尾的分号,如 @Bharatsing 所说他的回答。

替换代码中的以下任意行并尝试。希望对您有帮助!...

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text"+ i, "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "');", true);

注意:对于所有行来说,此 id 应该是唯一的,只有这样它才能正常工作。

ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text"+ id, "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "');", true);

关于javascript - 使用不同的参数从 C# 多次调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545143/

25 4 0
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com