1

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.

6
  • You can pass only 4 argument to register script like this ScriptManager.RegisterStartupScript(Page.GetType(), "text5", "populateDecor('" + id + "','" + name + "','" + details + "','" + servicesOffered + "', '" + imagePath + "')", true); Commented Aug 7, 2017 at 10:59
  • are you getting any js errors on browser console? Commented Aug 7, 2017 at 10:59
  • 1
    possible duplicate of stackoverflow.com/questions/7304692/… Commented Aug 7, 2017 at 11:01
  • @AmrElgarhy no i am not getting any error. The code is working fine but its only working once. The div is only appended by the arguments passed first. Commented Aug 7, 2017 at 11:21
  • @Kavin I have seen that post and I had already tried solution suggested there it wasn't working as i mentioned changing script keys not working either. Commented Aug 7, 2017 at 11:26

3 Answers 3

0

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @kavin. It helped me.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.