How Do I Add a Client Side Message Box to ASPX Pages?

Update: 06/11/2006
Due to a number of requests I received, I have added a VB version of the code example in this article

In many applications, it's common practice to use a client side dialog, like a message box, to ask the user for confirmation or display a message. For example, if you allow a user to delete, say, an article on your site, you better ask if they are sure they want to delete the item. Many people know they can add a confirmation box to standard HTML or classic ASP pages. However, the same trick is less well known for ASPX pages, but just as possible and easy to implement. This FAQ will show you a few different scenario's for adding a confirmation dialog to your ASPX pages.

Adding a Confirmation Dialog to Standard Buttons

This is probably the most common scenario; you have a simple button that, when clicked, posts the page back to the server where some logic is performed. But before the postback occurs, you want to give your users a way out in case they hit the Delete button by mistake, or because they changed their mind (users do that, you know). There are many situations where this can be used: in a shopping cart on the "Finalize Order" page to ask if the user really wants to order the goods. Or on a forum where you can post a new message. There are really many uses for a dialog box like this.
When you try to add the confirmation dialog, your first attempt may look like this:

<asp:Button 
  id="btnDoSomething" 
  runat="server" 
  Text="Click Me" 
  OnClick="return confirm('Are you sure you want to do this');">
</asp:Button>
However, when you run this code, you get a compilation error. Since the button is a server side button, the OnClick is interpreted as a server side instruction, and not as a client side script instruction. The trick is to add the onclick handler at run time through the Attributes collection of the control. In the code below I am adding the attributes in the Page_Load method of the page, but of course you can add this code to other locations in your code as well:

C#
private void Page_Load(object sender, System.EventArgs e)
{
  btnDoSomething.Attributes.Add("onclick", 
       "return confirm('Are you sure you want to do this');");
}
VB.NET
Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
  btnDoSomething.Attributes.Add("onclick", _
        "return confirm('Are you sure you want to do this');")
End Sub

When you compile this code, and run the page the button will now ask for confirmation when it's clicked. When you press OK, the page will be submitted to the server, causing a postback. If you cancel, nothing happens and the page stays as it was. If you look at the resulting HTML in the browser, this is what you'll see:

<input 
  type="submit" 
  name="btnDoSomething" 
  value="Click Me" 
  id="btnDoSomething" 
  onclick="return confirm('Are you sure you want to do this?');"
/>

This looks remarkably similar to the first code I showed you. However, this time the onclick handler is true client side code, causing the confirmation dialog to pop up on the client. If you press Cancel on the confirmation dialog, false is returned. In the end, this results in the default action for the button (submit back to the server) being ignored.

Displaying a Message Box at Load Time

Another common use for a dialog box is to display a confirmation message when the page loads. Usually, this is done by adding some JavaScript code to the onload of the body tag like this:

<body onload="alert('Hi there');">

This works fine when you need to show the dialog every time the page loads. But what if you want to display the dialog only after a post back? For example, when you want to use a dialog to tell your users their login attempt failed. In such a scenario, you'll need to use RegisterStartupScript. With this method you can add JavaScript of your choice to the page so it fires when the page loads. Instead of using an onload handler for the body tag, the ASP.NET run-time injects your JavaScript to the end of the page, right before the closing </form> and </body> tags. This can lead to a bit messy code, but it'll work almost the same as a cleaner handler. To try it out, add the following code to your page:

