alert box in asp.net

  • Thread starter Thread starter sonu
  • Start date Start date
S

sonu

How can we use alert and confirm method in asp.net application.

i am working with application where i want to use confirm box before
going for any operation. and same for alert box

Sonu
 
How can we use alert and confirm method in asp.net application.

i am working with application where i want to use confirm box before
going for any operation. and same for alert box

Sonu

Dealing with popup windows can be tricky and not safe (popup blockers
may negate your efforts.)

The easiest way would be to employ the "confirm" statement in
client-side javascript.

For example, consider the most common case - adding a confirmation
dialog when user attempts to delete a record from a datagrid (gridList
below) on ASP.NET page. The server-side event handler will only
execute if the user clicks "yes" on the client-side, causing the event
to fire:

In your HTML:

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="linkDelete" runat="server"
CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>


In your code behind:

// The below will add a confirmation dialog
// to each linkDelete instance at runtime
private void gridList_ItemDataBound(object sender,
DataGridItemEventArgs e )
{
if( e.Item.FindControl("linkDelete") !+ null )
{
LinkButton deleteButton =
e.Item.FindControl("linkDelete") as LinkButton;

deleteButton.Attributes.Add( "onclick",
@"javascript:return confirm('Are you sure you want to delete this
record?');" )

}
}

private void gridList_DeleteCommand( object source,
DataGridCommandEventArgs e )
{
// Delete the record here
}
 
this is in asp.net and language is vb
Public Shared Sub Alert(ByRef aspxPage As System.Web.UI.Page, ByVal
strMessage As String)
Dim strScript As String = "<script language=JavaScript>alert('" &
strMessage & "')</script>"
If (Not aspxPage.IsStartupScriptRegistered("strKey1")) Then
aspxPage.RegisterStartupScript("strKey1", strScript)
End If
End Sub

the above function is a public shared function which accepts page in which
alert is required and second param is of the message which you want to
dispaly.....
if u write this function in a class then call the class.function name with
params that should work.

For example if i wirte above code in "uitl .class"
then while calling i have to from webform as util.Alert(me, "Hello World")
 
for confirm box

Public Shared Sub Confirm(ByRef btn As WebControls.Button, ByVal strMessage
As String)
btn.Attributes.Add("onclick", "return confirm('" & strMessage & "');")
End Sub

write this code in a static class called util
in webform load you have to map the particular event to this confirm box
with the following systax
Me.button.Attributes.Add("onclick", "return confirm('Your message goes
here');")

this above code should work, further quires feel free to contact
(e-mail address removed)
 
Back
Top