Finding an asp:button and asp:textbox in Javascrip

2019-08-13 12:58发布

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.

2条回答
Deceive 欺骗
2楼-- · 2019-08-13 13:43

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

http://jquery.com/

查看更多
对你真心纯属浪费
3楼-- · 2019-08-13 13:59

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>
查看更多
登录 后发表回答