C#
if (Page.IsPostBack)
{  Page.RegisterStartupScript("ScriptDescription", 
    "<script type=\"text/javascript\">
    alert('Login Failed. Please try again');</script>");
}
VB.NET
If Page.IsPostBack Then
  Page.RegisterStartupScript("ScriptDescription", _
        "<script type=""text/javascript"">alert('Login Failed. " & _
        "Please try again');</script>")
End If

(Note that the three lines of code that starts with Page.RegisterStartupScript should be on one line). As you can see, this code only runs when the page has been posted back, for example by clicking a Login button. The ScriptDescription can be any name you choose, as long as it's unique for the current page. When you try to register a script block with the same key, the previous block will be overwritten.
The name for the method that adds the code is a bit misleading as you can "register" any type of text or HTML code and not just script. That's why it's necessary to add the <script> tag yourself; if you omit that, the text will end up as plain text in your page. If you look at the HTML source of the resulting page, this is what you'll see:

    <script 
        type="text/javascript">alert('Login Failed. Please try again!');
    </script>
  </form>
</body>
</html>

Adding a Confirmation Dialog to a DataGrid

The final example I want to show you is adding a confirmation box to a button in a DataGrid. Because the buttons are created for each item in the datasource you're binding your DataGrid to, you can't access the button directly to add the Attributes, as I showed you in my first example. However, adding the onclick handler to each button in the grid is easier than you might think. Consider the following simple DataGrid:

<asp:DataGrid id=DataGrid1 runat="server" 
     ShowHeader="False" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateColumn>
      <ItemTemplate>
        <%# Container.DataItem%>
      </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
      <ItemTemplate>
        <asp:Button ID="MyButton" Runat="server" Text="Delete" />
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>

With the following code, I can easily bind some data to the grid:

C#
private void Page_Load(object sender, System.EventArgs e)
{
  string[] movies = new string[] {"Reservoir Dogs", 
    "Pulp Fiction", "Natural Born Killers", "12 Monkeys"};
  // Bind the datasource to the DataGrid
  DataGrid1.DataSource = movies;
  DataGrid1.DataBind();
}
VB.NET
Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
  Dim movies As String() = New String() {"Reservoir Dogs", _
        "Pulp Fiction", "Natural Born Killers", "12 Monkeys"}
  ' Bind the datasource to the DataGrid
  DataGrid1.DataSource = movies
  DataGrid1.DataBind()
End Sub

When you run the page, the Delete button will always cause a post back. To add the client side JavaScript dialog that asks for confirmation box you'll need to make a few changes to the code. First of all, you need a method that is called for each item that you add to the DataGrid. Inside that method, you need to get a reference to the button in the item being added, and then add the JavaScript. Finally, you'll need to hook up that method to the control's ItemCreated event. The method should look similar to this:

C#
public void MyItemCreated(object sender, 
      System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  switch (e.Item.ItemType)
  {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    try
    {  
      Button myButton = (Button) e.Item.FindControl("MyButton");
      myButton.Attributes.Add("onclick", 
         "return confirm('Are you sure you want to delete " 
          + e.Item.DataItem.ToString() + "?');");
    }
    catch {}
    break;
  }
}
VB.NET
Protected Sub MyItemCreated(ByVal sender As Object, _
         ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)


  Select Case e.Item.ItemType
    Case ListItemType.Item, ListItemType.AlternatingItem
      Dim myButton As Button = CType(e.Item.FindControl("MyButton"), Button)
      myButton.Attributes.Add("onclick", _
                 "return confirm('Are you sure you want to delete " _
                 + e.Item.DataItem.ToString() + "?');")
  End Select
End Sub        

For each item that is added to the DataGrid this method is called. Inside the method I use a switch statement (Select Case in VB.NET) to distinguish between the real data items and other items like the Header and Footer for example. Once I know I have an Item or Alternating item I use the FindControl method to get a reference to the Delete button I defined in my template earlier. Note that FindControl returns a generic Control object. The Control class does not have an Attributes collection, so you'll need to cast it to the appropriate control; a Button in this example.
I also use e.Item.DataItem.ToString() to get a string representation of the DataItem that is being bound. I use this string to customize the confirmation message.

The final step is to link this method to the Grid's ItemCreated event. Modify the <asp:DataGrid code as follows:

<asp:DataGrid 
  id=DataGrid1 
  runat="server" 
  ShowHeader="False" 
  AutoGenerateColumns="False" 
  OnItemCreated="MyItemCreated"
>

Now when you run the page, and click one of the delete buttons, this is what you'll see:

Figure 1: The customized confirmation message.


I leave the actual Delete implementation to you, or to a future article.

ASP.NET 2.0 Makes a Confirmation Dialog a Lot Easier

The Button, LinkButton and Imagebutton controls have an OnClientClick property in ASP.NET 2.0 that makes displaying a confirmation dialog even easier. Just set this property to the confirm code you saw earlier:

<asp:Button ID="Button1" runat="server" Text="Delete" 
    OnClientClick="return confirm('Are you sure 
             you want to delete this item?');" />

You can use this code in a plain button that you place directly on a form, but also with buttons that are part of data bound controls, like the new GridView control.

Download Files

Source Code for This Article (C# Only)

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Feedback by Other Visitors of Imar.Spaanjaars.Com

On Tuesday, March 08, 2005 8:30:32 AM Sagrys Bester said:
Can you help me please?

I got it right to create my own custom html message boxes which are  generic, where I pass enumerator types for an icon, message text and buttons to be places on my message box at run time. I have a asp.net/
vb.net web application.

I want to stop users from interrupting the postback event when they click
for example on a button. I would like to display one of my message boxes without any buttons, just text stating "please wait for page postback". This is not a problem, but the trick is when to fire the message, because the javascript method should be on the page before the button is clicked and should be closed via the server side just as soon as processing on the button click has completed.

Thus, when a button is clicked, I wish to display a message, retain focus and therefore not permitting the user to click somewhere else for the duration of the postback. Then I would like to do some processing and close the message box again after the postback has finished.

Any suggestions? Any help would be appreciated!
On Tuesday, March 08, 2005 8:34:08 AM Imar Spaanjaars said:
I think what you need is a progress bar in ASP.NET. There are many controls and articles available for this subject:
http://www.google.com/search?hl=en&q=ASP.NET+progress+bar

Cheers,

Imar
On Wednesday, March 30, 2005 10:27:33 PM Brian Wren said:
Personal opinion:

The line

     onclick="return(confirm('are you sure?'));"

seems more readable to me as

     onclick="return confirm('are you sure?');"

The additional parens are superfluous.

This was a REALLY helpful item.  Thanks.
On Thursday, March 31, 2005 8:53:26 PM Imar Spaanjaars said:
Great suggestion. I have fixed that now.

Cheers,

Imar
On Saturday, April 02, 2005 9:51:04 AM Monaliza said:
Simple yet powerful ::thumbs:: !!!
On Tuesday, April 05, 2005 11:55:39 AM Neil said:
I want the message box to be displayed after a condition is satisfied...
ie, not on page load....
how can i do this??
On Saturday, April 09, 2005 4:10:38 PM Imar Spaanjaars said:
Just add the onclick handler on the location where you need it. E.g. inside an if or else statement.

Use

    myControls.Attributes.Remove

to remove the attribute from the control when you don't need it.

Imar


On Monday, April 18, 2005 6:09:08 AM Kele said:
Hi,

Hopefully a simple question.

I have inserted the following into my Page_Load function:

btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");

Which works on PostBack when the user clicks "OK".

The problem I am having is in the Page_Load function how can I determine if the call was from this Message Box or from some other action.  How can I determine that this resulted in an action from the btnDelete?
On Monday, April 18, 2005 6:23:46 AM Imar Spaanjaars said:
Hi Kele,

You would normally do that by handling the Click event for the button. To make that work, you'll need to wire the button's click Event to a method in your page. In VS.NET, you can just double click the button in Design View.

When the button is inside a data boud control, like a DataGrid, you can use the ItemCommand for this.

Cheers,

Imar
On Wednesday, July 06, 2005 5:45:14 AM Bhupinder pal singh said:
This is so good....
On Friday, July 22, 2005 6:17:04 PM David said:
This was a big help.  I found similar information on other sites, but none of them were anywhere as clear and helpful as this.  Thanks for sharing!

What are the chances Visual Studio will be changed to allow developers to add JavaScript directly in the source, where it belongs?

Thanks again.
On Saturday, July 23, 2005 4:11:55 PM Imar Spaanjaars said:
Hi David,

Thank you for your feedback.

ASP.NET 2 / Visual Studio 2005 will feature more or less the same principles so this article will still apply to the new release.

It's debatable whether the script "belongs there". After all, the buttons etc are server controls, which means that an onclick event will fire at the server. It would be very convenient if there was an onclientclick event though that would emit the required script to the client. Alas, it isn't there so we have to stick to the concepts explained in this article.

Cheers,

Imar
On Wednesday, February 22, 2006 9:24:16 AM Dheeraj Chauhan said:
i want a confirmation messagebox base on some condition and if that condition satisfy only then a messagebox should come. and after that if user choice is yes , then the following code should execute.

On Tuesday 4/5/2005 1:55:39 PM Neil said:
I want the message box to be displayed after a condition is satisfied...
ie, not on page load....
how can i do this??


On Saturday 4/9/2005 6:10:38 PM Imar Spaanjaars said:
Just add the onclick handler on the location where you need it. E.g. inside an if or else statement.

Use

    myControls.Attributes.Remove

to remove the attribute from the control when you don't need it.

Imar

plz clearify your above comments with example......


Dheeraj Chauhan
India

On Wednesday, February 22, 2006 9:36:01 AM Imar Spaanjaars said:
Hi Dheeraj,

Isn't that in the example I showed you?

On the server, use the first solution I offered, using this code

btnDoSomething.Attributes.Add("onclick",
       "return confirm('Are you sure you want to do this');");

This adds the confirmation box on the button's click. Only when the user clicks OK, the code for that button (at the server) is executed.

If, at a later stage in the page life cycle, you think you don't need the confirmation anymore, you can remove the attribute again:

btnDoSomething.Attributes.Remove("onclick");

Hope this helps,

Imar
On Tuesday, May 09, 2006 6:03:54 PM Iris said:
I got a VB.NET form page with a few list boxes and a few dropdown listboxes, which requires the user to make their selection before submit, and when the user submit I want to give them a confirmation message box which included their selected items.  I couldn't accomplish this with buttonConfirm.Attributes.Add(...), so do you guys have any suggestion. PLEASE HELP!

Thanks a million,
--Iris


----------
If (Not Page.IsPostBack) Then
   buttonConfirm.Attributes.Add(...)
End If
On Tuesday, May 09, 2006 6:44:38 PM Imar Spaanjaars said:
Hi Iris,

Can you define "couldn't accomplish" ?? Do you get an error? If so, what error do you get? Without knowing what goes wrong, it's hard to suggest a solution.

Imar
On Thursday, May 11, 2006 1:58:01 PM yogi said:
how to use javascript in aspx file when delet some data  
On Thursday, May 11, 2006 1:59:45 PM Imar Spaanjaars said:
Heuh?? What do you mean??

Imar
On Friday, May 12, 2006 3:17:38 AM dheeraj chuahan said:
Dear Yogi ,
Plz clear your question..
However if you want to show a confirmation message box on some deletion then add button attributes property in the page load as already explained above.If you want some thing else then tell me , I m always there to kill the doubt........................................
On Friday, May 12, 2006 3:26:12 PM Yan Liu said:
great job!
On Thursday, June 08, 2006 10:10:27 AM sunil Kandi said:
When i Click delete button it is showing Delete confirmation.when I click on OK it is sent to Server for futher execution. But when Iam clicking On another Delete button delete confirmation  is not coming.

Why.. HOw to avoid this?????

Please tell me sooon
On Thursday, June 08, 2006 5:22:20 PM Imar Spaanjaars said:
Hi sunil Kandi,

This sounds like a coding error. Can you post your code at http://p2p.wrox.com/ and then send me a link to that post?

Imar
On Saturday, July 08, 2006 4:24:14 PM Franz said:
Hi! Thanks so much for your explanations! Using this, I could add confirmation boxes at many places in our web application. However, I've got a problem: I attempted to start a long calculation by button press in the VB 2005 event handler. So far, it works. Because the procedure can be very lenghty, I wanted to ask the user to confirm before starting and during the calculation, I wanted to update a textbox with a percentage of calculation done to show the user that the application is still working. The problems is that the whole code will only be send to the client browser after completion of the whole on click button event handler. Thus, the confirmation box will appear after the calculation and the textbox will be updated after the calculation procedures, too. What can we do to get out of this? Is there any command to update the interface during running the event handler like doevents in windows programs? Thanks!! Greetings Franz
On Friday, July 14, 2006 6:18:43 PM Imar Spaanjaars said:
Hi Franz,

What you're looking for can be accomplished with AJAX (for example with Microsoft's Atlas implementation).

Search Google for "progress bar ajax" and you'll find some useful stuff.

Cheers,

Imar
On Monday, July 24, 2006 8:52:32 AM manish said:
hi,

i hv master (datalist)-details(datagrid). i want to shw onitemcreated event of mater which has two link buttons delete-edit with data(suppose A). do u want to delete (itmename)a. pls help me.
as alert box is running and showing Delete.
i want to show Delete A.on delete link.

thanx in advance..
                       manish


On Monday, July 24, 2006 9:22:38 AM Imar Spaanjaars said:
Hi manish,

I have absolutely no idea what you're talking about.....

Imar
On Wednesday, July 26, 2006 6:58:39 PM bruno said:
what about running some client script AFTER clientside validation, is that possible? i have a splash screen that should not appear if the page is not valid...
On Wednesday, August 23, 2006 8:43:17 AM Joakim said:
I have a problem when adding a "return confirm" to the OnClientClick: my ASP.NET Validation Controls stop working, i.e., the JavaScript checking the validation controls seems to be blocked, or not run, because of the "return". Is there a workaround to this? I sure would like to be able to do both.
On Thursday, August 24, 2006 4:25:15 PM Imar Spaanjaars said:
Hi Joakim,

When you look at the resulting HTML in the browser, has your button changed from input type="submit" to input type="button"?

In other scenario's that caused the relevant button to not submit to the server again.  If that's the case, you can fall back on the Attributes.Add method instead of the OnClientClick...

Cheers,

Imar
On Monday, August 28, 2006 1:44:21 PM chava said:
Hi,

This is a nice article. But i think i need a different solution for my problem. Here it is (i am using asp.net/vb.net 2.0);

I am implementing a search page, so i have multiple conditions in it.
1) If its an empty search and user clicked on search i should display an information messagebox to the user asking to input atleast one search criteria
2) If the search criteria is very narrow i should ask the user to elobarate it by showing a message

