【发布时间】:2017-08-07 10:51:27
【问题描述】:
我试图从 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 中。
提前致谢。
【问题讨论】:
-
你只能传递 4 个参数来注册这样的脚本
ScriptManager.RegisterStartupScript(Page.GetType(), "text5", "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "')", true); -
浏览器控制台是否出现 js 错误?
-
@AmrElgarhy 不,我没有收到任何错误。该代码工作正常,但它只工作一次。 div 仅由首先传递的参数附加。
-
@Kavin 我已经看过那篇帖子,并且我已经尝试过解决方案,建议它不起作用,因为我提到更改脚本键也不起作用。
标签: javascript c# jquery