asp.net的验证控件出现问题 [英] Problem with the validation controls of asp.net

查看:96
本文介绍了asp.net的验证控件出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将验证控件放在我的网页中..

这样,当我单击添加产品"按钮时.我可以在验证摘要中显示错误..

但是问题是,如果我在页面上单击任何其他链接或按钮以转到其他页面..这将迫使我在我验证的文本框上写..

我希望他们仅对添加产品按钮起作用.对我单击的其他链接不起作用.
我该怎么办..这是代码.

i put validations controls in my webpage..

so that when i click Add Product button . I can show Errors in Validation summary..

but the problem is that if on the page i click on any other link or button to go to some other page.. it will force me to write on the textboxes which i validate..

i want them to work only for add product button.. not for the other links i click.
how can i do it.. here is the code.

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
    .style4
    {
        width: 100%;
    }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        Add new Products:</p>
<uc1:nav ID="nav1"  runat="server" />
<p align="center">
         </p>
<p align="center">
        <asp:Label ID="UploadStatusLabel" runat="server"></asp:Label>
</p>
<table class="style4">
    <tr>
        <td>
            Category Name:</td>
        <td>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px" Width="170px">
        </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            Product Name:</td>
        <td>
        <asp:TextBox ID="txtproname" runat="server" Width="174px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtproname" 
            ErrorMessage="Please Enter Product Name" Display="Dynamic">*</asp:RequiredFieldValidator>
        
        </td>
    </tr>
    <tr>
        <td>
            Product Image:</td>
        <td>
        <asp:FileUpload ID="FileUpload1" runat="server" Width="218px" />
        </td>
    </tr>
    <tr>
        <td>
            Product Price:</td>
        <td>
        <asp:TextBox ID="txtproprice" runat="server" Width="138px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Description :</td>
        <td>
        <asp:TextBox ID="txtprodesc" runat="server" Height="50px" Width="267px" 
            TextMode="MultiLine"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtprodesc" 
            ErrorMessage="Enter Product Description" Display="Dynamic">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Original Price</td>
        <td>
            <asp:TextBox ID="txtOrigional" runat="server" Width="136px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            Stock Quantity</td>
        <td>
            <asp:DropDownList ID="dpquantity" runat="server">
                <asp:ListItem>0</asp:ListItem>
                <asp:ListItem>10</asp:ListItem>
                <asp:ListItem>20</asp:ListItem>
                <asp:ListItem>50</asp:ListItem>
                <asp:ListItem>100</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td colspan="2">
<p align="center">
         <asp:Button ID="btnaddproduct" runat="server" onclick="btnaddproduct_Click" 
            Text="Add Product" Width="115px" />
    
        </p>
        </td>
    </tr>
    <tr>
        <td align="center" colspan="2">
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        </td>
    </tr>
</table>
    <p align="center">
        <asp:Label ID="lblresult" runat="server"></asp:Label>
    </p>
    <p align="center">
         </p>
    <p align="center">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" onrowcancelingedit="_rowcancel" 
            onrowdeleting="_rowdeleting" onrowediting="_rowediting" 
            onrowupdating="_rowupdating" Width="446px" 
            onpageindexchanging="_pagechange" HorizontalAlign="Center" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="ProductID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtproid" runat="server" Text='<%# Eval("ProductID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblproid" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ProductName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtproname" runat="server" Text='<%# Eval("ProductName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblproname" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UnitCost">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtproprice" runat="server" Text='<%# Eval("UnitCost") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblproprice" runat="server" Text='<%# Eval("UnitCost") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
    </p>
    <p align="center">
         </p>
    <p align="center">
         </p>
    <p>
         </p>
    <p>
         </p>
    <p>
         </p>
</asp:Content>

推荐答案

设置 ^ ]属性设置为false ,以便您可以执行操作而无需执行验证.
Set the CausesValidation[^] property of the control to false so that you can perform action without validation is performed.


您需要将属性CausesValidation添加到您不想受到验证影响的控件中,并将其设置为 False .

那应该做到的.

问候,
爱德华
You need to add the attribute CausesValidation to those controls you do not want to be affected for validation and set it to False.

And that should do it.

Regards,
Eduard


这篇关于asp.net的验证控件出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