簡體   English   中英

如何將HTML參數傳遞給ASP.NET?

[英]How to pass HTML parameters to ASP.NET?

我正在使用Bootstrap(HTML&CSS),Microsoft Access和ASP.NET進行Web服務項目。

我從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();
        }
    }

}

當我運行頁面並輸入用戶名和密碼時,頁面不顯示消息,並且無法使用用戶名進行連接。

放置一個斷點,檢查代碼背后發生的情況以及您收到的值。

另外,嘗試使用onserverclick代替onclick

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

並在behine中

txt.value=@Your

您的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的函數,並且應該引用Web服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM