Solved

problem using confirm box on webform

Posted on 2004-09-01
18
286 Views
Last Modified: 2012-06-21
Hello all, i am trying to use a confirmbox on my webform, the problem is the code executes anyways no matter the outcome of the confirm, how can i pause the code till a decision is made on the confirm then continue from there?
Here is what i tried, if the value in a textfield is equal 123 pop up the confirm box and wait for a decision.
  Function recordcheck() As Boolean
        If txtMaint.Text = 123 Then
            Return False
        End If
        Return True
    End Function
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'button that adds the record
        If recordcheck() = False Then
            btnAdd.Attributes.Add("onclick", "if(confirm('A matching timecard entry exists, would you like to add this record anyways?')){('" + btnAdd.ClientID + "','');}else{return false}")
        End If
     end sub
0
Comment
Question by:tentavarious
  • 10
  • 7
18 Comments
 
LVL 10

Expert Comment

by:EBatista
ID: 11953858
hi tentavarious, use this approach:
btnAdd.Attributes.Add("onclick", "return confirm('A matching timecard entry exists, would you like to add this record anyways?")

OR this one:

Dim strMyClick As String = "<script language=JavaScript> function MyClick() {"
strMyClick += "return confirm('A matching timecard entry exists, would you like to add this record anyways?')
strMyClick += "}<"
strMyClick += "/"
strMyClick += "script>"

If (Not IsClientScriptBlockRegistered("scriptMyClick")) Then
        RegisterClientScriptBlock("scriptMyClick", strMyClick)
End If

btnAdd.Attributes.Add("onclick","MyClick()")

regards
0
 

Author Comment

by:tentavarious
ID: 11954256
Would i put that in my recordcheck function? and return false if record exists?
0
 

Author Comment

by:tentavarious
ID: 11954335
I still think the rest of my code will execute, i have more lines of code beneath the function call in the sub procedure, i didnt post them, i need for the rest of the lines not to execute if the user clicks cancel on the confirm box.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11956418
well, this should work:
btnAdd.Attributes.Add("onclick", "return confirm('A matching timecard entry exists, would you like to add this record anyways?")

the differences between my code and your is the 'return' keyword you miss, without it, the rest of the code will execute whatever the user hits ok or cancel button from the confirm box. just try it and let me know
0
 

Author Comment

by:tentavarious
ID: 11956969
Nope, didn't work, the confirm never pops up and the rest of the code gets executed. Below is how i used it in my codebehind page.  The only way i have gotten the confirmbox to work before, is when i added the attribute to the button on the page load before the btnAdd button gets clicked, but if i do that with this confirm box it will show up every time the button is clicked.
 
Function recordcheck() As Boolean
        If txtMaint.Text = 123 Then
            btnAdd.Attributes.Add("onclick", "return confirm('A matching timecard entry exists, would you like to add this record anyways?")
        End If
    End Function

'button that adds the record
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        recordcheck()
  'extra code here
 end sub
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11959412
i see what you wanna do, in such cases it is better to do the validation on the client side and prevent or grant the server side event execution using the returnValue from the client side event:

function MyClick() {
if (document.getElementById('txtMaint').value<>123)
{
        //the server click event will be executed
        event.returnValue=true;
 }
else //the value is 123
{
       //prevent the server side click event only if the user hit cancel.    
       event.returnValue=return confirm('A matching timecard entry exists, would you  like to add this record anyways?');
 }


lastly you have to add the attribute on the page_load:
btnAdd.Attributes.Add("OnClick", "MyClick()")

hope this help
Elio


0
 

Author Comment

by:tentavarious
ID: 11962973
Using the txtmaint = 123 is just an example, what acctually needs to happen is i will use oleDbreader to read through a access database table and check if the record exists already, depending on 3 columns in the database table, and i don't know how to read from a database using javascript.  That's why i am trying to do it in codebehind.  Anyways if i add that attribute on the page load, won't it pop up everytime the button is clicked, i only need it to popup if the record exists.  I'm lost on how to do this.  I did get it to work if i added the attribute after the page load and before the button click, but it doesnt always work.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11967390
you have to consider another way, i prefer using panels that i hide and show accordingly and that could be your solution here, ie, you have one panel with all the controls the user need (textbox, btnSave, etc), and another one hidden by default that have two buttons (OK and Cancel), then when the user fill in the controls and hit btnAdd you will check the db for coincidence, if you find one then hidde the first panel and show the second one to let the user make their choise.

personally i dont use popups boxes cos that ruins user experience.
regards
0
 

Author Comment

by:tentavarious
ID: 11967468
i got it to work with a function and a new button, i use a javascript popup to show the user what occured works fine now.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:EBatista
ID: 11967972
so you have two button on the same page, one for record checking and another for insert?
0
 

Author Comment

by:tentavarious
ID: 11968099
Yep, but the one button only gets displayed if a duplicate record is found.  This is probably the easist way i could do it, i just gives the user one final chance to check his or her record entry.
'check record function
  Function recordcheck() As Boolean
        If lblrecord.Text = "1" Then
            Return True
        End If
        Dim sSQL As String
        Dim dr As New DataSet
        sSQL = "SELECT * FROM TimeCards1 WHERE Maint_Num =" & txtMaint.Text & " and Dept_Num = '" & txtDept.Text & "' and Hours = " & txtHours.Text & " and Clock = '" & txtClock.Text & "' and (Date_Time = cDate('" & txtDate.Text & "'))"
        Dim dataobj As New OleDbDataAdapter(sSQL, strConn)
        dataobj.Fill(dr, "TimeCards1")
        If dr.Tables(0).Rows.Count >= 1 Then
            Return False
        End If
        Return True
    End Function

' add record subroutine
 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'button that adds the record
        If recordcheck() = False Then
            btnAdd.Enabled = False
            btncontinue.Visible = True
            lblMess.Text = "A similiar record exists, check to make sure the same record was not entered twice" + "" + "\nClick Continue to resume, then add if you would like to add the record"
            Me.RegisterStartupScript("alert", _
                                                          "<script language='javascript'>alert('" & lblMess.Text & "'); </script>")
            Exit Sub
        End If
'additional code here that adds the record
end sub

'new button
Private Sub btncontinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncontinue.Click
        lblrecord.Text = "1"
        btnAdd.Enabled = True
    End Sub
0
 
LVL 10

Expert Comment

by:EBatista
ID: 11969889
mmm...but what if the user dont want to continue and he/she just want to modify the data in the boxes, and hit btnAdd again, at that point btnAdd will be disable.
i still think that you should provide them with the 'ok' and 'cancel' options.
0
 

Author Comment

by:tentavarious
ID: 11996287
I think i will add a cancel button
0
 

Author Comment

by:tentavarious
ID: 11996307
Like i said before i dont know how to read records from a database using javascript, the txtmaint = 123 was just an example.
0
 

Author Comment

by:tentavarious
ID: 11999436
If there is a way, that i can read and access files from an MSAccess database with javascript your idea would be a lot better but since i have to use vb.net language i don't think i can get it to work correctly, because it has to determine if the record exists before it can add the attribute to the button, and the only way to tell if a record  exist is to click the same button first.
0
 
LVL 10

Accepted Solution

by:
EBatista earned 250 total points
ID: 12000699
dont use javascript, like i said, draw two server panels, one with the controls and another with the ok and cancel button, just hide them and show them accordingly.
if you need more clarification or code let me know
regards
0
 

Author Comment

by:tentavarious
ID: 12005982
Thanks i will take that approach instead, it would be so much easier if you could use the msgbox method with webforms, i wonder if they will incorporate that with the next version.
0
 
LVL 2

Expert Comment

by:waterhidden
ID: 12006130

still you have to add attribute to a button in button's prerender, btn.attributes.add(bulabula) doesn't work.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now