2011-11-28 63 views
0

我有一个母版页,并基于该母版页,我创建了一个带有TextBox和两个验证控件的页面,用于RequiredValidatorRegularExpressionValidator和一个ValidationSummaryASP.NET输入键

按在文本框输入键的时候,我预计双方验证表明其对的ValidationSummary的errorMessage但那不是发生了什么,当我按下页面上的按钮,它才会起作用。

根据此页我用<asp:panel>DefaultButton属性包装了我的代码,但它没有解决问题。 http://www.codeproject.com/KB/aspnet/aspnet_Enter_key_problem.aspx

我想知道这里的问题是什么以及是否有任何解决方法?

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> 

    **<asp:Panel runat="server" DefaultButton="EmailSend">** 
    <table style="width: 100%"> 
     <tr> 
      <asp:ValidationSummary ID="EmailValidation" runat="server" CssClass="failureNotification" 
       ValidationGroup="EmailValidation" /> 
     </tr> 
     <tr> 
      <td style="width: 76px"> 
       Email: 
      </td> 
      <td class="style1"> 
       <asp:TextBox ID="EmailForget" runat="server" Width="244px"></asp:TextBox> 
      </td> 
      <td> 
       <asp:RequiredFieldValidator ID="EmailRequiered" runat="server" ControlToValidate="EmailForget" 
        CssClass="failureNotification" ErrorMessage="RequiredFieldValidator" ValidationGroup="EmailValidation">*</asp:RequiredFieldValidator> 
       <asp:RegularExpressionValidator ID="EmailRegular" runat="server" ControlToValidate="EmailForget" 
        CssClass="failureNotification" ErrorMessage="email required" 
        ValidationGroup="EmailValidation">*</asp:RegularExpressionValidator> 
      </td> 
     </tr> 
     <tr> 
      <td style="width: 76px"> 
       &nbsp; 
      </td> 
      <td class="style1"> 
       <asp:Button ID="EmailSend" runat="server" Text="send" Width="56px" ClientIDMode="Static" /> 
      </td> 
      <td> 
       &nbsp; 
      </td> 
     </tr> 
    </table> 
    **<asp:Panel>** 
</asp:Content> 
+0

你尝试过加入验证组到ASP:按钮呢? – Jacques

+0

您错过了您的ValidationSummary控件周围的​​标签,请先将其添加!在我真正的代码按钮,验证组 – Nalaka526

+0

@Jacques是的,我已经添加了ASP。谢谢。 – 1AmirJalali

回答

0

尝试增加该代码

<script type="text/javascript"> 

    if (document.addEventListener) {//if Firefox 
     document.addEventListener("keypress", fireFoxHandler, true); 
    } else { 
    document.attachEvent("onkeyup", ieHandler); 
    } 

    function fireFoxHandler(evt) {    
     if (evt.keyCode == 13) { 
      document.getElementById("EmailSend").click(); 
     } 
    } 

    function ieHandler(evt) { 
     if (evt.keyCode == 13) { 
      document.getElementById("EmailSend").click(); 
     } 
    } 

</script> 

找到此溶液here。 (我没有在母版的情况下,请检查这个......)

+0

@ Nalaka525非常感谢它,即使在MasterPage场景中也是如此。 – 1AmirJalali

+0

我发现这个http://forums.asp.net/t/1100563.aspx/1这就解释了为什么的ValidationSummary和Enter键不能一起工作。 – 1AmirJalali

0

我解决了这个问题就像个是:

protected void Page_Load(object sender, EventArgs e) 
{ 
textboxusername.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13)  __doPostBack('" + LinkButton1.UniqueID + "','')"); 
    textboxpassword.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + LinkButton1.UniqueID + "','')"); 
} 

,这是链接..

<asp:LinkButton ID="LinkButton1" runat="server" class="btn" OnClick="LinkButton1_Click"> 
+0

感谢您的回复,我试过我们的脚本,但它没有奏效。 我也在页面loadPage上添加这两行。 Form.DefaultButton = EmailSend.UniqueID; Page.Form.DefaultFocus = EmailForget.ClientID; 它也没有工作。 – 1AmirJalali