0

I have a program that dynamically creates various text boxes / drop down lists. I am trying to figure out how to validate these fields only when one is changed. Basically if someone entered a date in the text box then I need the program to validate that the drop down list was changed or vice versa. If there are no changes to both fields then it should not validate. Any help would be extremely appreciated. Here is the code:

<asp:TemplateField HeaderText="ValidatedDate" SortExpression="ValidatedDate">
    <EditItemTemplate>
        <asp:TextBox ID="txtValDate" Width="100px" MaxLength="10" runat="server" AutoPostBack="true"
            Text='<%# Bind("ValidatedDate","{0:MM/dd/yyyy}") %>'></asp:TextBox>
        <asp:RegularExpressionValidator ValidationGroup="g1" ID="RegularExpressionValidator10"
            runat="server" ControlToValidate="txtValDate" Display="None" ErrorMessage="Validated Date: ##/##/####"
            ValidationExpression="\d{1,2}/\d{1,2}/\d{4}"></asp:RegularExpressionValidator>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblValidatedDate" runat="server" Text='<%# Bind("ValidatedDate","{0:MM/dd/yyyy}")%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductStatus" SortExpression="ProductStatusDescription">
    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource6" AutoPostBack="true"
            DataTextField="StatusDescription" DataValueField="StatusID" SelectedValue='<%# Bind("Status") %>'>
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblProductStatus" runat="server" Text='<%# Bind("ProductStatusDescription")%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

My apologies, the code can be a bit confusing without the correct context.

4

1 回答 1

0

ebyrob,谢谢你的帮助。然而,我想通了,它工作得很好。这是修复所有问题的代码:

    protected void AddError(string errorMessage)
    {
        cstValidate.IsValid = false;
        cstValidate.ErrorMessage = errorMessage;
        cstValidate.EnableClientScript = false;
    }

    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtValidatedDate = GridView2.Rows[e.RowIndex].FindControl("txtValDate") as TextBox;
        DropDownList ddlStatusCompare = GridView2.Rows[e.RowIndex].FindControl("dropdownlist4") as DropDownList;
        if (txtValidatedDate.Text == string.Empty && ddlStatusCompare.SelectedValue == "1")
        {
            AddError("Please enter a Validated Date");
            e.Cancel = true;
        }
        else if (txtValidatedDate.Text != string.Empty && ddlStatusCompare.SelectedValue == "0" 
            || txtValidatedDate.Text != string.Empty && ddlStatusCompare.SelectedValue == "99")
        {
            AddError("Please remove the Validated Date");
            e.Cancel = true;
        }
        if (!e.Cancel)
            Helpers.LogChanges(GridView2, e, _employeeID, "SaleProducts");
    }
于 2013-08-29T21:57:19.403 回答