简体   繁体   中英

How can I use a Modal Pop Up control in ASP.NET similar to a WinForms Message box?

I am building a form, where required field validation must have been checked. I am not using asp validator. I am using JQuery validator like

function checkRequiredInputs(){

$("#frmSaleSubmissionInfo").validate({  
    rules:{
        txtFName:{required: true},
        txtLName:{required: true},
        txtAddress:{required: true},
        txtPhone:{required: true}
    },
    messages:{
        txtFName:"Enter Name",
        txtLName:"Enter Name",
        txtAddress:"Enter Address",
        txtPhone:"Enter Phone Number"
    }
});

This is my client side validation. I am using c# in my code behind page. Now if I turned off allowjavascript option in my browser, as far as I know it won't allow javascript. So I am also doing server-side validation for required field. But, since ASP.NET does not have a message-box control, I am having trouble to let the user know what field he is keeping empty. Is there any way to use a control like the message box to show or let the user know what field(s) is/are required to fill the form successfully?

In my point of view, the best fitted to your requirement is using ASP.NET AJAX Toolkit ValidatorCallout control, which can help you to build such solution more fast way.

But if you don't want mixed up two javascript framework (ASP.NET AJAX and jQuery) than you can be expired here , where you can find a solution how to deal with a validation and jQuery.

If you want a modal popup, just send some javascript from the server code:

Response.Write(
    "<script type='text/javascript'>alert('A required field is missing.');</script>");

alert() is a javascript function. Response.Write() adds the <script> element to the end of the HTTP response (ie, the rendered HTML page).

A more elegant approach would be to use the ClientScriptManager to register a startup script:

protected void Page_Load(object sender, EventArgs e) {
    this.ClientScript.RegisterClientScriptBlock(this.GetType(), 
        "RequiredFieldValidationScript", 
        "alert('A required field is missing.');", 
        true);
}

The javascript code alert('A required field is missing.'); will be executed after the postback.

See also: http://www.4guysfromrolla.com/articles/021104-1.2.aspx

Without javascript, I can't imagine a simple way of recreating a modal type popup. You would have to go as to postback and re-render the page with a DIV blocking out the page and another div on top of it with the errors.

You can always pipe out the errors to a tag next to the submit button and call it a day though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM