Alert/Message

  • Thread starter Thread starter Guy Cohen
  • Start date Start date
G

Guy Cohen

Hi all

How do I present questions/messages to the user while I am in a CLICK event
of a button ?

I was told to look for javascript/vbscript but could not make it work.

Please advise
Guy
 
Guy,

Javascript functions alert and confirm is the way to go. You put them in the
client-side onclick event handlers. Example:

In the <head> section of your aspx page:
<script>
function askUser(text)
{
return confirm(text);
}
</script>

In the < body> section:
<ASP:Button id=myButton runat=server onclick="return askUser('Please
confirm...')" ...

In this simple case you could write just
<ASP:Button id=myButton runat=server onclick="return confirm('Please
confirm...')" ...
without having a separate script in the <head> section. I just wanted to
demonstrate you the general way of doing this sort of things.
 
Hi Eliyahu

I am familiar with that type of code
BUT :)
What if I need to show the alert or ask the question while being in
btnThis_Click (and use vb code...)

I was told to use : RegisterStartupScript

I need a sample pleaseeeeeeeeeeeee


TIA
Guy Cohen


Eliyahu Goldin said:
Guy,

Javascript functions alert and confirm is the way to go. You put them in
the client-side onclick event handlers. Example:

In the <head> section of your aspx page:
<script>
function askUser(text)
{
return confirm(text);
}
</script>

In the < body> section:
<ASP:Button id=myButton runat=server onclick="return askUser('Please
confirm...')" ...

In this simple case you could write just
<ASP:Button id=myButton runat=server onclick="return confirm('Please
confirm...')" ...
without having a separate script in the <head> section. I just wanted to
demonstrate you the general way of doing this sort of things.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Guy Cohen said:
Hi all

How do I present questions/messages to the user while I am in a CLICK
event of a button ?

I was told to look for javascript/vbscript but could not make it work.

Please advise
Guy
 
If you want the alert prior to doing work, the best method, per MSDN, is to
use the Emit Client Script method(s) and add the "attribute" for OnClick to
call the script. If if can be after the click event is finished, you can
Emit the code in the event handler. On the later, if the person refreshes
the page, the popup will come up again, but refresh will also refire the
event handler, so it is a proper action.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
I am familiar with that type of code
BUT :)
What if I need to show the alert or ask the question while being in
btnThis_Click (and use vb code...)

I was told to use : RegisterStartupScript

That's incorrect.
I need a sample pleaseeeeeeeeeeeee

In your Page_Load, add the following:

btnThis.Attributes.Add("onclick", "return confirm('Are you sure you want to
do this?');")
 
My example will work for btnThis_Click. If the user confirms the action, you
will get the server-side event as usual. If he cancels, there won't be any
server-side event either. This is what "return " for.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Guy Cohen said:
Hi Eliyahu

I am familiar with that type of code
BUT :)
What if I need to show the alert or ask the question while being in
btnThis_Click (and use vb code...)

I was told to use : RegisterStartupScript

I need a sample pleaseeeeeeeeeeeee


TIA
Guy Cohen


Eliyahu Goldin said:
Guy,

Javascript functions alert and confirm is the way to go. You put them in
the client-side onclick event handlers. Example:

In the <head> section of your aspx page:
<script>
function askUser(text)
{
return confirm(text);
}
</script>

In the < body> section:
<ASP:Button id=myButton runat=server onclick="return askUser('Please
confirm...')" ...

In this simple case you could write just
<ASP:Button id=myButton runat=server onclick="return confirm('Please
confirm...')" ...
without having a separate script in the <head> section. I just wanted to
demonstrate you the general way of doing this sort of things.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Guy Cohen said:
Hi all

How do I present questions/messages to the user while I am in a CLICK
event of a button ?

I was told to look for javascript/vbscript but could not make it work.

Please advise
Guy
 
My example will work for btnThis_Click. If the user confirms the action,
you will get the server-side event as usual. If he cancels, there won't be
any server-side event either. This is what "return " for.

Your example was:

<ASP:Button id=myButton runat=server onclick="return confirm('Please
confirm...')" ...

That's going to try to run a server-side onclick event, is it not...?
 
Oops... you are right, I forgot that the server-side event has the same
name. Sure one should use Attributes.

Thank you
 
Oops... you are right, I forgot that the server-side event has the same
name. Sure one should use Attributes.

Thank you

Or, use OnClientClick:

<asp:Button ID="btnDelete" runat="server" Text="Delete"
OnClientClick="return confirm('Confirm deletion?')" />

The problem with this approach (which I do use) is that it isn't
really easy to notice in the code. It's only visible in the Source
view in Visual Studio, not Design view or code behind. I don't
normally look too much in the Source view but since I know this is
there, I guess it's ok.
 
Back
Top