2015-05-11 142 views
1

我正在使用Bootstrap(HTML & CSS),Microsoft Access和ASP.NET製作Web服務項目。如何將HTML參數傳遞給ASP.NET?

我發現了一個代碼W3Schools的引導登錄表單:

<body> 
 
    <form method="POST" action="Login.aspx"> 
 
     <div class="container"> 
 
      <h2>Login area</h2> 
 
      <form class="form-inline" role="form"> 
 
       <div class="form-group"> 
 
        <label for="email">UserName:</label> 
 
         <input type="text" class="form-control" id="username1" name="username" placeholder="Enter UserName"/> 
 
       </div> 
 

 

 
    <div class="form-group"> 
 
     <label for="pwd">Password:</label> 
 
      <input type="password" class="form-control" id="password1" name="password" placeholder="Enter password"/> 
 
    </div> 
 
    
 
    <button type="submit" onclick="SubmitForm" class="btn btn-default">Submit</button> 
 
    </form> 
 
    </div>

這是C#代碼:

protected void SubmitForm(object sender, EventArgs e) 
{ 
    if (Page.IsValid) 
    { 
     string name=string.Format("{0}",Request.Form["username"]); 
     string pass = string.Format("{0}", Request.Form["password"]); 

     int userId; 
     userId = LoginService.GetUserId(name, pass, 0, 1); 

     if (userId == 0) 
     { 
      MessageBoxShow(Page, "UserName does not exists."); 

     } 
     else 
     { 
      MessageBoxShow(Page, "You are successfully connected."); 
      Session["userId"] = userId.ToString(); 
      SalService.DeleteFromSal(); 
     } 
    } 

} 

當我運行的網頁,並輸入用戶名和密碼,頁面不顯示消息,我不能連接用戶名。

回答

0

放一個斷點,檢查代碼隱藏中發生了什麼,以及你接收到了什麼值。

此外,請嘗試使用onserverclick而不是onclick

0
<input id ="txt" runat="server" type="text"> 

和behine

[email protected] 
+0

我不明白我應該在後面寫什麼。 – Linki

0

你的HTML使用ASP服務器端控件未格式化,但你仍然可以得到它的工作。您必須將您的按鈕更改爲提交按鈕。

<input type="submit" value="OK"/> 

然後更改您的代碼,如下所示。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (IsPostBack) 
    { 
     string name=string.Format("{0}",Request.Form["username"]); 
     string pass = string.Format("{0}", Request.Form["password"]); 

     int userId; 
     userId = LoginService.GetUserId(name, pass, 0, 1); 

     if (userId == 0) 
     { 
      MessageBoxShow(Page, "UserName does not exists."); 

     } 
     else 
     { 
      MessageBoxShow(Page, "You are successfully connected."); 
      Session["userId"] = userId.ToString(); 
      SalService.DeleteFromSal(); 
     } 
    } 

} 

確保後面的代碼是一個名爲頁login.aspx的和應該被稱爲login.aspx.cs

應該有一個login.aspx的頁面,並應具有有效的頁面指令指點到後面的代碼。

您還需要一個MessageBoxShow函數,並且應引用Webservice。