Results 1 to 5 of 5

Thread: [2008] Image button : OnClientClick

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    [2008] Image button : OnClientClick

    I have an Image Button with a OnClientClick that show a confirmation message.

    I want to use a message that is in a resource file. How would I do that?

    This does not work. My reference to the resource file is the message I see instead of having the keyvalue as message.

    Code:
    OnClientClick="return confirm('<%$ Resources:LacMessage, DeleteConfirmation %>');" />

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Image button : OnClientClick

    Call a method from your attribute:

    OnClientClick="return confirmDelete();"

    From your codebehind, use ClientScript.RegisterClientScriptBlock to write the javascript out to the page.

    This method takes a string, so you need to generate the javascript string first.

    Something like
    Code:
    string deleteConfirmValueFromResourceFile = Resources.MyResourceFile.DeleteString;
    
                string resourceValue = resourceManager.GetString(key);
    
    
    string confirmationJs = "function confirmDelete(){ return confirm('" + deleteConfirmValueFromResourceFile + "');}";
    
    ClientScript.RegisterClientScriptBlock(...............)



    Note the 'something like', implying that I typed this without an IDE, so you'll need to modify it according to your circumstances.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: [2008] Image button : OnClientClick

    hello mendhak.

    I am new to ASP.NET, so it was the first time for me using ClientScript.RegistreClientScriptBlock ..

    Below is the way I did it, could you tell me if is sounds correct to you or If I do not used it the proper way.

    I put this code in the design source code for my Delete button:
    Code:
    OnClientClick="return confirmDelete();"
    I put this code my FormLoad event.
    Code:
    Dim strConfirmDeleteMsg As String = Resources.LacMessage.DeleteConfirmation
    Dim strJavaSConfirmDelete = "<SCRIPT LANGUAGE='JavaScript' TYPE='text/javascript'> function confirmDelete() { return confirm('" + strConfirmDeleteMsg + "');} </SCRIPT>"
    ClientScript.RegisterClientScriptBlock(Me.GetType, "DeleteConfirmation", strJavaSConfirmDelete)
    It works very well. I just wanted to confirm I used that feature correctly before getting a bad habit.

    Thank you for your help.
    DoumB.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: [2008] Image button : OnClientClick

    I just noticed that in the FormLoad, I had to put the code out of my IsPostBack condition, otherwise, the javascript code was disappearing.

    So even if it is a PostBack, I have to execute the VB code again so the javascript still work.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Image button : OnClientClick

    Yes and yes.

    Your code looks good, and I'm glad it's working for you.

    And yes, the javascript needs to be written out to the page each time, it is not part of a control because you are writing it directly to a page rather than to a control, so it does not persist in viewstate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width