Is it possible to conditionally add client script to a button?

A

Author

I am trying to tackle this problem, but have no clue.

In my asp.net 3.5 web application (in C#), I have a GridView, in
which, the leftmost column has checkboxes as selectors.

*Outside* of the GridView at the bottom of the page, I have buttons
such as

[View Details] [Edit] [Delete]

When a user clicks on the Delete button, I would like to do either one
(exclusive) of the following:

1) If nothing is selected in the GridView, I display a message like
"Oops, you must select one item."
2) If one or more items are selected, I want to have a client script
popup which says: Are you sure you want to delete the selected item
(s)? And delete only if the user click "Yes".

It seems to be pretty tricky to *conditionally* add this OnClientClick
event to the Delete button.

Any two cents from the experienced? Thanks a lot.
 
A

Author

It seems to be pretty tricky to *conditionally* add this OnClientClick
event to the Delete button.

Luckily, there's no need...

<script "type=text/javascript">

function anyChecked()
{
    for (intElement = 0; intElement < document.forms[0].elements.length;
intElement++)
    {
        if ((document.forms[0].elements[intElement].type == 'checkbox') &&
(document.forms[0].elements[intElement].name.indexOf('MyGridView') > -1))
        {
            if(document.forms[0].elements[intElement].checked)
            {
                return true;
            }
        }
    }
    alert('Oops, you must select one item.');
    return false;

}

function confirmDelete()
{
    if (anyChecked())
    {
        return confirm('Are you sure you want to delete the selected item
(s)?');
    }
    else
    {
        return false;
    }

}

</script>

<asp:Button ID=MyDeleteButton" runat="server" Text="Delete"
OnClientClick="return confirmDelete();" OnClick="MyDeleteButton_Click" />

Thank you. I quickly tried it out. Click on the Delete button then
always shows "Oops, you must select one item." no matter how many (0
or more) are selected.

I guess, what I can try server-side like this:

1) Let the delete button's click event fire.

2) Display message depending on how many are selected.

3) If 1 or more are selected for deletion, display the confirmation
message, followed by a [Yes] button and a [No] button. Only if the
user clicks the [Yes] button, the deletion action is taken.
 
A

Author

Did you change 'MyGridView' to the name of your GridView?


The whole point of doing this is precisely to avoid a roundtrip to the
server...

Yes, I'm aware of that benefit. BTW, are we able to output *into* the
webform using javascript, instead of poping up the alert? Thanks. I
have very limited experience with Javascript.
 
A

Author

So did you change the name? If you didn't, it won't work unless by some
miracle your GridView is actually called MyGridView...

Yeah, I did it change it to gvCustomers.ClientID, but still always get
"Oops, you must select one item."
Yes. The usual method for doing this is to populate a <div /> or a <span />.

OK, that's good. I'll give it a try.
 
A

Author

But your GridView is called gvCustomers, not gvCustomers.ClientID...

Does it work if you change it to gvCustomers...?

Yes, if I change it to gvCustomsers, it works perfectly. Thanks. But
it sorta confuses me. Aren't we supposed to use the client ID of a
control in javascript?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top