NVRAM
NVRAM

Reputation: 7127

ASPX server-side function call after client-side user confirmation?

Maybe I'm searching with the wrong keywords, but I can't find a solution to this.

I've got an ASPX page on which, within an <asp:Repeater> I want to insert a button (per item) that will:

  1. ask (JS "confirm") the user if they really want to proceed, then
  2. call a method on the page class passing in the ID (Guid) from that item.

I can do 1 or 2, but can't figure out how to call back into the page method from JavaScript.

I've tried turning on the EnablePageMethods in the ScriptManager, but that didn't seem to do what I expected -- calling PageMethods.Blah(...) did not seem to have any effect.

Are there any ready examples? In fact, the confirmation string is the same for every entry.

Upvotes: 0

Views: 4451

Answers (5)

NVRAM
NVRAM

Reputation: 7127

Ultimately I decided to do it client-side. I omitted OnClick but set the onclientclick property to a function which confirms, then redirects to the page with a add parameter that is the ID of the table row in question.

Why? Well, my data source is a read-only list created from a couple of tables, and because of that (IIUC) it is created before the button click was handled. Hence, the page refreshed without the data that the user had just added.

Instead, the **add=***ID* parameter causes the add, a success (or error) message and hilights the newly inserted row. The downside is if the user were to refresh the page, of course, but in this case it's within an IFRAME so I don't think that's likely.

So, thanks. You answered my question, I just didn't realize that my question wasn't aiming where I wanted to go.

Incidentally, I'm letting them add a row to the DB. The reason for confirmation is that it incurs up to an hour of processing time on the server!

Upvotes: 0

galets
galets

Reputation: 18472

Here's what you need to do:

  1. Enable script manager (which you did)

  2. Create a web service, which will handle a callback into your code:

    [WebService]
    [ScriptService]
    public class MyWebService : System.Web.Services.WebService 
    {
        [WebMethod]
        public int SomeCallbackFunction() 
        { .... }
    }
    
  3. Put a link to a web service proxy into script manager (generated by [ScriptService]):

    <asp:ScriptManager runat="server" ID="scriptManagerId">
        <Services>
            <asp:ServiceReference  Path="WebService.asmx" />
        </Services>
    </asp:ScriptManager>
    
  4. In your JavaScript, you will be able to use the callback:

    function Blablabla() 
    {
        MyWebService.SomeCallbackFunction()
    }
    

This is only a general idea, of course, this code probably needs some polishing. You can look for more examples here.

Upvotes: 0

roman m
roman m

Reputation: 26521

here's what you need to do:

  1. make sure your button has runat="server"
  2. in that button, add onClientClick="javascript:return confirm('Are you sure?');"
  3. call your SERVER SIDE code from onClick even of the button

    <asp:Button id="Button1" Text="Do Stuff" OnClick="YOUR SERVER SIDE METHOD"    
      onClientClick="javascript: return confirm('Are you sure?');" runat="server"/>
    

Upvotes: 2

bytebender
bytebender

Reputation: 7491

<asp:Repeater ID="YourRepeater" runat="server">
    <ItemTemplate>
        <asp:Button ID="btnDelete" runat="server" OnClientClick="javascript:return confirm('Are you sure ?');" OnClick="btnDelete_OnClick" />
    </ItemTemplate>
</asp:Repeater>

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827236

You can perform the confirmation with the OnClientClick property of your button:

<asp:Button runat="server" ID="DeleteButton" CommandName="Delete"
 Text="Delete" OnClientClick="return confirm('Are you sure ?');" />

Upvotes: 3

Related Questions