Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

c# - send ASP.Net Control Id To a JavaScript Function

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 */
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try setting it in server-side code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...