Similarly, i have lot of conditions before starting a search and display an appropriate message if something wrong.

I dont think i can use the above implementation adding an attribute to my button, since i need to display multiple messages based on different conditions.

Can somebody throw a search light?

Thanks,
chava
On Monday, August 28, 2006 9:42:40 PM Imar Spaanjaars said:
Hi chava,

Instead of calling confirm directly, you can call a custom JavaScript function that performs all your tasks, and returns true or false at the end. E.g.:

OnClientClick="return MyFunction();"

function MyFunction()
{
  // Do whatever you need to do here
  // and return true or false
}

Hope this helps,

Imar
On Tuesday, September 05, 2006 1:07:50 PM gabriel okey said:
I have a VB.NET form page with a few radio buttons, dropdown listboxes and textboxes which require the user to make their selections and input before they submit, and when the user submit, I want to give them a  message box prompt asking,"Are you sure you want to submit?".  I couldn't accomplish this with buttonConfirm.Attributes.Add(...). On button click, the page loads without displaying the message box. Do you guys have any suggestion?
On Thursday, September 14, 2006 7:24:42 AM kapil said:
I want to prompt one confirm message  based on some condition.

I saw your previous mail based on this question.

But my question is when i add button.Attributes.Add() in page load it will add the confirmatio to button 's click event

Now in button's click event itself I am checking one condition and if that condition is true only then i want to prompt for confirmation and if it is "Yes" only then I want to execute some code .

If i remove onclick event from button  in the button code itself it will not work

How to solve this issue
On Friday, September 15, 2006 2:59:51 PM Sharad Yadav said:
Respected Sir,

I have problem with javascript alert box

when I

run my form in IIS ,It displays correct .
But when I use IP address then it does not Show alert dialog box,
instead it displays the action of OK button directly

Please give me solution on this .


Thank You.

With Regards
Sharad Yadav

On Saturday, September 16, 2006 11:40:30 AM Imar Spaanjaars said:
Hi Sharad,

How does the final HTML in the browser look? Do you see a difference between the two pages?

I suuggest you post your question together with the HTML source in a forum at http://p2p.wrox.com and then I'll take a look.

Cheers,

Imar
On Friday, September 29, 2006 4:51:16 AM Dinesh said:
It is a good article.
I have two DetailView controls on one page. I have added delete confirmation to both of them. I do get the confirmation message when clicking on delete, but the page doesn't post back even when clicking on OK. If I remove the delete confirmation script, it works fine.

Can you please help?

Thanks
On Saturday, September 30, 2006 8:02:46 PM Imar Spaanjaars said:
Hi Dinesh,

The same applies to you as it did to Sharad:

How does the final HTML in the browser look? Do you see a difference between the two pages?

