2013-01-22 61 views
0

我在asp.net新;我嘗試添加RequiredFieldValidator到電話號碼部分,但我不能在Visual Studio 2010,因爲它沒有「id」,當我在類中添加id時,它仍然沒有顯示。Asp.net驗證與div

另外我怎樣才能在提交按鈕上添加onclick?用一個RequiredFieldValidator所示

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="odevproje._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <fieldset> 
    <div class="legend"><h3>iletişim</h3></div> 
     <div class="form_row">      
    <div class="form_property form_required">Adınız</div> 
    <div class="form_value"><input type="text" size="32" name="name" value="" />      
      </div>  
     <div class="clearer">&nbsp;</div> 
     </div> 
     <div class="form_row">      
      <div class="form_property">Email</div> 
      <div class="form_value"><input type="text" size="32" name="email" value="" /></div> 
      <div class="clearer">&nbsp;</div> 
      </div> 
     <div class="form_row">     
      <div class="form_property">Telefon</div>         <div class="form_value"><input type="text" size="32" name="website" value="" /></div> 
      <div class="clearer">&nbsp;</div> 
     </div>    
     <div class="form_row">     
      <div class="form_property form_required">Mesaj</div> 
       <div class="form_value"><textarea rows="10" cols="46" name="comment"></textarea></div> 
        <div class="clearer">&nbsp;</div> 
     </div> 
      <div class="form_row form_row_submit"> 
     <div class="form_value"><input type="submit" class="button" value="Send"/> 
      <div class="clearer">&nbsp;</div> 
      </div>   
     </fieldset> 
    </div> 
    </form> 
</body> 
</html> 
+1

您輸入字段均服務器控件(RUNAT =「服務器」),並使用RequiredFieldValidator也是一個服務器控件,只能驗證服務器控件。 – Matt

+0

你能編輯我的密碼嗎?我不知道如何去做。 – Kaan

+0

[這裏是一個簡單的教程](http://asp.net-tutorials.com/validation/required-field-validator/) – markpsmith

回答

1
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="odevproje._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    <fieldset> 

     <div class="legend"><h3>iletişim</h3></div> 

      <div class="form_row"> 

       <asp:TextBox runat="server" id="emailTextBox"/> 
       <asp:RequiredFieldValidator runat="server" ControlToValidate="emailTextBox" Text="*" ErrorMessage="Email is required" /> 

      </div> 

      <asp:Button runat="server" id="saveButton" Text="Save" /> 

    </fieldset> 
    </div> 

    </form> 
</body> 
</html> 

僅有一個字段。

0

您需要使用asp.net服務器端控件,它在運行時創建html。

,例如,你可以使用:

<asp:textbox ID='phonenumber' runat='server'/> 
<asp:RequiredFieldValidator ID="ValidateName" 
    ControlToValidate="phonenumber"       
    runat="server" ErrorMessage="Phone Number is required is Required" 
    Display=Dynamic> 
</asp:RequiredFieldValidator>