[英]calling javascript function multiple times from C# with different parameters
I am trying to call javascript function multiple times from C# but its executing only once. 我正在尝试从C#多次调用javascript函数,但其执行一次。
Here's my C# code 这是我的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 Function 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>
I have tried it with different script keys as well. 我也尝试过使用不同的脚本键。 but still only first record is appended in the div.
但div中仍只会附加第一条记录。
Thanks in advance. 提前致谢。
You can try this: You need to add ;
您可以尝试以下操作:您需要添加
;
at end of calling finction. 在调用功能结束时。
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++;
}
I'm suggest easy resolve this problem.We recommend using literal for such operations. 我建议您轻松解决此问题。我们建议对此类操作使用文字。 Before add literal element, add to bottom of page.
在添加文字元素之前,请添加到页面底部。 I added asp.net literal and javascript sample.
我添加了asp.net文字和javascript示例。
Default.aspx Default.aspx
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
<asp:Literal ID="ltrMessage" runat="server"></asp:Literal>
Default.aspx.cs Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ltrMessage.Text="<script>alert('OK')</script>";
}
Hope it helps. 希望能帮助到你。
From your code, It seems, you have passed the key param as hard coded (as text5 ) and missed the semicolon at end of calling function like @Bharatsing said his answer. 从您的代码中看来,您已经将键参数传递为硬编码(如text5 ),并在调用函数结束时错过了分号,例如@Bharatsing说了他的答案。
Replace the following any of the line in your code and try it. 替换代码中的以下任何行,然后尝试。 Hope it helps you!..
希望对您有帮助!
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text"+ i, "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "');", true);
OR 要么
Note: For this id should be unique for all the rows then only it will work correctly. 注意:因为此ID对于所有行都应该是唯一的,所以只有它才能正常工作。
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text"+ id, "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "');", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.