Camillia
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:
CheckBox
Javascript
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.Attribut es.Add("on click", "return rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ")");
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 + ")");
}
CheckBox
<asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="chkResponseLetter_checked" title="Check if you have received a Response letter" ID="chkResponseLetter" />
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>
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.Attribut
Try this :
<asp:CheckBox runat="server" AutoPostBack="true" title="Check if you have received a Response letter" ID="chkResponseLetter" />
<script type="text/javascript">
window.onload = function() {
document.getElementById("<%= chkResponseLetter.ClientID %>").onchange = function() {
return confirm("Have you verified you have received the Response
}
}
</script>
With :<asp:CheckBox runat="server" AutoPostBack="true" title="Check if you have received a Response letter" ID="chkResponseLetter" />
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...
ASKER
I tried this document.getElementById(ch kResponseL etter).Cli entID
but i get undefined
but i get undefined
ASKER
tried this too but chkResponseLetter is undefined
var test = self.document.getElementBy Id("<%= chkResponseLetter.ClientID %>");
var test = self.document.getElementBy
ASKER
I can get the listview like this
var grid = document.getElementById("< %= upListview.ClientID %>");
now, i need to reference the checkbox inside that listview...
var grid = document.getElementById("<
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?
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
and In code behind, i have this
This works but the checkbox stays checked when user clicks cancel. I want to clear the checkbox when user clicks cancel
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;
}
and In code behind, i have this
if (checkBoxStatus.Value == "false")
return;
This works but the checkbox stays checked when user clicks cancel. I want to clear the checkbox when user clicks cancel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think I got it. The link above in 39041832, removing the autopostback, and changing the onclick to this
chkResponseLetter.Attribut es.Add("on click", "return rdoClick(" + lblPatientId.Text + "," + lblToHCProvider.Text + ")");
chkResponseLetter.Attribut
ASKER
the other 1 solutions didnt work. thanks
Open in new window