I suuggest you post your question together with the HTML source in a forum at http://p2p.wrox.com and then I'll take a look.

Cheers,

Imar

On Monday, October 16, 2006 11:58:54 AM Vincent said:
Great coding, thks for sharing !!
This helps me a lot !
On Thursday, October 26, 2006 8:38:21 PM Sean said:
Thank you very much for your code examples. I was able to get the client-side message box to include the text from a selected index in a datagrid:

deleteButton.Attributes("onclick") = "return confirm('Are you sure you want to delete " & e.Item.Cells(3).Text.ToString & " as a processor?');"

The third cell being the column with the processor's Name. (It took a few trys to find the right syntax.)

Can you direct me to info on a compair validator for a datagrid edit textbox and another field in the same selected row?

Thanks,
On Thursday, November 09, 2006 12:55:34 PM naveen said:
Great Article .....

The code can also be this way...

this.DeleteButton.Attributes.Add("onclick", "event.returnValue = window.confirm('Do You Want To Delete?');");

The Delete button on click routine is called and only when the ok button is clicked...

Can you please tell me a similar confirmation box which is cross browser compactiable... (Esspecially in Fire Fox)????
On Thursday, November 09, 2006 7:42:46 PM Imar Spaanjaars said:
Hi naveen,

I am not sure what you're asking. The code I used in the article *is* cross browser and *does* work in FireFox.

Why do you want to use something like event.returnValue = window.... ?

Imar
On Thursday, November 09, 2006 9:53:48 PM Kdkcchoco said:
Hi Imar

I have a little different situation.I have a drop down list.I have to check on the selection of few items from list and want a confirmation pop up accordingly on the selection of only those items from DD list.And again if clicked on yes perform certain steps and if no cancel the whole operation of making change from drop down.

Any help on this will be appreciated considering that i am new to Java Script.

Thanks
Choco
On Friday, November 10, 2006 4:49:48 AM Naveen said:
Hi Imar,

THank you for making me clear...

I was using the syntax I specified in my previous comment...

But now I am clear and the code is compactiable in IE ad Firefox...


Thank you very much...

Regards,
Naveen
On Friday, November 10, 2006 6:26:53 AM Imar Spaanjaars said:
Hi Kdkcchoco,

Although it depends on your design and implementation, you could roughly apply the same techniques to your solution. However, without seeing any code, it's hard to suggest something useful.

I think you're better off posting this at a forum like http://p2p.wrox.com. If you do post there, be sure to be more specific about your situation and include your code (if you have any).

Cheers,

Imar
On Friday, November 10, 2006 6:27:56 AM Imar Spaanjaars said:
Hi Naveen,

Glad it's all working now....

Imar
On Thursday, December 07, 2006 9:17:37 AM Oggie said:
I have in my ASP 2.0 web form Button Delete like this:

[asp:Button ID="btnDelete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this item?');" Text="Delete" Width="56px"  /]

When I click the message box apears. But I want this:

