Finding an asp:button and asp:textbox in Javascript

无人久伴 提交于 2019-12-24 09:54:06

问题


What I'm trying to do is get an asp:button to click. The only problem is that it is within a few tags.

Example:

<loginview>
     <asp:login1>
          <logintemplate>
              //asp:textbox and asp:button are located here.
          </loginview>
     </asp:login>
</logintemplate>

So how would I get javascript to point to that location so that I can manipulate it. For example, get the button to click.


回答1:


First, you need to figure out which template is being used, since you can only access the active one. (Anonymous or LoggedIn). Once you do that, use the FindControl method on the LoginView to find the ClientID of the element you need to reference.

For example:

<form runat="server">
  <asp:LoginView runat="server" ID="LoginView">
    <AnonymousTemplate>
      <asp:Button ID="ASPButton" Text="Button" runat="server" />
    </AnonymousTemplate>
  </asp:LoginView>
</form>

<script type="text/javascript">
 var el = document.getElementById('<%= LoginView.FindControl("ASPButton").ClientID %>');
</script>



回答2:


Check out the jQuery framework: you can find controls by ID, and then call methods/properties on those controls.

http://jquery.com/



来源:https://stackoverflow.com/questions/3024954/finding-an-aspbutton-and-asptextbox-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!