Asp.net Validations Not working with confirmation field

I want to add a confirmation window. The following code shows a yes / no confirmation window, but it does not care about verification. If I made the company name field mandatory, it enters the entry, even if I did not enter the company name.

I called this method in the pageload event:

CreateConfirmBox(btnAddEnquiry, "Do You Really Want to Add ?"); 

Method Definitions:

 public void CreateConfirmBox(System.Web.UI.WebControls.Button btn, string strMessage) { btn.Attributes.Add("OnClick", "return confirm('" + strMessage + "');"); } 

aspx file

 <asp:Button ID="btnAddEnquiry" runat="server" BackColor="#0000FF" ForeColor="LightSlateGray" OnClick="btnAddEnquiry_Click" Text="Add Enquiry" Width="154px" /> 
+4
source share
1 answer

You do not process any checks in your code.

I highly recommend you use the .NET Validation controls. They are good and hardly need to write any code.

More info here; http://msdn.microsoft.com/en-us/library/bwd43d0x%28v=vs.100%29.aspx

Code example;

 <asp:TextBox ID="TextBox1" runat="server" /> <asp:RequiredFieldValidator IdD="RequiredFieldValidator2" ControlToValidate="TextBox1" Display="Static" Width="100%" runat="server"> * </asp:RequiredFieldValidator> <asp:Button ID="btn_Save" Text="Validate" OnClick="btnSave_Click" runat="server" /> 

If you do not want to implement validators and use only the confirmation field and thus retain the same functionality as you currently have, you can do this using the OnCientClick button, which will stop PostBack if the user clicks the No. button.

  <asp:Button ID="btnAddEnquiry" runat="server" BackColor="#0000FF" ForeColor="LightSlateGray" OnClick="btnAddEnquiry_Click" OnClientClient="javascript: return confirm('Do You Really Want to Add ?');" Text="Add Enquiry" Width="154px" /> 
+2
source

Source: https://habr.com/ru/post/1440717/


All Articles