简体   繁体   中英

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.

Here's my C# code

 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

<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.

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.

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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