Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
<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>
Posted

Set the CausesValidation[^] property of the control to false so that you can perform action without validation is performed.
 
Share this answer
 
v3
Comments
Monjurul Habib 11-Dec-11 21:13pm    
5! CauseValidation :)
Wonde Tadesse 11-Dec-11 21:14pm    
Thanks
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
 
Share this answer
 
Comments
Monjurul Habib 11-Dec-11 21:13pm    
5!
[no name] 11-Dec-11 21:30pm    
thanks!
Wonde Tadesse 11-Dec-11 21:14pm    
5+
[no name] 11-Dec-11 21:30pm    
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900