Sub Button_Click

    If gridview is selected then
        Msgbox(Do you want to delete this item?.....
    Else
        Msgbox (You have to select gridview row!)
    end if
    

End Sub

How to do this? I would appreciate your help because I am new in ASP.NET.
On Thursday, December 07, 2006 9:19:59 AM Imar Spaanjaars said:
Hi Oggie,

I am not sure I understand what you want. Where do you want this code to run? On the server or on the client? And when you click one of the Delete buttons in the Grid. doesn't that mean it's already selected?

Can you elaborate a little?

Imar
On Thursday, December 07, 2006 11:57:08 AM Oggie said:
I have in my ASP 2.0 web form Button Delete and I want to use this script for confirmation message box but in this way:

Sub Button_Click

         If gridview is selected then
               Msgbox(Do you want to delete?.....
         else
                Msgbox (You have to select gridview.....
         end if

End Sub

I would appreciate some VB example because I am new in ASP 2.0
Thank you
On Thursday, December 07, 2006 12:03:27 PM Imar Spaanjaars said:
Well, you just repeated your previous post, without adding much detail.

Where do you have this button? Where do you want to run this code? What makes up a selected row? When do you want to fire this code? Without these details, I can't help much.

BTW: as you found out, you can't post HTML or ASP server tags here. Replace them with something like []

Imar
On Thursday, December 07, 2006 12:14:56 PM Oggie said:
Hi,

I have in my ASP 2.0 web form Button Delete and GridView. Button Delete is not in gridview, it is outside the grid on the form.
I want my user to select gridview row, and then click the Delete button to delete selected row. This is on the client side, on aspx page.

I want to use this script for confirmation message box but in this way:

Sub Button_Click

         If gridview is selected then
               Msgbox(Do you want to delete?.....
         else
                Msgbox (You have to select gridview.....
         end if

End Sub

I would appreciate some VB example because I am new in ASP 2.0
Thank you
On Thursday, December 07, 2006 12:16:50 PM Oggie said:
Sorry for repeated  previous post, my internet connection is bad.
On Thursday, December 07, 2006 12:19:55 PM Imar Spaanjaars said:
Hi Oggie,

There's no need to post the same code over and over again. Seeing it once is more than enough. It's the explanation that was missing.

Anyway, you may want to take a look at this article:

http://msdn.microsoft.com/msdnmag/issues/06/05/CuttingEdge/

Imar
On Thursday, December 07, 2006 12:42:41 PM Oggie said:
Imar,
I didn't post the same code over and over again. I send it only once, but my internet connection was broken, so I posted again. But, as you can see my post is repeated several times. I am sorry, you can delete that post.

I saw link you gave me, but it didn't solve my problem.  I wonder can I find some MessageBox.dll or some function to call from client side, without having to write javascript. I am very confused with this "simple" problem.
Thank you

On Thursday, December 07, 2006 12:48:31 PM Imar Spaanjaars said:
It's getting weirder and weirder.

Anyway, in JavaScript you can use confirm to ask for confirmation. If you don't want to use JavaScript (don't understand why) you can use VBScript which is Internet Explorer only. VBScript does have a MsgBox command to trigger a message box.

However, I suggest you look into JavaScript. It's the only client side language that workds cross browser.

Imar
On Friday, December 08, 2006 4:31:49 AM manoj.banga said:
Respecte Sir,
I have a save button and the validators on the page. the save button onclientclick method calls the javascript function to check some list values.  when i put some thing wrong on the text boxes which have the validators associated with it and when i press the save button the validator displays the error message in red color but the page post back which i do not want to. Please help me.
On Friday, December 08, 2006 11:26:47 AM Imar Spaanjaars said:
Hi manoj,

Are you calling a custom validation method? If so, do you return false from it when validation has failed? This is necessary to stop the form from being submitted.

If that doesn't help, I suggest you post your code on a forum like http://p2p.wrox.com so I can take a look at it.

Cheers,

Imar
On Tuesday, February 06, 2007 9:49:45 PM Gus said:
Nice code and it works for me. But how to handle it with the menu control. I have Menu control with one menu item "Delete". I need to let the users popup a msgbox with alternativ as Yes No.

Thanks
On Tuesday, February 06, 2007 9:55:28 PM Imar Spaanjaars said:
Hi Gus,

What kind of menu?

You should follow the same pattern, and use FindControl to look into the menu, find the control you want to add the behavior to, and add a client side onclick attribute.

Hope this helps,

Imar
On Tuesday, February 06, 2007 10:18:15 PM Gus said:
Hi Imar!

It is the standard menu control of VS 2005. In the menu I have item as "New", "Delete", "Print" and so on. The msgbox should fire when pressing "Delete" or "Print" with diffrent text to display. But how get the Attributes.Add for the diffrent menu items

Thanks for quick response!

Gus
On Tuesday, February 06, 2007 10:32:00 PM Imar Spaanjaars said:
Interesting question....

I don't know. The menu consists of MenuItem objects that don't seem to have useful things like a Controls or Attributes collection. Also, the Parent and Children return other MenuItem objects.

I think there must be a way to do it, but I just don't know how yet..... Maybe diagnosing the control hierarchy fo the Menu control sheds some light....

Imar
On Sunday, February 11, 2007 6:24:49 AM anubhuti mishra said:
i was worried for message alert and i got appropriate concept regarding this.
hello! visitors you can get benifitted by this site.
anubhuti
On Friday, March 23, 2007 4:36:34 AM David Nowak said:
Thank you for the article--I can only echo what so many have already said--it helped greatly.

I have a problem however.  I have a page with a server-side submit button, several text boxes, and some validation controls for required values.  If I do not add the javascript to the button attributes the validators fire correctly.  However, if I add a javascript function to provide confirmation "are you sure you wish to submit" the validators do not fire.  These validators are not custom, they simply are the validators provided in the toolkit.

Any help you can provide would greatly be appreciated.

Thank You.
On Friday, March 23, 2007 9:42:18 AM Imar Spaanjaars said:
Hi David,

Can you please post this at a forum like http://p2p.wrox.com? It's hard to see what's going on without seeing your code, and my site isn't the best place to post code in comments....

Cheers,

Imar
On Saturday, March 24, 2007 7:39:39 AM vinod said:
i am seeing u r code.
i am working on asp.net 2.0
where
          i am not getting
                            page.registerstartupscript()
                             is not found
                              if any namespace required tell me.
On Saturday, March 24, 2007 8:17:38 AM Imar Spaanjaars said:
Hi vinod,

Take a look here:

http://msdn2.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

RegisterStartupScript is now deprecated. You should use a method with the same name on the ClientScriptManager now.

Cheers,

Imar
On Monday, April 16, 2007 7:36:30 AM Poornima said:
This si a nice forum
On Monday, April 16, 2007 7:38:30 AM Poornima said:
I want to prompt one confirm message  based on some condition.

I saw your previous mail based on this question.

But my question is when i add button.Attributes.Add() in page load it will add the confirmation to button 's click event

Now in button's click event itself I am checking one condition and if that condition is true only then i want to prompt for confirmation and if it is "Yes" only then I want to execute some code .

If i remove onclick event from button  in the button code itself it will not work

How to solve this issue
On Monday, April 16, 2007 7:52:59 AM Imar Spaanjaars said:
Hi Poornima,

Instead of doing the confirm directly in the onclick handler, you can call a custom JavaScript function that performs your logic.

E.g. onclick="MyMethod();"

where MyMethod refers to a JavaScript function that returns either true or false.

HtH,

Imar
On Tuesday, June 19, 2007 8:44:50 AM Manish Kawatra said:
Im working in asp.net 2.0 AJAX
I Want to display confirmation box  on button2 .
example
button1
{
some statements-

button2_Click(sender,e)
}
both buttons are in update panel.
when i use  
string strMessage = " do u want to book a hall";
ScriptManager.RegisterStartupScript(UpdatePanel3, UpdatePanel4.GetType(), "clientScript", "confirm('" + strMessage + "'"hid_f")", true);
it shoe the message but also execute the statement of  button2 then message display on screen
    
On Tuesday, June 19, 2007 9:15:48 AM Imar Spaanjaars said:
Hi Manish,

Hard to tell without seeing more of your code. Can you please post this on a forum like http://p2p.wrox.com?

One thing that strikes me as odd is this: ScriptManager.RegisterStartupScript Why would you want to register a startup script for this? That fires as soon as the page is done loading.

Cheers,

Imar
On Wednesday, June 20, 2007 4:43:50 AM Manish Kawatra said:
Thanks Imar
main problem is when we click on button then it show confirmation message . when we use ajax tool confirmation button or through ScriptManager.RegisterStartupScript . but visiblity of button is false n IM using
like this
Button2_Click(sender,e) ;(calling as function.)
in button1_click ( in code behind)
IM working in C#.net n to display a message IM using messagebox dll that work on page refresh so I used ScriptManager.RegisterStartupScript .
my requirement is display a confirmation message when it come to
Button2_Click(sender,e) this statement n if yes then go further otherwise
return;
On Wednesday, June 20, 2007 5:17:34 PM Imar Spaanjaars said:
Hi Manish,

I assume you're trying to create a client side alert? If so, what do you need the messagebox dll for (whatever it is)? Why don't you use JavaScript's alert method?

Anyway, like I said, it's hard to tell without seeing your real code. The stuff you posted doesn't reveal much. Again, may I recommend you post this on a forum like http://p2p.wrox.com?

Imar
On Monday, June 25, 2007 4:29:57 PM Jagan said:
This is a nice posting. I need similar kind of functionality but on a condition and that condition has to do check some things on the server like user's first name and last name combination already exist. I do all this in my page load and then add the client side JS function to my button.OnClientClick. This works partially and not a stable solution. The biggest problem is once attributes added it invokes the client side script and based on confirmation box ("Yes" or "No") it further proceeds to button click event. When user name already exist the attributes added and confimation box is popped up. But when I change user's first or last name to some value which don't exist already, the confirmation box is launched anyways since this happens before page load (as this link existed before) and if user says No it won't even go into the page load and attributes exist forever. If user picks Yes then page load happens and check for valid user name is going to happen again and then attributes are corrected based on it. I think this solution may not be appropriate for this situation and I have tried to see if there are any other solutions (like launch a cofirm box from within ASP.NET server side code). Unfotunately I'm not finding a correct solution. I would appreciate any kind of help. Thanks!
On Monday, June 25, 2007 4:33:41 PM Imar Spaanjaars said:
Hi Jagan,

This is a bit too off-topic for me to answer here. May I suggest you post this on a forum like http://p2p.wrox.com? Makes it easier to share and suggest code.

Cheers,

Imar
On Monday, June 25, 2007 5:17:56 PM Jagan said:
Hi Imar,

Thanks for your quick response. I'll post it to http://p2p.wrox.com. I'm going crazy with this issue.

Jagan
On Friday, July 13, 2007 8:47:15 AM Mekonnen said:
I got the articley Very interesting. Since I am working know on asp.net and want to use message box on client side.

Since I am new to ASPX, Would you please help me how I know wether the user clicks yes or NO.
Thanks
On Friday, July 13, 2007 8:51:03 AM Imar Spaanjaars said:
Hi Mekonnen,

Well, if the page posts back to the server, the user clicked OK; otherwise they clicked Cancel.

Instead of returning false from the onclick handler, you can also call a custom JavaScript function.

Cheers,

Imar
On Wednesday, July 18, 2007 4:49:52 PM Angel Dávila said:
the code of naveen run perfectly
On Monday, July 23, 2007 4:52:26 PM mark said:
i am assuming that using the same technique, i can create an onerror event in my .aspx mobile page.

my javascript created by .Net is throwing a javascript error when the link in the objectlist is created. Would like to understand the specifics of the reason why this is happening.
On Monday, July 23, 2007 4:55:09 PM Imar Spaanjaars said:
Hi mark,

Hard to tell without seeing your code. May I suggest you post this on a forum like http://p2p.wrox.com?

Imar
On Monday, July 23, 2007 5:00:32 PM mark said:
Below is the HTML Page rendered by .NET.  carried over from the 1st post.

It used to work up until my hosting company upgraded their .Net

Server software, and this is when the problem began.

It is throwing a javascript error when the link is clicked.



script language=javascript
function __doPostBack(target, argument)
{
var theform = document.Form1
theform.__EVENTTARGET.value = target
theform.__EVENTARGUMENT.value = argument
theform.submit()
}
script>

a href="javascript:__doPostBack('link_add_tournament','Form5')"
Create New Tournament

On Monday, July 23, 2007 5:02:34 PM Imar Spaanjaars said:
Hi mark,

Again, hard to tell. This code doesn't tell me much, and it also doesn't really help you're not describing your problem in detail; including the error message you get and so on.

Again, may I suggest you post this on a forum like http://p2p.wrox.com?

Cheers,

Imar
On Tuesday, July 24, 2007 5:29:08 AM Mekonnen said:
Spaanjaars ;
Thanks very much for your help.

Is there any body who can help me in giving me a code that imports data from EXCEL to SQL Server 2000?
On Monday, September 24, 2007 6:52:41 AM lakshmiPriya said:
please help me!
i have one image button already i want to display one message while iam click that image button please reply soon
On Monday, September 24, 2007 6:25:17 PM Imar Spaanjaars said:
Hi lakshmiPriya,

I have no idea what you're talking about or asking.....

Sorry.

Imar
On Wednesday, October 03, 2007 9:36:19 AM Siddharth said:
I want to show alert message when data is saved that you save successfully and on update successfully ,how can i do it and on which event i ahve to work.
On Wednesday, October 03, 2007 7:41:23 PM Imar Spaanjaars said:
Hi Siddharth,

You can use RegisterStartupScript on the ClientScriptManager (in ASP.NET 2.0) to register a script that fires when the page loads after a Post Back.

Cheers,

Imar
On Thursday, May 08, 2008 4:04:46 PM Donald Joyce said:
You guys are awesome!!! Thank you so much!!!
On Wednesday, July 09, 2008 7:23:04 AM sandhya said:
I have a grid view in one page with 9 columns and when i select a row the values in that row should be displayed in other page which has 9 textboxes for each, using cross postback and hidden value
On Wednesday, July 09, 2008 7:27:40 AM Imar Spaanjaars said:
Hi sandhya,

How is this related to the original topic of this article: "How Do I Add a Client Side Message Box to ASPX Pages?"?

Imar
On Monday, August 18, 2008 6:41:38 AM Aakash said:
Hi,

I was using this script in my other pages but eventually I got a problem in this.

Actually, I want to display a messagebox to the user on the session timeout. On timeout, I need to display a messagebox and also redirect to the another page. In this scenario, it is not working.

Can you suggest me how to do it?

Thanks

Aakash
On Monday, August 18, 2008 6:59:35 AM Aakash said:
Hi,

don't bother.

I've got the solution.

Thanks
On Friday, August 29, 2008 9:20:47 AM Tijo said:
I have a problem here. i'm working on Visual Studio 2005. i have a gridview in which i have a delete button. On click of the delete button the user should be notified with the confirm message box telling him that he is abt to delete one record. and when he clicks on YES the record should be deleted .

I have doen this by doing the following
. i added a link button to the Grid by using a template field. and onClinet Click i have  coded as shown below OnClientClick="return confirm('Are you sure
             you want to delete this item?');"

This would give me a confirm message box.

But the problem for me ios that i wasn't able to identify the Row which the user is cliking  So if you guys have a solution for this or if ou feel like i have gone with the Wrong approch please Comment on this.i would be very much thankful.
On Tuesday, September 02, 2008 4:57:11 PM Imar Spaanjaars said:
Hi Tijo,

There are a few ways do to it. The simplest way is to add a Delete field to the grid.

Alternatively, you can use the CommandArgument and CommandName properties of the LinkButton and then handle the RowCommand event of the GridView:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

Cheers,

Imar
On Tuesday, September 23, 2008 9:48:41 AM shanwaj said:
Hi Imar,
  i am developing a web project(vs 2005 & MsAccess) with vb.net.in my home page i want to give some updates as alerts when a end user access or refresh the page.here i am using registerstratupscript method to give alerts after loading page to onload method.every thing is alright means i am getting only one update ,it means the page is calling registerstartupmethod only once. i am giving my code here. i want 2 to 3 updates as alerts by calling rgisterstartupscript method.plz help me how can i do it.i tried in many ways and searched google also. thanks in advance.

dim count as integer=ds.tables(0).rows.count

for i =0 to count
  if i>=count then
     exit for
   else
     dim dr as datarow=ds.tables(0).rows(i)
     dim strNews as string=dr("News")
     dim strDate as dateandtime=dr("startdate")
     dim strMsg as string=strnews & "" & strdate
     dim s as string=convert.tostring(i)    'key value
     dim strScript  as string="[script language=JavaScript]function Page_load(){alert('" & strMsg & "');}"
      strScript +="[/"
      strScript +="script]"
     if not page.isPostBack then
         If not Page.IssartupscriptRegistered(s) then
           Dim strAlert(count) as string
            strAlert(i)=strScript     'string value
            page.RegisterStartupScript(s,strAlert(i))
    
         End If
    End If
  
end if
next

In my table  table is containing 3updates. i am getting 3rd update only,i want 3 updates simantaneously like first,second,third. plz help me
On Tuesday, September 23, 2008 4:54:23 PM Imar Spaanjaars said:
Hi shanwaj,

Can you please post this on a forum like http://p2p.wrox.com?

This is a bit too uncoupled code for me to digest here.

Quick suggestion: I think you're overwriting the script on each iteration. Take a look at the documentation for Page.RegisterStartupScript (which is obsolete) and look at the key parameter in particular. Not sure if that's the real problem though.

Cheers,

Imar
On Wednesday, September 24, 2008 4:06:13 AM shanwaj said:
Hi Imar,
  thanks for your quick response.as per your suggestion  iam going to post this on p2p.wrox forum.one thing i want to ask,is it possible to give 3 to 4 alerts with RegisterStartupScript to page onload method.before loading page i am getting alerts with RegisterClientScript method.but i want with RegisterStartupScript method.i tried in too many ways.Plz give me  suggestion how to solve my problem.
thank you
On Wednesday, September 24, 2008 9:19:04 PM Imar Spaanjaars said:
Hi shanwaj,

As i suggested earlier, please try this on a forum like http://p2p.wrox.com. Much easier to deal with questions like this over there.

Cheers,

Imar
On Saturday, January 03, 2009 5:12:34 AM karthika said:
hi ,
im doing online exam project.  im using Ajax with Timer.here i want to display alert message when  time goes like tis 0:0:0.0.

plz any one help me.
its very urgent.

thanks in advance
karthika
On Saturday, January 03, 2009 8:54:29 AM Imar Spaanjaars said:
Hi karthika,

This is a bit too off-topic for me to answer here. Try this on a forum like http://p2p.wrox.com. Much easier to deal with questions like this over there.

Cheers,

Imar
On Monday, January 05, 2009 4:33:01 AM shanwaj said:
Hi  Imar,
   I read ur article,It is very useful for us which is binding confrimation box to datagrid.Thanks a lot to posting this article to me.till now I am using vb.net to develop web project.I want to learn c#.Could you please tell me any online free c# book.
thanks a lot
        shanwaj
On Monday, January 05, 2009 8:21:25 AM Imar Spaanjaars said:
Hi shanwaj,

Sorry, don't know of a free eBook that teaches you C#. It may be worth checking out paid books like this one: http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Microsoft-Visual-C-2008.productCd-047019135X.html

Cheers,

Imar
On Sunday, April 12, 2009 7:19:15 PM SPD said:
Hi,
I have a problem,i am using server side button,when i click this it has to pop up confirm msg box,i used your approach,it works fine.but problem is i have few  requiredfield and compare validators on my page.If i enter wrong value i get error msg in validators along with msg box .How do i stop them from doing this.I want msg box to be poped only when all fields have proper value and user clicks that button.I am using Asp.net 2.0
and using OnclickClient event.

pls help.
On Sunday, April 12, 2009 7:33:47 PM Imar Spaanjaars said:
Hi SPD,

You can call Page_ClientValidate() from your client side code to trigger the validation at the client. When it returns false, don't call the confirm method.

Cheers,

Imar
On Friday, April 17, 2009 5:26:09 PM Jim said:
how can you do the samething but use a dropdownlist. I have it connected to the DDL, but how do you trap the return value
On Friday, April 17, 2009 6:20:15 PM Imar Spaanjaars said:
Hi Jim,

I am not sure I understand what you're trying to accomplish...

Cheers,

Imar
On Friday, April 17, 2009 6:48:06 PM Jim said:
instead of a button for the pop msgbox I would like to use a Dropdownlist box. In this list would be edit, delete, and close on the delete and close would trigger the pop msgbox 'are you sure you want to do this?'  right now I have in the DDL1_Click sub where i have select statement for Delete and close and the pop msgbox opens from there that works. now how to I check for 'Ok' and 'Cancel'?

sorry the the confusion before.

PS. I am working in asp.net using VB and .Net 1.1

Jim
On Friday, April 17, 2009 8:30:46 PM Imar Spaanjaars said:
Hi Jim,

I still don't get it completely. Sentences such as these:

"right now I have in the DDL1_Click sub where i have select statement for Delete and close and the pop msgbox opens from there that works."

don't really help. Are you aware of an invention called interpunction? You may want to reconsider what you've typed before you hit the Post Comment button.

Anyway, it seems you're mixing up client and server side code. DDL1_Click seems to suggest a server side handler while the alert box trigers at the client. You can use the *JavaScript* confirm method confirm to pop up a message box at the *client*. The method returns true for OK and false for Cancel so you can use that to determine other actions.

Cheers,

Imar
On Monday, April 20, 2009 8:54:06 AM shanwaj said:
Hi Imar,
   I unsubscribed this article thread in my mail by mistakely.Please post me this updated article to me.Thank you very much for sending me this article
On Monday, April 20, 2009 10:11:08 AM jim said:
Sorry Imar,

I am using your "Confirmation Dialog to Standard Buttons" to have a msgbox pop up when the user select a dropdown list item.

Here is the code example: Dropdownlist_click

If DropDownList1.SelectedIndex() = 1 Then
MessageBox("you have selected close")
End If      

Sub MessageBox is a mix or code from your example and my additions

Sub MessageBox(ByVal msg As String)
Dim lbl As New Label
lbl.Text = "script language='javascript'" & Environment.NewLine & "confirm('" + msg + "')
/script"
        Page.Controls.Add(lbl)
    End Sub

Note Removed the bracets from the script line

All of this is working great I can Select an item from the dropdown box and the messagebox popup

My Question is how do I check for 'Ok' and 'Cancel' ?


Jim
On Monday, April 20, 2009 6:10:18 PM Imar Spaanjaars said:
Hi shanwaj,

If you checked the "notify me of replies" option when replying, you should receive new updates again from now on.

Cheers,

Imar
On Monday, April 20, 2009 6:18:25 PM Imar Spaanjaars said:
Hi jim,

You can't do that like this. Like I said, confirm runs at the client, while you need to access the result at the server. I see at least two options:

1. Make a button that posts back to the server. If the user clicks OK, the page is submitted and thus you know OK is clicked. This works fine if you're not interested in the Cancel option.

2. Store the result of the confirm box in a (hidden) text box (asp:Hidden for example) in the page using JavaScript. Post back the page in both cases, and at the server read the value of the hidden field.

Cheers,

Imar
On Wednesday, March 10, 2010 1:16:19 PM suman said:
I Want to display confirmation box  on button2 .
example
button1
{
some statements-

button2_Click(sender,e)
}
both buttons are in update panel.
when i use  
string strMessage = " do u want to book a hall";
ScriptManager.RegisterStartupScript(UpdatePanel3, UpdatePanel4.GetType(), "clientScript", "confirm('" + strMessage + "'"hid_f")", true);
it shoe the message but also execute the statement of  button2 then message display on screen
    
On Wednesday, March 10, 2010 3:20:35 PM Imar Spaanjaars said:
Hi suman,

First of all, you don't have the "return" keyword to do something with the return value from confirm. Secondly, a start up script is just that: some code that runs at start up. Why do you think it would change the button's behavior?

Imar
On Tuesday, August 17, 2010 8:02:23 AM Aleksey said:
Thank you, I was looking for it during three days
On Friday, December 17, 2010 9:07:19 PM Ed said:
But what if we want a server-side popup with Yes/No buttons instead of the Ok/Cancel buttons that confirm() uses?  Is this simple to do in an asp.net web application using c#?
On Friday, December 17, 2010 9:10:46 PM Imar Spaanjaars said:
Hi Ed,

A server side pop up makes not sense. Are you going to click Yes or No on the server for all your visitors?

If you mean a client side message box, but with buttons other than OK and Cancel, look into client side libraries such as jQuery UI.

Cheers,

Imar
On Saturday, December 18, 2010 10:02:47 PM Ed said:
Sorry, I meant client-side.  Thanks for pointing me to jquery ui.
On Friday, November 25, 2011 5:48:41 AM sanjeet kumar said:
HI,
  I am displaying day and date from sunday to sat in label.it's taking from first label to last label and displaying day and date.I want to select any date and infront of that day it should display.in asp.net.
date and time is coming ,but i want to fix day from sun to sat and selected date should display when i will select date from calendar.
On Friday, November 25, 2011 5:51:00 AM Imar Spaanjaars said:
Hi sanjeet,

a) I have no idea what you're asking or saying

b) How is this related to the original topic of this article: displaying a message box?

Try posting this on a forum such as http://p2p.wrox.com

Cheers,

Imar
On Saturday, December 17, 2011 5:45:14 PM Ankush said:
Hello, I want to use confirm message to stop closing site, but unable to understand. Please help me if possible
On Saturday, December 17, 2011 6:03:43 PM Imar Spaanjaars said:
You really don't want that as your users will hate you for it. It's up to them to decide when they want to leave.

If you want to persist, try a Google search.

Cheers,

Imar
On Thursday, January 05, 2012 4:54:27 AM za said:
hi imar :)

wanna ask you how i want to show alert message after the data has been saved into database .

i had tried this , it works but the data is not insert into database :(
it's appear the alert message only .

protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onclick","javascript:alert('your data has been saved')");
        }

protected void Button1_Click(object sender, EventArgs e)
        {
            con.Open();

            //insert data into table
            da.InsertCommand = new SqlCommand("INSERT INTO T3L3B VALUES (@bil_ap_dipasang,@bil_pengguna,@peratus_liputan)", con);

            da.InsertCommand.Parameters.Add("@bil_ap_dipasang", SqlDbType.Int).Value = Convert.ToInt32(txtap.Text.ToString());
            da.InsertCommand.Parameters.Add("@bil_pengguna", SqlDbType.Int).Value = Convert.ToInt32(txtpengguna.Text.ToString());
            da.InsertCommand.Parameters.Add("@peratus_liputan", SqlDbType.Int).Value = Convert.ToInt32(txtperatus.Text.ToString());
            da.InsertCommand.ExecuteNonQuery();

            con.Close();
            clear();


        }

hope u can help me , thanks :)
On Thursday, January 05, 2012 6:30:54 AM Imar Spaanjaars said:
Hi there,

You're executing this code even before the data is inserted. What you want to do instead is execute that code after the insert. You probably want to use RegisterStartUpScript as explained here: http://msdn.microsoft.com/en-us/library/asz8zsxy.aspxcheers,

Imar
On Thursday, January 05, 2012 7:36:34 AM za said:
o i c :) by the way thanks a lot yaa .
i'll try it . if i have any problem i'll contact you again ;)
On Tuesday, May 13, 2014 4:14:59 AM Melina said:
hi imar, what if i want to show alert message when the user first come into the page and when the users reload the page without clicking on any buttons ?
There will be some if statement to compare 2columns in sql server.
please help me to solve this problem. thank you
On Tuesday, May 13, 2014 10:00:20 AM Imar Spaanjaars said:
Hi Melina,

Detecting a refresh can be tricky when you're not causing a postback. You may find what you need in this article:

http://devproconnections.com/aspnet/trap-browser-refresh

Cheers,

Imar
On Tuesday, May 13, 2014 12:18:27 PM melina said:
hi imar, thank you for ur reply.
but is possible to do it without any button?
because mine is like every time the user come to the page then it will refer to some sql command that comparing 2 column and make alert message from it.
On Tuesday, May 13, 2014 1:30:19 PM Imar Spaanjaars said:
Did you read the article I linked to? It doesn't use a button.

What exactly are you trying to accomplish? Can you elaborate as currently I don't understand the problem.

Imar
On Tuesday, May 13, 2014 2:36:30 PM Melina said:
Yaa, i read it already. as i can see , it uses 2 buttons which is click and refresh.
opps, so sorry
hmmm
actually i want every time users enter the page, the alert message will come out. But with one condition where the product amount in database is less than its safetystockamount. so the alert message will be like : 'the stock limit has been reached!'

actually i tried to do it already but i didnt get the desired output. I refer to ur explanation above to it.

im so sorry making u confused with my question@@
please suggest me other solution for this. thank you
On Tuesday, May 13, 2014 3:30:57 PM Imar Spaanjaars said:
If you want this to happen every time, why not just add the message to Page_Load?

Imar
On Tuesday, May 13, 2014 11:43:05 PM Melina said:
yaa,i want it to come out every time but with one condition where where the product amount in database is less than its safetystockamount. So, if there is no any product that reached its limit then this alert message will not come out.

below is my code but it is not working:
    protected void Page_Load(object sender, EventArgs e)
    {
Page page = HttpContext.Current.Handler as Page;
        SqlConnection connection = new SqlConnection("Data Source=MELINA_YEO-VAIO;Initial Catalog=InventorySystem;Integrated Security=True");
      
        if
            (page != null)
        {
            connection.Open();
            SqlCommand abc = new SqlCommand("Select case when (P_Amount <= SafetyStockamount) then 'Limit stock has been reached' end as Alert from Product");
      


            string message = "Limit stock has been reached!";

            ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + message + "');", true);

        }
    }
}
On Wednesday, May 14, 2014 8:10:47 AM Imar Spaanjaars said:
Oh, so this is no related to the Alert box, but to SQL right? Your SQL seems off and you're never executing the command object so this would never work. Did you try running the SQL statement against SQL Server directly in Management Studio? Also, why are you getting a reference to the page when you are already in a page?

This is getting a bit too much out of scope for me to answer here. May I suggest you post this in a forum such as http://p2p.wrox.com and supply more details about your complete page, your database table and the scenario you're trying to implement? That makes it a lot easier to post code and provide more details than as comments here.

Cheers,

Imar
On Wednesday, May 14, 2014 9:03:28 AM melina said:
yes actually. hemm, what do you mean with 'Your SQL seems off and you're never executing the command object so this would never work'?? i tried to execute my sqlcommand in my sql server and the result shows the stock condition of each product. for the product with good stock level then the result is null while the product with lesser amount then the safety stock the result will be 'limit amount has been reached!'.

ya i will try it there. thank you  for ur recommendation. So it means that i cannot follow urs code above and just input sql command between them also?@@
i think because it needs to refer to db so it is hard to do, right??
im quite new in this field so i lack of knowledge about it @@
sorry for my poor english ><
On Wednesday, May 14, 2014 10:29:11 AM Imar Spaanjaars said:
Your code never executes the SQL command or deals with the result. Just declaring a SqlCommand doesn't automatically execute the underlying SQL statement.

I replied to your post on the Wrox forum with a link to a ADO.NET tutorial that shows you how to work with objects like the SqlCommand.

Cheers,

Imar

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.