我试图从C#多次调用JavaScript函数,但它只执行一次。从不同参数的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('” +编号+ “ ''” +名字+“”注册脚本,'+ details +'','“+ servicesOffered +”','“+ imagePath +”')“,true);' –
你会在浏览器控制台上发现任何js错误吗? –
可能的重复https://stackoverflow.com/questions/7304692/how-to-call-javascript-in-for-loop-code-behind – Kavin