2015-09-07 196 views
-2

我是C#,ASP.NET中的初學者。 我想要的是當點擊「提交」按鈕時,需要顯示一條消息'您的註冊成功'。 但是,當我點擊這裏,沒有任何顯示,但它顯示其他電腦相同的代碼的消息。 有人可以幫助我嗎?我的項目中是否有配置錯誤?在c中顯示消息#

代碼在這裏:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Windows.Forms; 
public partial class Registration : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

} 
protected void submit_Click(object sender, EventArgs e) 
{ 
    Response.Write("Your registration is succesful"); // Not displaying 
    //MessageBox.Show("Test Display"); 
} 

}

ASP.NET

<form id="form1" runat="server"> 
    <div> 

    </div> 
    <table class="style1"> 
     <tr> 
      <td class="style3"> 
       Username</td> 
      <td class="style6"> 
       <asp:TextBox ID="uname" runat="server" Width="180px"></asp:TextBox> 
      </td> 
      <td class="style8"> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="uname" ErrorMessage="Username is required" ForeColor="Red"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       Email</td> 
      <td class="style6"> 
       <asp:TextBox ID="email" runat="server" Width="180px"></asp:TextBox> 
      </td> 
      <td class="style8"> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ControlToValidate="email" ErrorMessage="Email is required" ForeColor="Red"></asp:RequiredFieldValidator> 
       <br /> 
       <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="email" ErrorMessage="You must enter the valid email id" 
        ForeColor="Red" 
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       Password</td> 
      <td class="style6"> 
       <asp:TextBox ID="pass" runat="server" TextMode="Password" Width="180px"></asp:TextBox> 
      </td> 
      <td class="style8"> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
        ControlToValidate="pass" ErrorMessage="Password is required" ForeColor="Red"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       Confirm Password</td> 
      <td class="style6"> 
       <asp:TextBox ID="passr" runat="server" TextMode="Password" Width="180px"></asp:TextBox> 
      </td> 
      <td class="style8"> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
        ControlToValidate="passr" ErrorMessage="Confirm password is required" 
        ForeColor="Red"></asp:RequiredFieldValidator> 
       <br /> 
       <asp:CompareValidator ID="CompareValidator1" runat="server" 
        ControlToCompare="pass" ControlToValidate="passr" 
        ErrorMessage="Both passwords must be same" ForeColor="Red"></asp:CompareValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style3"> 
       Country</td> 
      <td class="style6"> 
       <asp:DropDownList ID="country" runat="server" Width="180px"> 
        <asp:ListItem>Select Country</asp:ListItem> 
        <asp:ListItem>USA</asp:ListItem> 
        <asp:ListItem>UK</asp:ListItem> 
        <asp:ListItem>GERMANY</asp:ListItem> 
        <asp:ListItem>FRANCE</asp:ListItem> 
        <asp:ListItem>INDIA</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
      <td class="style8"> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
        ControlToValidate="country" ErrorMessage="Select a country name" 
        ForeColor="Red" InitialValue="Select Country"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style4"> 
      </td> 
      <td class="style7"> 
       <asp:Button ID="submit" runat="server" Text="Submit" /> 
      </td> 
      <td class="style5"> 
       <input id="Reset1" type="reset" value="reset" /></td> 
     </tr> 
     <tr> 
      <td class="style2"> 
       &nbsp;</td> 
      <td class="style6"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
    </table> 
    </form> 
+1

'MessageBox'只對其運行的Web服務器是本地的,不應該與asp一起使用。net –

+1

您認爲應該在什麼電腦上顯示消息框?事實上,你相信這段代碼做了一些有用的事情,這表明你可能對ASP.NET的工作原理有一個概念上的問題。 ASP在* server *上運行代碼,* *將* text *發送給* client *。這個模型是否清晰? –

+1

「ASP.NET」中沒有'MessageBox.Show' – Rahul

回答

2

您需要了解服務器端和客戶端之間的區別。

服務器端是發生在服務器上的所有事情(例如ASP,ASP.NET,PHP),用於創建發送到瀏覽器的HTML。它還處理用戶提交回來的東西(回發)時瀏覽器返回的信息。

客戶端是一旦收到HTML或者用戶在頁面上點擊某個元素等操作時在瀏覽器上發生的所有事情。

你正在做的是試圖運行Windows應用程序樣式MessageBox.Show服務器......這是行不通的。

如果您希望瀏覽器顯示「警報」窗口(有點像MessageBox),那麼您需要將客戶端腳本發送到瀏覽器。試試這個...

protected void submit_Click(object sender, EventArgs e) 
{ 
    Response.Write("Your registration is succesful"); 
    var script = "window.alert('Test Display');"; 
    ClientScript.RegisterStartupScript(this.GetType(), "message", script, true); 
} 

按照由OP評論...

我的問題是的Response.Write( 「登記是成功的」);不工作

而不是使用Response.Write使用<asp:Literal>控制(它給你準確定位控制在你需要它的優勢),並設置它的.Text屬性(請記住,這將留在後回,所以你可能需要清除它)。

您也可以使用<asp:Label>,這不僅可以讓您定位它,還可以包含.CssClass.Style屬性以獲得更好的格式。

+0

感謝您的考慮和時間..真的很抱歉,我犯了一個很大的錯誤,我忘了刪除MessageBox.Show(「測試顯示」);我的問題是Response.Write(「您的註冊成功」);是不行的 –

1

如果您想顯示確認消息框,請嘗試在.aspx頁面上使用jquery。您可以添加功能,這樣

function confirmationAccept() { 
    return confirm("Accept?"); 
} 

而不是增加屬性的OnClientClick這樣的:

<asp:Button ID="addButton" runat="server" Text="Add" OnClientClick="return confirmationAccept()"> 
+0

真的很抱歉,我犯了一個大錯誤,我忘了刪除MessageBox.Show(「Test Display」);我的問題是Response.Write(「您的註冊成功」);不工作 –

0

您可以通過使用ClientScript.RegisterStartupScript顯示JavaScript的警告消息。

+0

真的很抱歉,我犯了一個大錯誤,我忘了刪除MessageBox.Show(「Test Display」);我的問題是Response.Write(「您的註冊成功」);不管用 –