2014-02-26 55 views
0

我只是ASP.NET的初學者。現在我有2頁:主頁和子頁面(內容頁面)。在子頁面上,我創建了一個html表單,用於將用戶輸入提交給服務器。我使用asp按鈕來提交這些,但它只是通過運行我的JavaScript驗證用戶的輸入。底部的標籤用於檢查是否回發。即使頁面在點擊提交按鈕後刷新,它總是顯示「False」。我不知道我在這裏做錯了什麼。另外,我想在提交給服務器之後在文本框中顯示用戶的所有信息。希望你能幫我解釋清楚。非常感謝你。ASP.NET POSTBACK在contentplaceholder中不起作用

1 /母版頁:

2 /子頁面(內容頁):

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server"> 
<div class="row"> 
     <div class="box"> 
      <div class="col-lg-12"> 
       <hr> 
       <h2 class="intro-text text-center">Contact <strong>Mon Ami Cafe Restaurant</strong> 
       </h2> 
       <hr> 
      </div> 
      <div class="col-md-8" id="map-canvas"> 
       <!-- Embedded Google Map using an iframe - to select your location find it on Google maps and paste the link as the iframe src. If you want to use the Google Maps API instead then have at it! --> 
       <!--iframe width="100%" height="400px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=14291+S+Euclid+St,+Garden+Grove,+CA+92843&amp;aq=&amp;sll=33.754949,-117.938489&amp;sspn=0.010437,0.021136&amp;ie=UTF8&amp;hq=&amp;hnear=14291+S+Euclid+St,+Garden+Grove,+California+92843&amp;t=m&amp;z=14&amp;ll=33.754949,-117.938489&amp;output=embed"></!--iframe><br /><small><a href="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=14291+S+Euclid+St,+Garden+Grove,+CA+92843&amp;aq=&amp;sll=33.754949,-117.938489&amp;sspn=0.010437,0.021136&amp;ie=UTF8&amp;hq=&amp;hnear=14291+S+Euclid+St,+Garden+Grove,+California+92843&amp;t=m&amp;z=14&amp;ll=33.754949,-117.938489" style="color:#0000FF;text-align:left">View Larger Map</a></small--> 
      </div> 

      <div class="col-md-4"> 
       <h5>Phone:</h5> 
       <p><strong>*******</strong> 
       </p> 
       <h5>Email:</h5> 
       <p><strong>*******</strong> 
       </p> 
       <h5>Address:</h5> 
       <p><strong>*******</strong> 
       </p> 
      </div> 
      <div class="clearfix"></div> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="box"> 
      <div class="col-lg-12"> 
       <hr> 
       <h2 class="intro-text text-center">Contact 
       </h2> 
       <hr> 

       <form name="ContactForm" method="post"> 
        <div class="row" id="ContactForm"> 
         <div class="form-group col-lg-4"> 
          <label>Name</label> 
          <input type="text" id="NAME" class="form-control"> 

         </div> 
         <div class="form-group col-lg-4"> 
          <label>Email Address</label> 
          <input type="text" id="EMAIL" class="form-control"> 

         </div> 
         <div class="form-group col-lg-4"> 
          <label>Phone Number</label> 
          <input type="text" id="PHONE" class="form-control"> 

         </div> 
         <div class="clearfix"></div> 
         <div class="form-group col-lg-12"> 
          <label>Message</label> 
          <textarea id="MSG" class="form-control" rows="6"></textarea> 

         </div> 
         <div class="form-group col-lg-12"> 
          <input type="hidden" name="save" value="contact"> 
          <asp:Button ID="btnSubmit" runat="server" OnClientClick="return validateForm();" Text="Submit"/> 
         </div> 
        </div> 
       </form> 
      </div> 
     </div> 
    </div> 

    <!--The output information will be here --> 
    <p><asp:Label id="lbl1" runat="server" /></p> 
</asp:Content> 

這裏是我的腳本@Chia ...:

<script> 
    function validateName() { 
     var x = document.getElementById("NAME").value; 
     if (x == null || x == "") { 
      alert("Name must be filled out"); 
      return false; 
     } 
     else 
      return true; 
    } 

    function validateEmail() { 
     var y = document.getElementById("EMAIL").value; 
     var atpos = y.indexOf("@"); 
     var dotpos = y.lastIndexOf("."); 
     if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= y.length) { 
      alert("Not a valid e-mail address"); 
      return false; 
     } 
     else 
      return true; 
    } 

    function validatePhone() { 
     var formatForm = /^[1-9]\d{9}$/; 
     var z = document.getElementById("PHONE").value; 
     if (z.length == 0) { 
      alert("Phone must be filled out"); 
      return false; 
     } 
     else if (z.length < 10 || z.length > 10 || !(z.match(formatForm))) { 
      alert("Not a valid phone number"); 
      return false; 
     } 
     else 
      return true; 
    } 

    function validateMess() { 
     var t = document.getElementById("MSG").value; 
     if (t == null || t == "") { 
      alert("Pleave leave your message"); 
      return false; 
     } 
     else 
      return true; 
    } 

    function validateForm() { 
     if (validateName()) { 
      if (validateEmail()) { 
       if (validatePhone()) { 
        if (validateMess()) { 
         //alert("Submitted Sucessfully"); 
         return true; 
        } 
       } 
      } 
     } 
     return false; 
    } 
</script> 
+0

set autopostback =「true」 –

+0

您是否在CodeBehind中編寫代碼以處理單擊PostBack上的按鈕?你的MasterPage不是有一個Form標籤嗎? – Alexander

+0

@ChiragSutariya我應該在哪裏設置該功能? – user3355041

回答

1

您需要添加一個onclick屬性給你的asp按鈕,它在你的代碼後面調用一個事件。例如

<asp:Button ID="btnSubmit" runat="server" OnClientClick="return validateForm();" OnClick="btnSubmit_Click" Text="Submit"/> 

這將觸發事件

protected void btnSubmit_Click(object sender, System.EventArgs e) 
{ 
    // Do stuff here after postback 
} 

因爲你已經連接好您的OnClientClick屬性和ValidateForm返回一個布爾值,那麼這將正確地只允許onclick事件來觸發,如果validateForm函數返回true。

+0

這是正確的答案。 – naveen