Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

disable validation on a single control .net

  • 08-08-2008 11:07am
    #1
    Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭


    hi all,

    just wondering would anyone be able to help i have an asp.net web form with multiple controls all using validation.. each field is required to allow the user to register online.. but alternatively i have an option to print the entire screen with the controls filled in so the user may post in the data instead.. here is my problem one of the controls is a file upload and obviously this can not be printed.. so i need to disable this control i presume in the btn.click function so that the form is valid when all fields are filled in except the upload field.. but i cant just use the causesvalidation=false as this will render all validation disabled.. any help would be great my code is below...


    <!-- the print link button-->

    <asp:LinkButton
    runat="server"
    id="btnPrintScreen"
    text="PRINT FORM>>"
    cssclass="hyperlink_pdfX">
    </asp:LinkButton>


    <!--the upload field and validator-->

    <asp:FileUpload
    ID="fupFileUpload1"
    runat="server"
    Width="220px" />
    <asp:RequiredFieldValidator
    runat="server"
    CssClass="rfvStyle"
    ControlToValidate="fupFileUpload1"
    id="rfvFileUpload" />


    <!--the code behind the button-->

    Private Sub btnPrintScreen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintScreen.Click

    Page.RegisterStartupScript("Startup", "<script language=""javascript"" type=""text/javascript"">window.onload=printPage;</script>")

    End Sub


    <!--the javascript function-->

    ffunction printPage() {

    if (window.print) {

    if(navigator.userAgent.toLowerCase().indexOf('mozilla') != -1)

    {

    if(navigator.userAgent.toLowerCase().indexOf('msie') !=-1)

    {

    // much better browser, so do nothing

    }

    else

    {

    if(parseInt(navigator.appVersion)>3 && parseInt(navigator.appVersion)<5)

    {

    alert('you are using nav4 so print format not supported');

    }

    }

    }

    window.print();

    } else {

    alert("Sorry, your browser doesn't support this feature.");

    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    This may work for you.

    If you disable client side validation on the rfvFileUpload control when the print button is pressed, then the form will be submitted. As .net requires you to specifically check if the control is valid, you can still do your processing.

    This is the C# example:
     protected void btnPrintScreen_Click(object sender, EventArgs e)
        {
            rfvFileUpload.EnableClientScript = false; 
            rfvFileUpload.Text = "";
            if (!rfvFileUpload.IsValid)
            {
                Response.Write("control was not valid, but submitting the form anyway");
                Page.RegisterStartupScript("Startup", "<script language=\"javascript\" type=\"text/javascript\">window.onload=printPage;</script>");
            }        
        }
    


  • Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭Stab*City


    thanks a million eoin_s im gonna convert this to VB and try!! ill let you know how i get on!!


  • Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭Stab*City


    tried this but no luck..


    Public Sub btnPrintScreen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintScreen.Click

    Dim ClientScript



    rfvFileUpload.EnableClientScript = false

    rfvFileUpload.Text = ""

    if not rfvFileUpload.IsValid Then

    Response.Write("control was not valid, but submitting the form anyway")

    ClientScript.RegisterStartupScript("Startup", "<script language=\"javascript\" type=\"text/javascript\">window.onload=printPage;</script>")

    End If



    End Sub


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    You're still unable to submit the form?

    Here are the full files I tested this with in case I missed something:

    ASPX:
    <&#37;@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
        function printPage() {
    
    if (window.print) {
    
    if(navigator.userAgent.toLowerCase().indexOf('mozilla') != -1)
    
    {
    
    if(navigator.userAgent.toLowerCase().indexOf('msie') !=-1)
    
    {
    
    // much better browser, so do nothing
    
    }
    
    else
    
    {
    
    if(parseInt(navigator.appVersion)>3 && parseInt(navigator.appVersion)<5)
    
    {
    
    alert('you are using nav4 so print format not supported');
    
    }
    
    }
    
    }
    
    window.print();
    
    } else {
    
    alert("Sorry, your browser doesn't support this feature.");
    
    }
    
    }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            &nbsp;<!--the upload field and validator--><asp:FileUpload
    ID="fupFileUpload1"
    runat="server"
    Width="220px" />
    <asp:RequiredFieldValidator
    runat="server"
    CssClass="rfvStyle"
    ControlToValidate="fupFileUpload1"
    id="rfvFileUpload" EnableClientScript="False" >*</asp:RequiredFieldValidator>
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                ErrorMessage="*"></asp:RequiredFieldValidator><br />
        <asp:LinkButton
    runat="server"
    id="btnPrintScreen"
    text="PRINT FORM>>"
    cssclass="hyperlink_pdfX" OnClick="btnPrintScreen_Click">
    </asp:LinkButton>
            <asp:Button ID="Button1" runat="server" Text="Button" /></div>
        </form>
    </body>
    </html>
    
    

    Code Behind:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnPrintScreen_Click(object sender, EventArgs e)
        {
            rfvFileUpload.EnableClientScript = false;
            rfvFileUpload.Text = "";
            if (!rfvFileUpload.IsValid)
            {
                Response.Write("control was not valid, but submitting the form anyway");
                Page.RegisterStartupScript("Startup", "<script language=\"javascript\" type=\"text/javascript\">window.onload=printPage;</script>");
            }        
        }
    }
    
    


  • Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭Stab*City


    no the form submits fine.. but when i click the print form button is still throws up the rfv error for the upload control... basically i need the upload validated when submit button is pressed.. but when print form button is pressed i need the validation turned off..


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    Weird - just did exactly the same page in VB and it doesn't work. I'll have another look.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    It looks like VB and C# are behaving differently.

    Is removing all client side validation from the controls an option for you? I did this using VB and the error message still appears, even after I set the text of the validator to nothing. Can't figure out why. However, I added a line in the top of the JavaScript to hide the validator span tag from view.

    VB Page:
    <&#37;@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript">
        function printPage() {
    
    document.getElementById("rqFile").style.display = "None";
    
    if (window.print) {
    
    if(navigator.userAgent.toLowerCase().indexOf('mozilla') != -1)
    
    {
    
    if(navigator.userAgent.toLowerCase().indexOf('msie') !=-1)
    
    {
    
    // much better browser, so do nothing
    
    }
    
    else
    
    {
    
    if(parseInt(navigator.appVersion)>3 && parseInt(navigator.appVersion)<5)
    
    {
    
    alert('you are using nav4 so print format not supported');
    
    }
    
    }
    
    }
    
    window.print();
    
    } else {
    
    alert("Sorry, your browser doesn't support this feature.");
    
    }
    
    }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>    
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
            <asp:RequiredFieldValidator ID="rqTXT" ControlToValidate="TextBox1" runat="server" ErrorMessage="*" EnableClientScript="False"></asp:RequiredFieldValidator>
            <br />
            <asp:FileUpload ID="FileUpload1" runat="server" /><asp:RequiredFieldValidator ID="rqFile"
                runat="server" ErrorMessage="*" ControlToValidate="FileUpload1" EnableClientScript="False"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="btnPrint" runat="server" Text="print" />
            <asp:Button ID="btnSubmit" runat="server" Text="submit" />
                
        </div>
        </form>
    </body>
    </html>
    

    code behind:
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            Page.RegisterStartupScript("Startup", "<script language=""javascript"" type=""text/javascript"">window.onload=printPage;</script>")
            rqFile.EnableClientScript = False
            Response.Write("test: " & rqFile.Text & "<BR>")
            rqFile.Text = ""
            Response.Write("test: " & rqFile.Text & "<BR>")
    
            If Not (rqFile.IsValid) Then
                Response.Write("File upload not populated, but do print script")
            End If
    
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    End Class
    
    


  • Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭Stab*City


    hi there thanks for all your help.. disabling clientside validation is not an option so here is what i did.. i removed the rfv and set the text of a label to display error text after btn_submit is clicked if there is no file present.. this then solved the problem of no validation on the upload when a user tries to print the page without the upload file.. again thank you very very much for all your help!!


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    No problems at all, for what it is worth. Strange that there appeared to be differences in how VB and C# handled it.


  • Registered Users, Registered Users 2 Posts: 2,413 ✭✭✭Stab*City


    ya strange.. but that one of the best things about this stuff more than one way to do everything.. now im off home.. enjoy your weekend!!


  • Advertisement
Advertisement