2013-04-02 25 views
0

我有這個UpdatePanelASP:UpdatePanel中只有工作時,其它輸入有效

<asp:ScriptManager ID="CheckManager" runat="server" /> 
    <asp:UpdatePanel ID="checkUsername" runat="server" UpdateMode="Conditional" > 
    <ContentTemplate> 
    <asp:Label ID="canUse" runat="server" Text=""/> 
    <asp:Button ID="check" runat="server" Text="Usable?" onclick="check_Click" /> 
    <asp:TextBox ID="username" runat="server" ></asp:TextBox> 
    </ContentTemplate> 
    <Triggers> 
    <asp:AsyncPostBackTrigger controlid="check" eventname="Click" /> 
    </Triggers> 
    </asp:UpdatePanel> 

更新代碼:

protected void check_Click(object sender, EventArgs e) 
    { 

     using (HotelEntities h = new HotelEntities()) 
     { 
      UserHandle u = new UserHandle(); 
      if (u.FindUserByUsername(h, username.Text.ToString()).Any()) 
      { 
       canUse.ForeColor = Color.Magenta; 
       canUse.Text = "You cannot use this username"; 
       username.Text = ""; 
      } 
      else 
      { 
       canUse.ForeColor = Color.Green; 
       canUse.Text = "you can use this username"; 
      } 
     } 
    } 

,但只能當其它形式輸入(即有驗證例如RequiredFieldValidator)是有效的。

我該如何解決這個問題?

+1

你能解釋一下_「只在_時纔有效嗎?我不知道什麼_works_或_works不是_的意思。 –

+0

@TimSchmelter「不起作用」表示不更新標籤 –

+1

您尚未顯示更新標籤(哪個標籤)的代碼。 –

回答

2

設置的CausesValidation =對於您不希望被點擊Button控件時執行驗證按鈕「假」

<asp:Button ID="check" CausesValidation="false" runat="server" 
Text="Usable?" onclick="check_Click" /> 

Button.CausesValidation Property:獲取或設置指示按鈕被點擊時控制是否執行驗證的值。

0

您需要將ValidationGroup屬性設置爲與按鈕和文本框驗證等相同。例如,您需要在textBox1附近使用字段驗證程序。你有兩個按鈕。如果您不使用ValidationGroup,那麼您將無法在未驗證輸入的情況下單擊其中的任何一個。 您可以 Check here看到一個例子

相關問題