简体   繁体   English

如何通过javascript显示/隐藏按钮包含在模板中

[英]How to show/hide buttons contain in a template by javascript

I have two asp.net buttons inside a template (Expand and Collapse) I want to implement a simple client side javascript function to hide the expand button after press it and show the collapse button and vice versa. 我在模板中有两个asp.net按钮(扩展和折叠),我想实现一个简单的客户端javascript函数,以在按下扩展按钮后隐藏扩展按钮并显示折叠按钮,反之亦然。

<asp:Button ID="btnExpand" runat="server" CommandName="Expand" 
            CommandArgument='<%# Container.DataElement("Id")%>' Text="+" />

<asp:Button ID="btnCollapse" runat="server" CommandName="Collapse" 
            CommandArgument='<%# Container.DataElement("Id")%>' Text="-" />

I tried OnClientClick event but I didn't know how to get the sender button and the second button from javascipt because they're in a template and their IDs will be generated. 我尝试了OnClientClick事件,但是我不知道如何从javascipt获取发件人按钮和第二个按钮,因为它们位于模板中,并且将生成其ID。

I tried also to change their visibility from the code behind in the server (by Visible property) but the problems is the event handler will be fired after the postback and the changes will not be applied in the client. 我还尝试通过服务器上的代码(通过Visible属性)更改其可见性,但问题是事件处理程序将在回发后被触发,并且更改将不会应用于客户端。

Any help !! 任何帮助!

sorry if the question is silly, I'm new in the web development. 抱歉,如果这个问题很傻,我是Web开发的新手。

Thanks in advance. 提前致谢。

Use ClientID like: 像这样使用ClientID:

<!-- Supposing you have the following button control -->
<asp:button id="myButton" runat="server" />

<script type="text/javascript">

    var theID = '<%= myButton.ClientID %>';

    // Now do whatever you like with it.

</script>

In the function where you are implementing the hide and show template, put $("#buttonID").hide(); 在实现隐藏和显示模板的函数中,放入$("#buttonID").hide(); .

Be careful where you place that code. 请注意将代码放置在何处。 You need to make sure that the html client renderer has finished creating that item. 您需要确保html客户端渲染器已完成创建该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM