asp.net中通過form表單submit提交到后台的實例


 

前台<body>中的代碼:

<body>
    <div id="top"> </div>
        <form id="login" name="login" action="?Action=Login" method="post">
            <div id="center">
            <div id="center_left"></div>
            <div id="center_middle">
                <div class="user">
                    <label>用戶名:
                        <input type="text" name="UserName" id="UserName" />
                    </label>
                </div>
                <br />
                <div class="user">
                    <label>密 碼:
                        <input type="password" name="UserPassword" id="UserPassword" />
                    </label>
                </div>
            </div>
            <div id="center_middle_right"></div>
            <div id="center_submit">
                <div class="button"> <img alt="" id="loginin" src="images/dl.gif" width="57" height="20" onclick="document.login.submit()"/> </div>
                <div class="button"> <img alt="" id="loginreset" src="images/cz.gif" width="57" height="20" onclick="document.login.reset()"/> </div>
            </div>
            <div id="center_right"></div>
            </div>
        </form>
    <div id="footer"></div>
</body>

 

通過圖片的點擊事件,執行form.submit()傳遞form中的參數。

后台cs代碼:

protected string Action = "";
   myBaseClass myData = new myBaseClass();
   protected class UserLoginInfo
   {
       public string UserName = "";
       public string UserPassword = "";
   }
   protected UserLoginInfo _UserLoginInfo = new UserLoginInfo();//創建對象
 
   protected void Page_Load(object sender, EventArgs e)
   {
       Init_WebControls();
   }
 
   public void Init_WebControls()
   {
       try
       {
           if (!string.IsNullOrEmpty(Request.QueryString["Action"]))//獲取form的Action中的參數
           {
               Action = Request.QueryString["Action"].Trim().ToLower();//去掉空格並變小寫
           }
           switch (Action)
           {
               case "login":
                   if (!string.IsNullOrEmpty(Request.Form["UserName"]) && !string.IsNullOrEmpty(Request.Form["UserPassWord"]))//獲取form中的參數
                   {
                       _UserLoginInfo.UserName = Request.Form["UserName"].ToString();
                       _UserLoginInfo.UserPassword = Request.Form["UserPassWord"].ToString();
                       string user = "select 管理員名稱,密碼 from T_管理員表 where 管理員名稱='" + _UserLoginInfo.UserName + "' and 密碼='" + _UserLoginInfo.UserPassword + "'";
                       if (myData.readDataSet(user).Tables[0].Rows.Count == 1)
                       {
                           Response.Redirect("Main.aspx", false);//防止Response.End 方法終止頁的執行
                       }
                       else
                       {
                           Response.Write("<Script Language=JavaScript>alert('密碼或用戶名錯誤,請重試!');</Script>");
 
                       }
                   }
                   break;
           }
       }
       catch (Exception ex)
       {
           throw new Exception(ex.Message);
       }
   }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM