繁体   English   中英

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

[英]calling javascript function multiple times from C# with different parameters

我正在尝试从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中仍只会附加第一条记录。

提前致谢。

您可以尝试以下操作:您需要添加; 在调用功能结束时。

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++;
                }

我建议您轻松解决此问题。我们建议对此类操作使用文字。 在添加文字元素之前,请添加到页面底部。 我添加了asp.net文字和javascript示例。

Default.aspx

<body>
<form id="form1" runat="server">
    <div>

    </div>

</form>
 <asp:Literal ID="ltrMessage" runat="server"></asp:Literal>

Default.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
    {
        ltrMessage.Text="<script>alert('OK')</script>";
    }

希望能帮助到你。

从您的代码中看来,您已经将参数传递为硬编码(如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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM