Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag 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 + ")");
Avatar of guru_sami
guru_sami
Flag of United States of America image

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

Avatar of 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" />
Avatar of 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...
I tried this document.getElementById(chkResponseLetter).ClientID
but i get undefined
tried this too but chkResponseLetter is undefined
var test = self.document.getElementById("<%= chkResponseLetter.ClientID %>");
I can get the listview like this
var grid = document.getElementById("<%= upListview.ClientID %>");

now, i need to reference the checkbox inside that listview...
Did you try adding Attributes onchange (as leakim971 suggested) via code-behind, like the onclick in your original post?
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
Avatar of Camillia
Camillia
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 + ")");
the other 1 solutions didnt work. thanks