views:

194

answers:

2

I have the following link. On click, I'd like to check the item.primary_company field and if populated, give the user a warning and ask if they would like to continue. How can I do this?

<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>

EDIT

I've changed to this, but nothing happens when clicked. Also, I don't know how to reference the item to do the check on the primary_company field. I only want to message to show if item.primary_company.HasValue. I'd also like to show item.company1.company_name in the confirm message.

<a href="#" onclick="return Actionclick("<%= Url.Action("Activate", new {id = item.company_id}) %>");" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>


<script type="text/javascript">
function Actionclick(url)
{
    alert("myclick");
    if ( confirm('Do you want to activate this company\'s primary company and all other subsidiaries?'))
        {
            location.href(url);
        }

};
</script>
+1  A: 
<a onclick="return companyClick(\"<%= Html.Encode(item.company_name) %>\");" href="<%= Url.Action("Activate", new { id = item.company_id }) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all">
    <span class="ui-icon ui-icon-refresh"></span>
</a>

and in javascript:

function companyClick(companyName) {
    return confirm(
        'Do you want to activate '
        + companyName + 
        ' company's primary company and all other subsidiaries?');
}

And using jQuery:

<a href="<%= Url.Action("Activate", new { id = item.company_id }) %>" title="<%= Html.Encode(item.company_name) %>" class="fg=button fg-button-icon-solo ui-state-default ui-corner-all">
    <span class="ui-icon ui-icon-refresh"></span>
</a>

$(function() {
    $('a').click(function() {
        return confirm(
            'Do you want to activate '
            + $(this).title + 
            ' company's primary company and all other subsidiaries?');
    });
});

UPDATE:

Forgot to escape company's apostrophe:

function companyClick(companyName) {
    return confirm(
        'Do you want to activate '
        + companyName + 
        ' company\'s primary company and all other subsidiaries?');
}
Darin Dimitrov
The link works, but I'm not getting the confirm message.
RememberME
Sorry, I've made a mistake and didn't escape the `company\'s` apostrophe. Please see my update.
Darin Dimitrov
I had added that myself. Still wasn't getting the confirm message though. I added an "alert" before the return confirm and that didn't fire either.
RememberME
Some of my company names have apostrophes in them. I happened to be testing on 2 of those which caused the issue.
RememberME
+1  A: 

The code in your edited example fails because of the double use of doublequotes.

Regarding only showing the confirmation with the company name if item.primary_company.HasValue is true, it can be done either server side or client side.

Server side, change how the link works depending on the status:

<% if (item.primary_company.HasValue) { %>
<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>');"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>
<% } else { %>
<a href="<%= Url.Action("Activate", new {id = item.company_id}) %>"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh">
        link</span></a>
<% } %>

<script type="text/javascript">
    function Actionclick(url,companyName) {
        if (confirm('Confirm. CompanyName = ' + companyName)) {
            location.href = url;
        }
    };
</script>

Client side, send a parameter to javascript, telling it whether or not to confirm:

<a href="#" onclick="return Actionclick('<%= Url.Action("Activate", new {id = item.company_id}) %>', '<%= Html.Encode(item.company1.company_name) %>', <%= item.primary_company.HasValue ? "true" : "false" %>));"
    class="fg=button fg-button-icon-solo ui-state-default ui-corner-all"><span class="ui-icon ui-icon-refresh"></span></a>

<script type="text/javascript">
    function Actionclick(url,companyName,showConfirmation) {
        if (showConfirmation) {
            if (confirm('Confirm. CompanyName = ' + companyName)) {
                location.href = url;
            }
        }
        else {
            location.href = url;
        }
    };
</script>
Simen Echholt
Thank you so much for both examples. That really helps!
RememberME