c# - Postback Issue with Javascript Popup -
i know there lot of questions similar one, don't see quite same. have aspx page , using javascript show confirmation popup. when clicking on button, value click isn't used instead previous value is. if click ok first time cancels. if click ok second time goes ok, if click cancel still goes ok, click cancel once again , goes cancel. i'm sure issue postback, don't know how resolve since i'm not calling javascript on button click, later on. use code behind determine if record duplicate , if show popup letting user know, , giving them option accept or cancel. if there no duplicate script popup doesn't called. how resolve issue?
javascript in aspx
function confirm() { var confirm_value = document.createelement("input"); confirm_value.type = "hidden"; confirm_value.name = "confirm_value"; if (confirm("duplicate record. want save?")) { confirm_value.value = "yes"; } else { confirm_value.value = "no"; } document.forms[0].appendchild(confirm_value); }
this code cs, called method after testing conditions.
scriptmanager.registerstartupscript(this, base.gettype(), "confirmation", "confirm();", true); string confirmvalue = request.form["confirm_value"]; if (confirmvalue == "yes") { this.page.clientscript.registerstartupscript(this.gettype(), "alert", "alert('you clicked yes!')", true); } else { this.page.clientscript.registerstartupscript(this.gettype(), "alert", "alert('you clicked no!')", true); }
note: using autopostback several fields on same page data entry form , depending on selected 1 field depends on how field populated.
here 1 way it.
in markup, can add hidden field monitor if duplicate data must overwritten. in example below, btnsavedata
button may present in form.
<asp:hiddenfield id="hiddenoverwritedata" runat="server" value="false" /> <asp:button id="btnsavedata" runat="server" text="save data" onclick="btnsavedata_onclick" />
the event handler of button click should check if data exists. if so, client script registered; if not, new data saved.
protected void btnsavedata_onclick(object sender, eventargs e) { // check if data record exists bool isduplicate = ... if (isduplicate) { scriptmanager.registerstartupscript(this, this.gettype(), "confirmsave", "confirmsavedata();", true); } else { dosavedata(); } } private void dosavedata() { // save data ... }
the javascript function asks confirmation. if user confirms, value of hidden field set true
, form submitted again; if not, saving operation canceled.
<script type="text/javascript"> function confirmsavedata() { if (confirm("duplicate data. want overwrite?")) { var hiddenoverwritedata = document.getelementbyid('<%= hiddenoverwritedata.clientid %>'); hiddenoverwritedata.value = 'true'; form1.submit(); } } </script>
in page_load
, check if user has confirmed duplicate data should overwritten, in case data saved , hidden field value reset.
protected void page_load(object sender, eventargs e) { if (hiddenoverwritedata.value == "true") { hiddenoverwritedata.value = "false"; dosavedata(); } }
Comments
Post a Comment