2014-01-28 132 views
0

有一個aspx頁面。當用戶點擊(+)添加電話號碼時,應該檢查並查看是否輸入了以前的號碼。如果有的話,那麼只有當另一個文本框被動態地添加來添加另一個電話號碼等等。然而,它不會在(+)的按下時觸發驗證觸發器,因爲它允許頁面進行回發,儘管沒有輸入任何東西以及頁面執行回發時觸發我在其他控件上顯示的驗證觸發器以及相應的錯誤消息。如何避免這一點?如何避免在回發時觸發驗證觸發器

ASPX

<tr><td class="labels">Tel. No. (XXX-XXXX) </td> 
    <td class="tb"> 
    <asp:TextBox ID="PN1" runat="server" width="120px"></asp:TextBox> 
    <asp:ImageButton ID="ImageButtonAdd1" runat="server" ImageUrl="~/Styles/Images/add.jpg" 
    Height="16px" Width="23px" AlternateText="Add another Phone Number" 
    CausesValidation="False" Onclick="TelNum2_Click" /> 
    <asp:RegularExpressionValidator ID="RegularExpressionValidatorPN" runat="server" 
    ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX" ControlToValidate="PN1" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font-Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></tr> 




    <tr id="phoneNum2" runat="server"><td class="labels"> Tel. No 2. (XXX-XXXX)</td> 
    <td class="tb"><asp:TextBox ID="PN2" runat="server" Width="120px"></asp:TextBox> 
    <asp:ImageButton ID="ImageButtonAdd2" runat="server" ImageUrl="~/Styles/Images/add.jpg" 
    Height="16px" Width="23px" AlternateText="Add another Phone Number" CausesValidation="False" 
    Onclick="TelNum3" /> 
    <asp:RegularExpressionValidator ID="RegularExpressionValidatorPN2" runat="server" 
    ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX" 
    ControlToValidate="PN2" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font- 
    Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></td></tr> 

    <tr id="phoneNum3" runat="server"><td class="labels"> Tel. No 3. (XXX-XXXX)</td> 
    <td class="tb"><asp:TextBox ID="PN3" runat="server" Width="120px"></asp:TextBox> 
    <asp:RegularExpressionValidator ID="RegularExpressionValidatorPN3" runat="server" 
    ErrorMessage="Please enter a VALID Phone Number in the format XXX-XXXX" 
    ControlToValidate="PN3" Display="Dynamic" Font-Italic="True" ForeColor="#FF3300" Font- 
    Bold="True" ValidationExpression="\d{3}-\d{4}"></asp:RegularExpressionValidator></td></tr> 

CS

 protected void Page_Load(object sender, EventArgs e) 
    { 
     UpdatePanel1.Visible = true; 
     BtnNew.Visible = true; 
     BtnDelete.Visible = false;  
     BtnUpdate.Visible = false; 
     BtnSave.Visible = false; 
     BtnCancel.Visible = false; 
     pubvar.DisableAllControls(Page); 

     if (!Page.IsPostBack) 
     { 
      processAgentData.Visible = false; //area in which textboxes are displayed 
      phoneNum2.Visible = false; 
      phoneNum3.Visible = false; 
     } 
     else 
     { 
      processAgentData.Visible = true; 
     } 


    } 


    protected void TelNum2_Click(object sender, ImageClickEventArgs e) 
    { 
     try 
     { 
      if (PN1.Text.Trim().Length > 0) 
      { 
       phoneNum2.Visible = true; 
      } 
      else 
      { 
       phoneNum2.Visible = false; 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0} Exception caught.", ex); 
     } 
    } 
    protected void TelNum3(object sender, EventArgs e) 
    { 
     try 
     { 
      if (PN2.Text.Trim().Length > 0) 
      { 
       phoneNum3.Visible = true; 
      } 
      else 
      { 
       phoneNum3.Visible = false; 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0} Exception caught.", ex); 
     } 
    }  

回答

1

您需要設置CauseValidation = true您的圖像按鈕。

+0

其已經默認爲.. – New2This

+0

啊!我錯過了。雖然我已經更新了我的答案。您的控件應該啓動驗證頁面進行驗證。 –