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

时间:2017-08-07 10:51:28

标签: javascript c# jquery

我试图从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中。

提前致谢。

3 个答案:

答案 0 :(得分:0)

您可以尝试这样做:您需要在调用结束时添加;

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

答案 1 :(得分:0)

我建议轻松解决此问题。我们建议使用文字进行此类操作。在添加文字元素之前,请添加到页面底部。我添加了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>";
    }

希望它有所帮助。

答案 2 :(得分:0)

从您的代码中看来,您已经将密钥参数传递为硬编码(如 text5 )并在调用函数结束时错过了分号,如@Bharatsing所说他的回答。

替换代码中的以下任何一行并尝试一下。希望它对你有所帮助!..

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

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

{{1}}