0

私は2つのビューを持っています。view1 の最後のコントロールは txtName で、view2 の最初のコントロールは txtAge です。TAB を押してフォーカスを txtName から txtAge に変更する必要がありますが、これを達成できません。

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

アラートを確認すると<%=txtAge.ClientId%>、ポップアップに表示されます。これはどこが間違っているのでしょうか。

これが私の新しい発見です:

このコードは、ターゲット コントロールが同じビューにある場合はうまく機能しますが、別のビューにある場合は機能しません。したがって、最初にビューを変更してからフォーカスについて心配するために、何か他のことも行う必要があると思います。

<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 */
}
4

5 に答える 5

1

サーバー側のコードで設定を試すことができます:

txtName.Attributes["onfocusout"] = String.Format("SetFocus('{0}');", txtAge.ClientId); 
于 2013-10-23T13:36:31.320 に答える
1

おそらくhttp://msdn.microsoft.com/en-us/library/ms178231%28v=vs.100%29.aspxを使用するだけで十分ですtabindex

JavaScript ではなく組み込み機能を使用します。

asp.net 4.x を使用している場合は、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>

jQuery を使用してもかまわない場合は編集してください。div をラップしてから、次の<asp:View..ようにすることができます。

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

これは疑似コードであることに注意してください。それはただの考えです。

于 2013-10-23T13:37:39.923 に答える
0

その状況でインライン ブロックを使用して属性を設定することはできません...リテラル<%=txtAge.ClientId%>テキストを取得していて、期待する処理された値を取得していないことがわかります。

あなたには2つの明らかな解決策があります...

最初のもの(Yuriyがすでに答えであなたに与えたもの)は、コードビハインドから設定することです...

txtName.Attributes["onfocusout"] = String.Format("SetFocus('{0}');", 
                                                 txtAge.ClientId);

(免責事項、私はそれを使用したことがないので、それが何であるかわかりませんMultiView。そのため、以下の2番目のオプションが実際にあなたのシナリオで機能するかどうかは100%確信が持てません。また、テストされていないコードです。)

もう1つは、ムラリが回答で提供したものを使用することですが、追加の操作が必要です...

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

function SetFocus(nameCtrl) {
  var ageId = nameCtrl.id.replace(/txtName/,"txtAge");
  var ageCtrl = document.getElementById(ageId);
  ageCtrl.focus();
}
于 2013-10-23T13:53:43.397 に答える
0

onfocusout="SetFocus('<%=txtAge.ClientId%>');"></asp:TextBox> 行を次のように変更しますonfocusout="SetFocus('txtAge');"></asp:TextBox>

ClientId を送信する必要はありません

于 2013-10-23T14:32:01.373 に答える
0

これを試して、それがより良いかどうかを確認してください。

 <asp:Textbox id="txtName" runat="server" 
onfocusout='<%= String.Format("SetFocus(\"{0}\");", txtAge.ClientId) %>'></asp:TextBox>
于 2013-10-23T13:34:11.187 に答える