【问题标题】:calling javascript function multiple times from C# with different parameters从 C# 使用不同的参数多次调用 javascript 函数
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 谢谢@kavin。它帮助了我。
【解决方案2】:

你可以试试这个:你需要在调用函数的末尾添加;

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

【讨论】:

    【解决方案3】:

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

    默认.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>";
        }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2014-02-05
      • 2020-05-07
      相关资源
      最近更新 更多