Javascript Confirmation box not working from code behing
-
I have a checkbox in my page which is autopostback true. If the user clicks on the checkbox, I want to display a confirmation text box in javascript, and if the user clicks on Ok, then only the postback should occur, otherwise not. Even though the js is working and confirmation box displays, no postback is happening. Below is my code: Javascript code:
function ReportConfirmation() {
if (confirm("Proceeding with Output File Generation ? ")) { return true; } else { return false; } }
<asp:CheckBox runat="server" ID="chkGenerateRpt" AutoPostBack="true" OnCheckedChanged="chkGenerateRpt_CheckedChanged" Text="Generate Reports"/>
Code-Behind in PageLoad (outside the IsPostBack):
chkGenerateRpt.Attributes.Add("OnClick", "javascript:return ReportConfirmation(); ");
protected void chkGenerateRpt_CheckedChanged(object sender, EventArgs e)
{
if (chkGenerateRpt.Checked)
{
// do some stuff
}
}If i remove return from the pageload code above, the page will work irrespetive of whether the user clicks on cancel or Ok button in confirmation box? Could anybody please help in finding out the error? Thanks in advance
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have a checkbox in my page which is autopostback true. If the user clicks on the checkbox, I want to display a confirmation text box in javascript, and if the user clicks on Ok, then only the postback should occur, otherwise not. Even though the js is working and confirmation box displays, no postback is happening. Below is my code: Javascript code:
function ReportConfirmation() {
if (confirm("Proceeding with Output File Generation ? ")) { return true; } else { return false; } }
<asp:CheckBox runat="server" ID="chkGenerateRpt" AutoPostBack="true" OnCheckedChanged="chkGenerateRpt_CheckedChanged" Text="Generate Reports"/>
Code-Behind in PageLoad (outside the IsPostBack):
chkGenerateRpt.Attributes.Add("OnClick", "javascript:return ReportConfirmation(); ");
protected void chkGenerateRpt_CheckedChanged(object sender, EventArgs e)
{
if (chkGenerateRpt.Checked)
{
// do some stuff
}
}If i remove return from the pageload code above, the page will work irrespetive of whether the user clicks on cancel or Ok button in confirmation box? Could anybody please help in finding out the error? Thanks in advance
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
I checked the page source and found that: if you enable
AutoPostBack
for a check box, anonclick
handler is automatically attached to the page. Now when you add anotheronclick
event to the page both of them are concatenated and looks like this -<input id="chkGenerateRpt" type="checkbox" name="chkGenerateRpt" onclick="javascript:return ReportConfirmation(); ;setTimeout('__doPostBack(\'chkGenerateRpt\',\'\')', 0)" /><label for="chkGenerateRpt">Generate Reports</label>
See the bold part. Thus it is not working. Now how we can overcome this problem: This thread describes the possible solutions - http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-web-controls/4449/Checkbox-AutoPostBack-JavaScript[^] I will paste the solution here as well - 1. Manualy create a new checkBox class which derived from the asp.net's CheckBox control and override the render method so as to insert our own script before the build in "AutoPostBack" script (__doPostBack) 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls namespace and we can add a serverchange event for it and also handler its clientside "onclick" script event. But we need to manually post back the page when user click it at clientside, for example:
<input type="checkbox" id="chkClient" runat="server" value="..."
name="chkClient" onclick="alert('hello');document.forms[0].submit();"
onserverchange="chkServer_CheckedChanged">in page's codebehind we have :
protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
{
Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
}I hope this clears your doubt and solves your issue! :thumbsup: Cheers! Ankur
..Go Green..
-
I checked the page source and found that: if you enable
AutoPostBack
for a check box, anonclick
handler is automatically attached to the page. Now when you add anotheronclick
event to the page both of them are concatenated and looks like this -<input id="chkGenerateRpt" type="checkbox" name="chkGenerateRpt" onclick="javascript:return ReportConfirmation(); ;setTimeout('__doPostBack(\'chkGenerateRpt\',\'\')', 0)" /><label for="chkGenerateRpt">Generate Reports</label>
See the bold part. Thus it is not working. Now how we can overcome this problem: This thread describes the possible solutions - http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-web-controls/4449/Checkbox-AutoPostBack-JavaScript[^] I will paste the solution here as well - 1. Manualy create a new checkBox class which derived from the asp.net's CheckBox control and override the render method so as to insert our own script before the build in "AutoPostBack" script (__doPostBack) 2. Use the InputCheckBox control in the System.Web.UI.HtmlControls namespace and we can add a serverchange event for it and also handler its clientside "onclick" script event. But we need to manually post back the page when user click it at clientside, for example:
<input type="checkbox" id="chkClient" runat="server" value="..."
name="chkClient" onclick="alert('hello');document.forms[0].submit();"
onserverchange="chkServer_CheckedChanged">in page's codebehind we have :
protected void chkServer_CheckedChanged(object sender, System.EventArgs e)
{
Response.Write("<br>CheckChanged at: " + DateTime.UtcNow.ToString());
}I hope this clears your doubt and solves your issue! :thumbsup: Cheers! Ankur
..Go Green..
-
Thank you Ankur. I think the second one is the best option. Thanks a lot !!! :rose:
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.