.net之旅-JavaScript的导入引用(42)

    技术2022-05-11  127

    JavaScript的应用是B/S基本上必不可少的。可是怎样才能对其有效管理和引用呢。本文将JS写在一个文件夹Inc下。引用通过src="目录/*.js"

    下面是一个登录时通过回车来实现点击登录按钮的示例:

    1 在*.aspx中添加代码如下:

    <script type="text/javascript" language="javascript" src="Inc/Common.js"></script> 

     <td style="height: 26px; width: 160px;" align="center"><asp:Button ID="ButtonLogin" runat="server" Text="登录" ToolTip="用户登录" OnClick="ButtonLogin_Click" /></td>

    详细代码如下:

    <% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Index.aspx.cs" Inherits="_Default"  %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html  xmlns ="http://www.w3.org/1999/xhtml"   > < script  type ="text/javascript"  language ="javascript"  src ="Inc/Common.js" ></ script > < head  runat ="server" >      < title > 企业综合管理平台 </ title > </ head > < body >      < form  id ="formLogin"  runat ="server" >      < table >          < tr >              < td  style ="height: 26px; width: 67px;" > 用户名: </ td >              < td  style ="width: 160px; height: 26px" >< asp:TextBox  ID ="TextBoxUserId"  runat ="server" ></ asp:TextBox ></ td >          </ tr >          < tr >              < td  style ="width: 67px" > 密  码: </ td >              < td  style ="width: 160px" >< asp:TextBox  ID ="TextBoxUserPwd"  runat ="server" ></ asp:TextBox ></ td >          </ tr >          < tr >              < td ></ td >              < td  style ="height: 26px; width: 160px;"  align ="center" >< asp:Button  ID ="ButtonLogin"  runat ="server"  Text ="登录"  ToolTip ="用户登录"  OnClick ="ButtonLogin_Click"   /></ td >          </ tr >          < tr >          < td ></ td >          < td >< asp:Label  ID ="LabelError"  runat ="server"  Text ="用户名或密码错误"  Visible ="False"  ForeColor ="Red" ></ asp:Label ></ td >          </ tr >      </ table >      </ form >      </ body > </ html >

     

    2 Common.js代码如下:

    function SubmitKeyClick(button) {        if (event.keyCode == 13)     {                document.getElementById("ButtonLogin").click();    }}

    3 *.aspx.cs调用如下:

    TextBoxUserPwd.Attributes.Add("onkeydown", "SubmitKeyClick('ButtonLogin');");

    详细代码如下:

         /// <summary>    /// 登录按钮处理    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>      protected   void  ButtonLogin_Click( object  sender, EventArgs e)     {        if (ValidInput())        {            if (CheckUserLogin())            {                Session["userId"= TextBoxUserId.Text;                Response.Redirect("~/InterFace/MainFame.aspx");            }        }    }

    测试通过

     


    最新回复(0)