JavaScript In ASP Control

G

Guest

Hello, hopefully this is a simple question.. I have the following statemen

<asp:Button ID="cmdPPIPDelete" Text="Delete" Runat="server" CssClass="buttonDelete" CommandName="Delete" OnClick="return confirm('Please Confirm You Want To Delete Record')"></asp:Button

Each time I navigate to this page, I get a compile error of "CS1041 Identifier expected, return is a keyword"

Iif I take out the javascript, then I have no errors when I navigate to this page. So I am pretty confident that it is the Javascript causing the problem. I am wanting to bring up a dialoage box when the user presses the button to confirm the delete

I also tried the following

<script language="javascript"
function RecordDeleteConfirmation()

confirm("Do you want to permanently delete this record"

</script><asp:Button ID="cmdPPIPDelete" Text="Delete" Runat="server" CssClass="buttonDelete" CommandName="Delete" OnClick='Javascript: RecordDeleteConfirmation()'></asp:Button

It gives me a compile error looking for ")"

What am I doing wrong
 
A

Anatoli Trifonov

Compiler tries to parse OnClick as server side script not client side.

What is the solution then? I do not know yet.
Some one else might be able to help.
Try adding scripts on the form level maybe ...

--
_____________________________________
Anatoli Trifonov
Software Developer & Consultant
Minds are like parachutes - they only function when open.
--Thomas Dewar
Jim Heavey said:
Hello, hopefully this is a simple question.. I have the following statement

<asp:Button ID="cmdPPIPDelete" Text="Delete" Runat="server"
CssClass="buttonDelete" CommandName="Delete" OnClick="return confirm('Please
Confirm You Want To Delete Record')"> said:
Each time I navigate to this page, I get a compile error of "CS1041
Identifier expected, return is a keyword".
Iif I take out the javascript, then I have no errors when I navigate to
this page. So I am pretty confident that it is the Javascript causing the
problem. I am wanting to bring up a dialoage box when the user presses the
button to confirm the delete.
I also tried the following:

<script language="javascript">
function RecordDeleteConfirmation()
{
confirm("Do you want to permanently delete this record")
}
</script><asp:Button ID="cmdPPIPDelete" Text="Delete" Runat="server"
CssClass="buttonDelete" CommandName="Delete" OnClick='Javascript:
 
J

John Saunders

Anatoli Trifonov said:
Compiler tries to parse OnClick as server side script not client side.

What is the solution then? I do not know yet.
Some one else might be able to help.
Try adding scripts on the form level maybe ...

In the codebehind, you can add:

cmdPPIPDelete.Attributes.Add("onclick", "return confirm('Please Confirm You
Want To Delete Record')")

You don't want to use the "javascript:" prefix except in the href of an <a>
tag.
 
G

Guest

Thanks, If I am using a DataList, will I have to use the "OnItemCreated" event to associate the script to the control? The example you provided assumed that it was an "ordinary" control. Am I correct in needing to use the "OnItemCreated" event to find the control and then load the value into the control?
 
R

Richard

here ya go for a on ItemDataBound.
Look at the IF statement, it make sure it doesnt put an onclick event to the
header and footer and i suggest you keep it in or else you will get an error
:D

Sub DataGrid_ItemDataBound(Sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header AND _
e.Item.ItemType <> ListItemType.Footer then
Dim deleteButton as LinkButton = e.Item.Cells(8).Controls(0)
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete?');"
End If
End Sub

Jim Heavey said:
Thanks, If I am using a DataList, will I have to use the "OnItemCreated"
event to associate the script to the control? The example you provided
assumed that it was an "ordinary" control. Am I correct in needing to use
the "OnItemCreated" event to find the control and then load the value into
the control?
 
J

John Saunders

Richard said:
here ya go for a on ItemDataBound.
Look at the IF statement, it make sure it doesnt put an onclick event to the
header and footer and i suggest you keep it in or else you will get an error
:D

Sub DataGrid_ItemDataBound(Sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header AND _
e.Item.ItemType <> ListItemType.Footer then
Dim deleteButton as LinkButton = e.Item.Cells(8).Controls(0)
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete?');"
End If
End Sub

Richard, I'd suggest that Jim use e.Item.Cells(8).FindControl instead of
assuming that it will always be Control 0. Also, I think I'd have tested for
ItemType = ListItemType.Item Or ListItemType.AlternatingItem Or
ListItemType.EditItem Or ListItemType.SelectedItem. This will protect
against future item types being added to the ListItemType enumeration.
 
R

Richard

yup, its a much better answer, i just copied the code from an old db project
of mine as an answer to his second question.
 

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

Top