[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

how to validate ASP .NET controls for data bound controls?

Hello, I need to validate some controls on a web form. But they are associated with data bound controls. How can I validate these controls? I have used asp.net validations controls before, but for this case I am stuck. In the code snippet below is my code. Can someone please help me out?

I am using .net 3.5 with VS 2008
<%@ Page Title="" Language="VB" MasterPageFile="~/EMRMasterPage.master" AutoEventWireup="false" CodeFile="AddCustomer.aspx.vb" Inherits="AddCustomer" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <form id="form1" runat="server" style="background-color: #FFFFEE" CLASS="controlaligner" >
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="TempUserID" DataSourceID="SqlDataSource1" 
        BackColor="#3399FF" BorderColor="Silver" BorderStyle="Solid" >
        <EmptyDataRowStyle BorderStyle="None" />
        <Columns>
            <asp:BoundField DataField="TempUserID" HeaderText="TempUserID" 
                InsertVisible="False" ReadOnly="True" SortExpression="TempUserID" />
            <asp:BoundField DataField="UserFirstName" HeaderText="First Name" 
                SortExpression="UserFirstName" />
            <asp:BoundField DataField="UserLastName" HeaderText="Last Name" 
                SortExpression="UserLastName" />
            <asp:BoundField DataField="txtHomeCity" HeaderText="City" 
                SortExpression="txtHomeCity" />
            <asp:BoundField DataField="txtHomeState" HeaderText="State" 
                SortExpression="txtHomeState" />
            <asp:BoundField DataField="txtHomeZip" HeaderText="Zip" 
                SortExpression="txtHomeZip" />
            <asp:BoundField DataField="userExpire" HeaderText="Expire Date" 
                SortExpression="userExpire" />
        </Columns>
        <EmptyDataTemplate>
            No Active
        </EmptyDataTemplate>
    </asp:GridView>
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
         DataKeyNames="TempUserID" DataSourceID="SqlDataSource1" 
        DefaultMode="Insert" Height="50px" Width="333px" GridLines="None">
        <Fields>
            <asp:BoundField DataField="TempUserID" HeaderText="TempUserID" 
                InsertVisible="False" ReadOnly="True" SortExpression="TempUserID" />
            <asp:BoundField DataField="UserFirstName" HeaderText="First Name" 
                SortExpression="UserFirstName" />
            <asp:BoundField DataField="UserLastName" HeaderText="Last Name" 
                SortExpression="UserLastName" />
            <asp:BoundField DataField="userEmail" HeaderText="Email" 
                SortExpression="userEmail" />
            <asp:BoundField DataField="userPhone" HeaderText="Phone" 
                SortExpression="userPhone" />
            <asp:BoundField DataField="useraltemail" HeaderText="Email" 
                SortExpression="useraltemail" />
            <asp:BoundField DataField="useraltphone" HeaderText="Phone" 
                SortExpression="useraltphone" />
            <asp:BoundField DataField="intUserHorbitzID" HeaderText="intUserHorbitzID" 
                SortExpression="intUserHorbitzID" />
            <asp:BoundField DataField="txtHomeAddress" HeaderText="Address" 
                SortExpression="txtHomeAddress" />
            <asp:BoundField DataField="txtHomeCity" HeaderText="City" 
                SortExpression="txtHomeCity" />
            <asp:BoundField DataField="txtHomeState" HeaderText="State" 
                SortExpression="txtHomeState" />
            <asp:BoundField DataField="txtHomeZip" HeaderText="Zip" 
                SortExpression="txtHomeZip" />
            <asp:BoundField DataField="userExpire" HeaderText="Expire Date " 
                SortExpression="userExpire" />
            <asp:CommandField InsertText="Save" ShowEditButton="True" ButtonType="Button"  
                ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:EMRConnectionString %>" 
        DeleteCommand="DELETE FROM [tbl_sideUser] WHERE [TempUserID] = @TempUserID" 
        InsertCommand="INSERT INTO [tbl_sideUser] ([UserFirstName], [UserLastName], [userEmail], [userPhone], [useraltemail], [useraltphone], [intUserHorbitzID], [txtHomeAddress], [txtHomeCity], [txtHomeState], [txtHomeZip], [userExpire]) VALUES (@UserFirstName, @UserLastName, @userEmail, @userPhone, @useraltemail, @useraltphone, @intUserHorbitzID, @txtHomeAddress, @txtHomeCity, @txtHomeState, @txtHomeZip, @userExpire)" 
        SelectCommand="SELECT [TempUserID], [UserFirstName], [UserLastName], [userEmail], [userPhone], [useraltemail], [useraltphone], [intUserHorbitzID], [txtHomeAddress], [txtHomeCity], [txtHomeState], [txtHomeZip], [userExpire] FROM [tbl_sideUser]" 
        UpdateCommand="UPDATE [tbl_sideUser] SET [UserFirstName] = @UserFirstName, [UserLastName] = @UserLastName, [userEmail] = @userEmail, [userPhone] = @userPhone, [useraltemail] = @useraltemail, [useraltphone] = @useraltphone, [intUserHorbitzID] = @intUserHorbitzID, [txtHomeAddress] = @txtHomeAddress, [txtHomeCity] = @txtHomeCity, [txtHomeState] = @txtHomeState, [txtHomeZip] = @txtHomeZip, [userExpire] = @userExpire WHERE [TempUserID] = @TempUserID">
        <DeleteParameters>
            <asp:Parameter Name="TempUserID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserFirstName" Type="String" />
            <asp:Parameter Name="UserLastName" Type="String" />
            <asp:Parameter Name="userEmail" Type="String" />
            <asp:Parameter Name="userPhone" Type="String" />
            <asp:Parameter Name="useraltemail" Type="String" />
            <asp:Parameter Name="useraltphone" Type="String" />
            <asp:Parameter Name="intUserHorbitzID" Type="Int32" />
            <asp:Parameter Name="txtHomeAddress" Type="String" />
            <asp:Parameter Name="txtHomeCity" Type="String" />
            <asp:Parameter Name="txtHomeState" Type="String" />
            <asp:Parameter Name="txtHomeZip" Type="String" />
            <asp:Parameter Name="userExpire" Type="DateTime" />
            <asp:Parameter Name="TempUserID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="UserFirstName" Type="String" />
            <asp:Parameter Name="UserLastName" Type="String" />
            <asp:Parameter Name="userEmail" Type="String" />
            <asp:Parameter Name="userPhone" Type="String" />
            <asp:Parameter Name="useraltemail" Type="String" />
            <asp:Parameter Name="useraltphone" Type="String" />
            <asp:Parameter Name="intUserHorbitzID" Type="Int32" />
            <asp:Parameter Name="txtHomeAddress" Type="String" />
            <asp:Parameter Name="txtHomeCity" Type="String" />
            <asp:Parameter Name="txtHomeState" Type="String" />
            <asp:Parameter Name="txtHomeZip" Type="String" />
            <asp:Parameter Name="userExpire" Type="DateTime" />
        </InsertParameters>
    </asp:SqlDataSource>
    </form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
 
</asp:Content>

Open in new window

0
brgdotnet
Asked:
brgdotnet
1 Solution
 
kirihimeteCommented:
what specifically do you need to validate here? do you need to validate data values in the gridview?
0
 
brgdotnetAuthor Commented:
I need to validate the data bound fields. Let me give you a specific example from the code snippet I posted. If you look at the code in design view, you will see several text boxes. For example, "First Name"

I need to validate that field to make sure the application user is prevented from submitting invalid characters such as numeric values or special characters for the persons first name.
So that is a specific example.
0
 
Bob LearnedCommented:
That sounds like you need a RegularExpressionValidator to ensure only valid characters are entered.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
kirihimeteCommented:
0
 
informaniacCommented:
Basically u'll have to use Template columns


<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
     <%# Eval("UserFirstName")%>
</ItemTemplate>
<EditItemTemplate>
    <asp:TextBox ID="txtUserFirstName" runat="Server" CssClass="flattxt" MaxLength="10" Text='<%#Bind("UserFirstName") %>'></asp:TextBox>
.... Place ur validation control over here.....
 
<asp:RegularExpressionValidator id="reg1" runat="server" ControlToValidate="txtUserFirstName"                                                            >
</EditItemTemplate>

Open in new window

0
 
brgdotnetAuthor Commented:
This is the best article on the web that will show how it needs to be done. I appreciate the replies above but they were minimaly helpful. Nevertheless, I appreciate the help and will award the points.

http://www.asp.net/Learn/data-access/tutorial-19-vb.aspx
Go here:
http://www.asp.net/Learn/data-access/tutorial-19-vb.aspx

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now