0

I have a simple JS pop up "are you sure..." window, along with a dropdownlist and asp:button. What I want when I click the button is to get this message:

"Are you sure you want to perform <%= ACTION %>?"

where ACTION is a string coming from dropdownlist.SelectedItem.Text.

The following does not seem to work:

OnClientClick="return confirm('Are you sure you want to perform <%= ACTION %>? );

It actually just prints the <%= ACTION %> instead of the value.

I have also tried:

function testMe() {
    return confirm('Are you sure you want to perform ' + document.getElementById('<%= hfAction.ClientID %>').value + '?');
}

OnClientClick="testMe();"

But the above causes postback regardless of clicking cancel or OK.

Which is the correct usage?

2
  • how you have defined ACTION in your C# code Commented Jul 19, 2012 at 12:43
  • yes its a simple string. Commented Jul 19, 2012 at 12:46

3 Answers 3

2

Try this:

<asp:DropDownList runat="server" ID="ACTION">
    <asp:ListItem>Action 1</asp:ListItem>
    <asp:ListItem>Action 2</asp:ListItem>
</asp:DropDownList>

<script type="text/javascript">
    function ExecuteConfirm() {
        var ddlId = '<%= ACTION.UniqueID %>';
        var action = $("#" + ddlId + " option:selected").text();
        return confirm('Are you sure you want to perform ' + action + ' ?');
    }
</script>

<asp:Button runat="server" Text="Button" OnClientClick="return ExecuteConfirm();" />
Sign up to request clarification or add additional context in comments.

3 Comments

Microsoft JScript runtime error: The value of the property '$' is null or undefined, not a Function object
I used jquery on my sample code. You can use "var action = document.getElementById(ddlId).value;" instead of the like that contains the "var action = $..."
Ok i was just missing the import in the header. Works fine now. Thanks :)
2

You can't use server tags inside of server control declarations. Set the OnClientClick script in the code behind when initialising the object.

mybutton.OnClientClick 
    = string.format("return confirm('Are you sure you want to perform {0}?');", ACTION);

1 Comment

In what sense? Same issue? Error?
1

Sorry, my last answer was complete garbage (didn't think before posting). What about this (you're almost right)?

function testMe() {
    var val = document.getElementById('<%= hfAction.ClientID %>').value;
    return confirm('Are you sure you want to perform ' + val + '?');
}

OnClientClick="return testMe();"

You just missed the return on the OnClientClick.

2 Comments

Almost there. The problem I'm having is that it is not picking up the new value of hfAction.value after I select another item on the DDL. If I do a postback only then it picks up the value. I set it like so: protected void ddlActions_SelectedIndexChanged(object sender, EventArgs e) { hfAction.Value = ddlActions.SelectedItem.Text; }
Can you not just query the DDL instead of hfAction then? So document.getElementById('ddlActions.ClientID').value?

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.