2017-08-07 95 views
1

我試圖從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中。

在此先感謝。

+0

可以傳遞只有4個參數是這樣'ScriptManager.RegisterStartupScript(Page.GetType(), 「text5」, 「populateDecor('」 +編號+ 「 ''」 +名字+「」註冊腳本,'+ details +'','「+ servicesOffered +」','「+ imagePath +」')「,true);' –

+0

你會在瀏覽器控制檯上發現任何js錯誤嗎? –

+1

可能的重複https://stackoverflow.com/questions/7304692/how-to-call-javascript-in-for-loop-code-behind – Kavin

回答

0

你可以試試這個:你需要在調用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++; 
       } 
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>"; 
    } 

希望它能幫助。

0

從您的代碼,看來,你已經通過了關鍵 PARAM爲硬編碼(如text5),錯過了分號調用函數一樣@Bharatsing說,他的回答結束。

在代碼中替換下面的任何行並嘗試。希望它可以幫助你..

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

OR

注:對於這個ID應該是所有行唯一那麼只有它才能正常工作。

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