Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a javascript confirm dialog that asks the user a certain question. I have the javascript code on the aspx page and calling it from code behind in a loop, the problem I am having is that if a user selects multiple options to transfer some data between tables the confirm dialog only shows up once a transfer has been made.

Please can someone tell me where I am going wrong:
C#
foreach (ListItem Item in listbox1.Items)
{
    if (Item.Selected == true)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "progress", @"window.onload = AddMediaGroupToExisitingSuffix();", true);
        //do transfer
    }
}

C#
function AddMediaGroupToExisitingSuffix() {
    if (confirm("'do you want to transfer data?'") == true)
        return true;
    else
        return false;
}



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
JR009 6-Oct-14 3:33am    
why you are using @"window.onload = AddMediaGroupToExisitingSuffix();" ?

try,

ScriptManager.RegisterStartupScript(this, GetType(), "progress", "AddMediaGroupToExisitingSuffix();", true);
Kats2512 6-Oct-14 3:47am    
i tried all, even alert, the window.onload was just before I posted this and none of them seem to go while looping.
Sinisa Hajnal 6-Oct-14 4:01am    
It will simply register the same function multiple times, will it execute it multiple times too once the page loads?

You should make javascript / jQuery function client-side which iterates through selected items and checks with the user...but it would be better to ask only once for all the data. By asking for each you will get one of the following: users wil l stop using your page due to irritation OR if they have to use it they will start hitting Yes / Enter without paying attention to it and will start processing even if they really don't need / want to.

Single confirmation and then send the marked data - put yourself in users shoes, how would you feel if you clicked select all on the page with 5-10 items and then had to click yes same number of times?
Kornfeld Eliyahu Peter 6-Oct-14 5:35am    
In fact it smart enough to register the same script only once - that the reason it asks for type and key...
Kats2512 6-Oct-14 4:13am    
that is what is required, user must confirm multiple times

1 solution

Refer - ScriptManager.RegisterStartupScript Method[^].
Quote:

The script block that is rendered by the RegisterStartupScript method executes when the page finishes loading but before the page's client onload event is raised. Startup script blocks are located at the bottom of the rendered ASP.NET page just before the </form> tag.


I would suggest you to go for Ajax Modal Popups.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900