interkey
interkey

Reputation: 355

C# - Call the same javascript function inside a loop

Good day!

I am trying to achieve to change the color in the calendar cell of EXT Calendar for each holiday (holiday dates are from the database). But it only changes the first holiday which is the first line in the database.

My client side code :

        var applyCss = function (param1) {
        var css = "#CalendarPanel1-month-day-" + param1 + " {background-color: pink;}";

        Ext.net.ResourceMgr.registerCssClass("someCssClassId", css);
    };

My server side code:

DataTable holiday = Attendance.getInstance().getHolidays();

            for (var i = 0; i < holiday.Rows.Count; i++)
            {
                var hd = holiday.Rows[i]["holiday_date"].ToString();
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript", "applyCss(" + hd + ")", true);

            }

Appreciate any recommendations / suggestions / solutions. Thanks!

Upvotes: 0

Views: 254

Answers (3)

Fabr&#237;cio Murta
Fabr&#237;cio Murta

Reputation: 345

Use

X.AddScript("applyCss(" + hd + ")");

(but the array string appending approach provided by the others works too)

Upvotes: 0

Coder1991
Coder1991

Reputation: 735

My suggestion would be instead of looping through the cells and RegisterClientScriptBlock function in server side you can get all the cells in one 'string array' and pass that to the client side function as a parameter via RegisterClientScriptBlock function.

Your client side function's parameter should be of string array type and you can loop through the array in the client side which will fetch you the result.

Upvotes: 1

Koby Douek
Koby Douek

Reputation: 16675

The reason is that you can only call Page.ClientScript.RegisterClientScriptBlock once per postback.

My suggestion: Create a string that will hold all the scripts you would like to run, and use that string in Page.ClientScript.RegisterClientScriptBlock:

DataTable holiday = Attendance.getInstance().getHolidays();
string script = string.Empty;

for (var i = 0; i < holiday.Rows.Count; i++)
{
     var hd = holiday.Rows[i]["holiday_date"].ToString();
     script += " applyCss(" + hd + "); ";
}
Page.ClientScript.RegisterClientScriptBlock(GetType(), "myScript", script, true);

Upvotes: 2

Related Questions