积极答复者
js调用.net后台代码

问题
-
这是top_bottom.master母板页的
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="top_bottom.master.cs" Inherits="top_bottom" %> <!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"> <head runat="server"> <title>同类商品大全</title> <asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder> </head> <body><form id="form1" runat="server"> <div> <table align="center" width="983px" cellpadding="0" cellspacing="0" border="0"> <tr><td align="center"> <table style="width:983px;height:114px;"> <tr style="width:983px;"> <td style="width:983px;height:27px;" colspan="10"><table width="983px"><table width="983px"> <tr> <td style="width:32px;"> <asp:ImageButton ID="imgBtnLeftSearch" ImageUrl="~/img/search_go.jpg" runat="server" Height="27" align="left" OnClick="imgBtnLeftSearch_Click" /></td> <td align="right">
<asp:ImageButton ID="ibtnLoginOut" runat="server" Visible="false" ImageUrl="~/img/loginOut.jpg" onclick="ibtnLoginOut_Click" Width="50" Height="21"/></td> </tr> </table></td> </tr> </table> </td></tr> <tr><td style="width:983px;" align="center"> <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </td></tr> </table> </div> </form></body> </html>这是top_bottom.master.cs
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class top_bottom : System.Web.UI.MasterPage { protected void imgBtnLeftSearch_Click(object sender, ImageClickEventArgs e) { } protected void ibtnLoginOut_Click(object sender, ImageClickEventArgs e) { } }
这是“调用js母板页代码分开.aspx”(使用了上面的母板页),英文的命名我也试了,不可以<%@ Page Language="C#" MasterPageFile="~/top_bottom.master" AutoEventWireup="true" CodeFile="调用js母板页代码分开.aspx.cs" Inherits="调用jsC" Title="无标题页" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <script type="text/javascript"> function EnterIsKeydown(evt) { evt = window.event ? window.event : evt; if (evt.keyCode.toString() == "13")//is it enter key { document.getElementById("<%=btnAdd.ClientID %>").click(); } } </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <body onkeydown="EnterIsKeydown(event)"> 请先输入,再按回车<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:ImageButton ID="btnAdd" runat="server" ImageUrl="imgs/login.jpg" OnClick="btnAdd_Click" AlternateText="请按回车" ToolTip="回车"/> </body> </asp:Content>
这是“调用js母板页代码分开.aspx.cs”
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public partial class 调用jsC : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAdd_Click(object sender, ImageClickEventArgs e) { Response.Write("当前时间:" + DateTime.Now.ToString()); } }
焦点只是集中在第一个按钮(母板页)上,怎么集中到子页面的btnAdd这个按钮上?
- 已编辑 NewJoin 2009年9月24日 7:02
答案
-
<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1" > <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> <asp:ImageButton OnClick="btnAdd_Click" ID="btnAdd" runat="server" ></asp:ImageButton> <script type="text/javascript"> window.onload = function() { document.getElementsByTagName("body")[0].onkeydown=function (evt) { evt = window.event ? window.event : evt; if (evt.keyCode == 13)//is it enter key { document.getElementById('<%= btnAdd.ClientID %>').click(); } } } document.getElementById('<%= TextBox1.ClientID %>').onkeypress = function() { if (event.keyCode == 13) { document.getElementsByTagName("body")[0].focus(); return false; } } </script> </asp:Content>
- 已建议为答案 mldark 2009年9月24日 8:42
- 已标记为答案 KeFang Chen 2009年9月25日 2:10
全部回复
-
关键的问题是要有光标在页面上,也就是能触发ketdown事件
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { Response.Write("okk"); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function EnterIsKeydown() { alert("x"); if (event.keyCode == 13)//is it enter key { document.getElementById("<%=ImageButton1.ClientID %>").click(); } } </script> </head> <body onkeydown="EnterIsKeydown()"> <form id="form1" runat="server"> 有内容啊 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/a.JPG" OnClick="ImageButton1_Click" Style="width: 26px" /> 有内容啊 有内容啊 </form> </body> </html>
【孟子E章】 -
如果用母板的话 可以这样
<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1" > <asp:ImageButton OnClick="btnAdd_Click" ID="btnAdd" runat="server" ></asp:ImageButton> <script type="text/javascript"> window.onload = function() { document.getElementsByTagName("body")[0].onkeydown=function EnterIsKeydown() { if (event.keyCode == 13)//is it enter key { document.getElementById('<%= btnAdd.ClientID %>').click(); } } } </script> </asp:Content>
-
以下代码在IE8,Firefox3.5,Chrome3,Safari 4,opera 10下测试都通过
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { Response.Write("当前时间:" + DateTime.Now.ToString()); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function EnterIsKeydown(evt) { evt = window.event ? window.event : evt; if (evt.keyCode.toString() == "13")//is it enter key { document.getElementById("<%=ImageButton1.ClientID %>").click(); } } </script> </head> <body onkeydown="EnterIsKeydown(event)"> <form id="form1" runat="server" defaultbutton="ImageButton1"> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/a.JPG" OnClick="ImageButton1_Click" Style="width: 26px" /> 鼠标点击页面的文字,按回车键。或者在文本框输入文字回车。 <input /> </form> </body> </html>
【孟子E章】 -
您好,我会在以后不这样的了,我还是个msdn的新手,以后我会把修改写在回复里面或者重新建帖子。
我的意思是:在母板页有按钮(imgBtnLeftSearch和ibtnLoginOut),在使用了母板页的子页面也有按钮(bntAdd),通过上面的方法,不能做到焦点在子页面的的按钮上。我要实现的功能就是,在这个子页面按回车键就相当于单击btnAdd按钮,而实际情况是,我输入文本后,按回车,并没执行btnAdd的事件,并且可以看到回车后焦点集中在imgBtnLeftSearch上,也就是说,回车后它执行的是imgBtnLeftSearch按钮的事件,而我要它执行的是btnAdd的事件
不知道这样,还算详细吗? -
<asp:Content ID="Content1" runat="server" contentplaceholderid="ContentPlaceHolder1" > <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> <asp:ImageButton OnClick="btnAdd_Click" ID="btnAdd" runat="server" ></asp:ImageButton> <script type="text/javascript"> window.onload = function() { document.getElementsByTagName("body")[0].onkeydown=function (evt) { evt = window.event ? window.event : evt; if (evt.keyCode == 13)//is it enter key { document.getElementById('<%= btnAdd.ClientID %>').click(); } } } document.getElementById('<%= TextBox1.ClientID %>').onkeypress = function() { if (event.keyCode == 13) { document.getElementsByTagName("body")[0].focus(); return false; } } </script> </asp:Content>
- 已建议为答案 mldark 2009年9月24日 8:42
- 已标记为答案 KeFang Chen 2009年9月25日 2:10