Avatar of Camillia
Camillia
Flag for United States of America asked on

Explain solution :Code does a postback even tho Javascript returns false

This is the solution (seems like it) to my issue below but I dont know how to implemet it. If you scroll down, the developer explains a hidden field
http://forums.asp.net/t/1789189.aspx/1



I have the code below and I put "alert" and I get "false" when I cancel the popup msg. It should just cancel and not call the code but not sure why it's calling the code. This is what I have:

protected void lvOutgoing_OnItemDataBound(object sender, ListViewItemEventArgs e)
    { 
        ....
        chkResponseLetter.Attributes.Add("onclick", "rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ")");
    } 

Open in new window


CheckBox

<asp:CheckBox runat="server" AutoPostBack="true"  OnCheckedChanged="chkResponseLetter_checked" title="Check if you have received a Response letter"  ID="chkResponseLetter" />

Open in new window


Javascript

<script type="text/javascript">
         function rdoClick(click, toHCProviderId) {
                         var msg = confirm("Have you verified you have received the Response letter?");
             alert(msg); //*** I get false here

             if (msg) {//if yes Clicked

                // do whatever

                 return true;
             }
             else
                 return false;

             
         }

    </script>

Open in new window


I tried this but with this...when user clicks on cancel...there's no post back. BUT, it breaks the "ok" functionality. Even when OK is clicked, there's no post back but there should be when user clicks ok

chkResponseLetter.Attributes.Add("onclick", "return rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ")");
ASP.NETJavaScript

Avatar of undefined
Last Comment
Camillia

8/22/2022 - Mon
guru_sami

Try this:
chkResponseLetter.Attributes.Add("onclick", "return rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ");");

function rdoClick(click, toHCProviderId) {
            var msg = confirm("Have you verified you have received the Response letter?");
            alert(msg); //*** I get false here

            if (msg) {//if yes Clicked

                // do whatever
     <%=Page.ClientScript.GetPostBackEventReference(chkResponseLetter,chkResponseLetter.ClientID)%>;
                return true;
            }
            else
                return false;


        }

Open in new window

leakim971

Try this :
<script type="text/javascript">
   window.onload = function() {
        document.getElementById("<%= chkResponseLetter.ClientID %>").onchange = function() {
                    return confirm("Have you verified you have received the Response 
        }
   }
</script>

Open in new window

With :
<asp:CheckBox runat="server" AutoPostBack="true" title="Check if you have received a Response letter"  ID="chkResponseLetter" />
Camillia

ASKER
The checkbox is inside a listview so I cant use chkResponseLetter.ClientID or chkResponseLetter to get to that element. I think there's an elementID something I can use to get to controls inside a listview/gridview in Javascript...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Camillia

ASKER
I tried this document.getElementById(chkResponseLetter).ClientID
but i get undefined
Camillia

ASKER
tried this too but chkResponseLetter is undefined
var test = self.document.getElementById("<%= chkResponseLetter.ClientID %>");
Camillia

ASKER
I can get the listview like this
var grid = document.getElementById("<%= upListview.ClientID %>");

now, i need to reference the checkbox inside that listview...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
guru_sami

Did you try adding Attributes onchange (as leakim971 suggested) via code-behind, like the onclick in your original post?
Camillia

ASKER
Yes, I have the onclick attribute but my problem is this line chkResponseLetter.ClientID

chkResponseLetter is inside a listview and Javascript cant find it.

Now, I did this per that solution I posted in my orig post:

1. Added a hidden field
<asp:HiddenField ID="checkBoxStatus" runat="server" ClientIDMode="Static" />

2. I changed the Javascript to below
 if (msg) {//if yes Clicked
                  var msg = confirm("Have you verified you have received the Response letter?");
                   document.getElementById("checkBoxStatus").value = true;
                 return true;
             }
             else {
                 document.getElementById("checkBoxStatus").value = false;
                 return false;
             }

Open in new window


and In code behind, i have this
 
if (checkBoxStatus.Value == "false")
            return;

Open in new window


This works but the checkbox stays checked when user clicks cancel. I want to clear the checkbox when user clicks cancel
ASKER CERTIFIED SOLUTION
Camillia

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Camillia

ASKER
I think I got it. The link above in 39041832, removing the autopostback, and changing the onclick to this

chkResponseLetter.Attributes.Add("onclick", "return rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ")");
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Camillia

ASKER
the other 1 solutions didnt work. thanks