2013-08-28 37 views
0

我有一个程序,动态创建各种文本框/下拉列表。我试图弄清楚如何只在更改时验证这些字段。基本上,如果有人在文本框中输入日期,那么我需要程序验证下拉列表是否已更改,反之亦然。如果两个字段都没有更改,则不应验证。任何帮助将不胜感激。下面是代码:验证动态创建的字段,当一个被更改

<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> 

我的道歉,该代码可以是一个有点混乱,没有正确的上下文。

+0

您是否在代码隐藏中生成了一些代码?因为这看起来像* .ascx片段。如果您在代码隐藏中添加控件,1)请向我们展示代码。 2)一定要在'Page_Init()'而不是'Page_OnLoad()'中执行,否则事件不会被绑定。 – 2013-08-28 20:44:41

+0

这里有代码隐藏,但是它处理这个信息被放入的gridview(分页/排序)。这是来自* aspx页面。我会展示更多代码,但随后它会变成敏感内容。我只是在我的智慧结束这一点。除了这一件事以外,一切都很完美。 – Chris

+0

事件是否在动态控件上触发:从不?只有当直接修改?不被解雇,因为你没有附加事件给他们?如果“从不”,请参阅我原来的评论,确保你不会改变页面结构比你应该... PS - 如果值得被“敏感”,应该值得写一个简单的自包含的例子从头到尾都会出现问题。 – 2013-08-28 20:56:00

回答

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"); 
    }