发送ASP.Net控件ID JavaScript函数 [英] send ASP.Net Control Id To a JavaScript Function

查看:64
本文介绍了发送ASP.Net控件ID JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2次。在厂景在最后一次操作txtName的&安培;对视图2的第一个控制txtAge。需要从txtName的焦点改为txtAge在TAB preSS,但我无法达致这。

ASPX:

 < ASP:多视点ID =multiview1=服务器ActiveViewIndex =0>
  < ASP:鉴于ID =视图1=服务器>
  < ASP:文本框的id =改为txtName=服务器
onfocusout在=SetFocus的('<%= txtAge.ClientId%GT;');>< / ASP:文本框>
  < / ASP:视图>< ASP:鉴于ID =视图2=服务器>
   < ASP:文本框的id =txtAge=服务器>< / ASP:文本框>
< / ASP:视图>
< / ASP:多视角>

JS:

 函数的SetFocus(控件){
    / *警报(控件); * /
 的document.getElementById(控件)。重点();
}

当我检查了警报,它显示了<%= txtAge.ClientId%GT; 在弹出。这哪里是走错了。

下面是我的新发现:

这code效果很好,当目标控制在了同样的看法,但是当它是另一种看法,那它不。所以我觉得,别的东西也应该做到先更改视图,然后担心的焦点:

 < ASP:文本框的id =改为txtName=服务器
onfocusout在=SetFocus的();>< / ASP:文本框>功能SetFocus的(){
 的document.getElementById('<%= txtEmail.ClientID%GT;')。重点();
 / * txtEmail是在同一视图厂景为txtName的* /
}


解决方案

也许是足够用的tabIndex
http://msdn.microsoft.com/en -us /库/ ms178231%28V = VS.100%29.aspx

使用构建功能

而不是JavaScript的。

如果你使用asp.net 4.x中您可以使用的ClientIDMode =静
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

 < ASP:多视点ID =multiview1=服务器ActiveViewIndex =0>
    < ASP:鉴于ID =视图1=服务器>
        <! - 其他控制 - >
        < ASP:文本框的id =改为txtName=服务器的TabIndex =1/>
    < / ASP:视图>
    < ASP:鉴于ID =视图2=服务器>
        < ASP:文本框的id =txtAge=服务器的TabIndex =2/>
        <! - 其他控制 - >
    < / ASP:视图>
< / ASP:多视角>

修改
如果你不介意使用jQuery。你可以用一个div周围< ASP:查看.. ,然后像做:

  $(div.view输入:最后一子)上(变,函数(){。
    。$(本).parent()的next()找到(输入:第一胎)专注();
});

请记住,这是伪code。这只是一个想法。

I have 2 views. The last control on view1 is txtName & the first control on view2 is txtAge. Need to change the focus from txtName to txtAge on TAB press, but I fail to acheive this.

ASPX:

<asp:Multiview ID ="multiview1" runat="server" ActiveViewIndex="0">
  <asp:view ID="view1" runat="server">
  <asp:Textbox id="txtName" runat="server" 
onfocusout="SetFocus('<%=txtAge.ClientId%>');"></asp:TextBox>
  </asp:view>

<asp:view ID ="view2" runat="server">
   <asp:Textbox id="txtAge" runat="server" ></asp:TextBox>
</asp:view>
</asp:Multiview>

JS:

function SetFocus(controlId){
    /*alert(controlId);*/
 document.getElementById(controlId).focus();
}

When I check the alert, it shows <%=txtAge.ClientId%> in the popup. Where is this going wrong.

Here is my new finding:

This code works well when the target control is in the same view, but when it is in another view, then it doesnt. So I think, something else should also be done to change the view first and then worry about the focus:

<asp:Textbox id="txtName" runat="server" 
onfocusout="SetFocus();"></asp:TextBox>

function SetFocus(){
 document.getElementById('<%=txtEmail.ClientID%>').focus();
 /* txtEmail is in the same view 'view1' as in txtName */
}

解决方案

Perhaps it is sufficient to use tabindex http://msdn.microsoft.com/en-us/library/ms178231%28v=vs.100%29.aspx

that uses build in features rather then javascript.

if you use asp.net 4.x you can use clientidmode=static http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx

<asp:Multiview ID ="multiview1" runat="server" ActiveViewIndex="0">
    <asp:view ID="view1" runat="server">
        <!-- other controls -->
        <asp:Textbox id="txtName" runat="server" TabIndex="1"/>
    </asp:view>
    <asp:view ID ="view2" runat="server">
        <asp:Textbox id="txtAge" runat="server" TabIndex="2"/>
        <!-- other controls -->
    </asp:view>
</asp:Multiview>

edit if you do not mind using jQuery. You can wrap a div around <asp:View.. and then do something like:

$("div.view input:last-child").on("change", function(){
    $(this).parent().next().find("input:first-child").focus();
});

please keep in mind that this is pseudo code. It is just an idea.

这篇关于发送ASP.Net控件ID JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