验证文本框,以使值与另一个文本框不同

时间:2015-05-14 09:47:58

标签: c# asp.net validation

我想验证textbox,以便Text的{​​{1}}值与firstNameTextBox不同。我使用nicknameTextBox的{​​{1}}属性,就像这样 -

InitialValue

以下是代码:

RequiredFieldValidator

但是,使用此代码后,验证无法正常工作,只有在第二次单击按钮后才能正常工作。我还尝试将所有 fieldValidatorInitialValue.InitialValue = firstNameTextBox.Text; 代码放到 RequiredFieldValidator fieldValidatorInitialValue = new RequiredFieldValidator(); TextBox firstNameTextBox = new TextBox(); TextBox nicknameTextBox = new TextBox(); protected void Page_Load(object sender, EventArgs e) { Button submitButton = new Button(); submitButton.CausesValidation = true; submitButton.Click += submitButton_Click; nicknameTextBox.ID = "nickname"; firstNameTextBox.ID = "firstname"; fieldValidatorInitialValue.ControlToValidate = firstNameTextBox.ID; } protected void submitButton_Click(object sender, EventArgs e) { fieldValidatorInitialValue.InitialValue = nicknameTextBox.Text; } 事件处理程序中,但在这种情况下它根本不起作用,有人可以帮我解决吗?

3 个答案:

答案 0 :(得分:2)

试试这个。你应该使用CompareValidator而不是RequiredFieldValidator,运算符=“NotEqual”

 <asp:CompareValidator runat="server"
                  ControlToValidate="tbFName"
                  ControlToCompare="tbLName"
                  Type="String"
                  Operator="NotEqual"
                  ErrorMessage="First and last name cannot be the same" />

答案 1 :(得分:0)

尝试以下代码

protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
         Button submitButton = new Button();
         submitButton.CausesValidation = true;
         submitButton.Click += submitButton_Click;
         nicknameTextBox.ID = "nickname";
         firstNameTextBox.ID = "firstname";
         fieldValidatorInitialValue.ControlToValidate = firstNameTextBox.ID;
       }
    }

答案 2 :(得分:0)

  

我认为你试图比较“名字”和“尼克”的方式   名称“不必要地复杂。           而不是你可以简单地使用CompareValidator并在单击按钮上实现相同的任务。在这里,将此添加到您的   设计师页面:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.05.14 at 04:02:08 PM IST 
//


package generated;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}dependanttaskId" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "dependency")
public class Dependency {

    @XmlElementRef(name = "dependanttaskId", type = JAXBElement.class, required = false)
    @XmlMixed
    protected List<Serializable> content;

    /**
     * Gets the value of the content property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the content property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getContent().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link String }
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * 
     * 
     */
    public List<Serializable> getContent() {
        if (content == null) {
            content = new ArrayList<Serializable>();
        }
        return this.content;
    }

}
  

如果要动态添加Validator,请添加CompareValidator        而不是RequiredFieldValidator并添加以下属性:

 <asp:CompareValidator ID="comNames" runat="server"
     ControlToValidate="firstNameTextBox"
     ControlToCompare="nicknameTextBox"
     ErrorMessage="Error: Name cant be same"
     SetFocusOnError="True" Text="*"></asp:CompareValidator>   
  

注意:如果您使用ValidationGroup作为按钮,请使用相同的名称   ValidationGroup for Validator也是。

相关问题