javascript - Avoid Postback when calling code behind -


so i'm trying input validation on asp form , if validation fails it's not supposed try , submit form show modal window (bootstrap) error , let user fix error/fill in blanks forgot.

but whenever press html button (or asp button i've tried both) shows modal window , postback right after. can see happens because have upload fields lose file references , have dynamically created dropdown resets.

i find way around after looking @ several answers havne't found solution.

using popular return false; solution makes submit button stop working together.

my html:

<div class="row">     <script>         function activityadd() {             __dopostback('activity_add', 'postback');         };     </script>     <asp:button cssclass="btn btn-success" style="font-size: 20px;" runat="server" onclientclick="activityadd();" text="submit"/>     <a class="btn btn-danger" runat="server" href="~/index" style="font-size: 20px;">cancel</a> </div> 

c# page_load:

protected void page_load(object sender, eventargs e) {     scriptmanager.registerstartupscript(this, typeof(page), "createdepartmentdropdown", "$(document).ready(function(){createdepartmentdropdown(" + getdepartmentdropdownlist() + ");});", true);     scriptmanager.registerstartupscript(this, typeof(page), "registerdatepickers", "$(document).ready(function(){registerdatepickers();});", true);     //activity_add.attributes.add("onclick", "return false;");     //activity_add.click += activity_submit_click;     if (request["__eventargument"] == "postback")     {         submitactivity();     } } 

and submit method:

public bool submitactivity() {     bool inputvalidated = true;     list<string> errormessages = new list<string>();      int fye = int32.parse(fye_dropdown.value);     string activityname = activity_name_field.value;     string[] ax_accounts = (ax_account_numbers_field.value.contains(',') ? ax_account_numbers_field.value.split(',') : new string[1] { ax_account_numbers_field.value });      if (activityname.length == 0)     {         inputvalidated = false;         errormessages.add("the activity name not filled.");     }     string activity_responsible = responsible_field.value;     int department;      if (department_dropdown_selected_value.value.length == 0)     {         department = 0;     }     else     {         department = int32.parse(department_dropdown_selected_value.value);     }      datetime start;     datetime end;      // since dates formatted americans rearrange day , month in code.     // otherwise javascript checks 2 calendars break , can't parse datetime object.     try     {         string[] date = datepicker_start.value.split('/');         string parsestring = date[1] + "/" + date[0] + "/" + date[2] + " 00:00:00 am";         start = datetime.parse(parsestring);     }     catch (exception)     {         inputvalidated = false;         errormessages.add("the start date not formatted right. please click in box , choose date calendar.");     }      try     {         string[] date = datepicker_start.value.split('/');         string parsestring = date[1] + "/" + date[0] + "/" + date[2] + " 00:00:00 am";         end = datetime.parse(parsestring);     }     catch (exception)     {         inputvalidated = false;         errormessages.add("the end date not formatted right. please click in box , choose date calendar.");     }      if (ax_accounts[0].length == 0)     {         inputvalidated = false;         errormessages.add("you need add @ least 1 ax account activity.");     }      if (!description_upload.hasfile)     {         inputvalidated = false;         errormessages.add("please choose file upload detailed description of activity");     }      if (!estimation_upload.hasfile)     {         inputvalidated = false;         errormessages.add("please choose file upload estimation of activity.");     }      if (inputvalidated == false)     {         stringbuilder sb = new stringbuilder();         sb.append("an error happened while submitting activity. please see below details.");         sb.append("<br>");         foreach (string msg in errormessages)         {             sb.append("- ").append(msg).append("<br>");         }         string jsexec = util.modalalert(sb.tostring(), "#error_modal", ".modal-body");         scriptmanager.registerstartupscript(page, gettype(), "error_modal_show", jsexec, false);         return false;     }     else     {         byte[] descriptionbytes = description_upload.filebytes;         string descriptionfilename = description_upload.filename;          byte[] estimationbytes = estimation_upload.filebytes;         string estimationfilename = estimation_upload.filename;          string msg = util.alert("success");         response.write(msg);         return true;     } } 

but doesn't work expected either. fields mentioned earlier still reset. it's infuriating me no end because it's going frustrating experience user. idea on how approach problem?


Comments