[英]Hide button in asp.net formview using jquery
I want to hide a button control inside a FormView
trough JQuery
. 我想在
FormView
槽JQuery
隐藏一个按钮控件。 The form section is defined in this way: 表单部分以这种方式定义:
<body>
<div id="main">
<form id="_frmMain" runat="server">
<div id="contenitore">
<asp:FormView
ID="_fvMain"
runat="server"
DefaultMode="Edit"
Width="100%">
<EditItemTemplate>
<asp:Table CssClass="sub" runat="server">
<asp:TableRow CssClass="tr_button_list">
<asp:TableCell ColumnSpan="3">
<asp:Button
ID="_btnOk"
ClientIDMode="Static"
Text="Ok"
runat="server"
CssClass="butt_orange_small"
OnClientClick="javascript: return ShowSection('section1');" />
<asp:Button
ID="_btnCancel"
ClientIDMode="Static"
Text="Cancel"
runat="server"
CssClass="butt_orange_small"
OnClientClick="javascript: return ShowSection('section2');" />
I use this code: 我用这个代码:
$(function () {
var _btnOk = $("#_btnOk");
_btnOk.hide();
});
but it's not working. 但它不起作用。 If I debug this script I found the
hidden
property of the _btnOk
object to remain false even after the .hide()
call. 如果我调试这个脚本,我发现
_btnOk
对象的hidden
属性即使在_btnOk
.hide()
调用之后也保持为false。
BTW I cannot hide the button using the class reference since it will hide _btnCancel
also and I need this to remain visibile. 顺便说一下,我无法使用类引用隐藏按钮,因为它也会隐藏
_btnCancel
,我需要这个才能保持可见性。
Your problem is that asp:FormView create an iframe .
你的问题是asp:FormView创建一个iframe 。
So jquery can't find your button, because it's not in main frame. 所以jquery找不到你的按钮,因为它不在主框架中。
As said in this tutorial, to access element in an iframe with jquery you have to do something like this: 如本教程所述,要使用jquery访问iframe中的元素,您必须执行以下操作:
$("#_fvMain").contents().find("#_btnOk").hide();
尝试按下这样的按钮:
var _btnOk = $("#<%= _btnOk.ClientID %>");
$(function () {
$('#contenitore input[type="button"][value="Ok"]').hide();
});
Your JQuery code is fine. 你的JQuery代码很好。 Its probably a problem with the following:
它可能是以下问题:
Begin Edit 开始编辑
Are you sure that the id is resolving to the correct html tag? 你确定id正在解析为正确的html标签吗? You should use the debug tools in the browser to navigate to the button and check the id.
您应该使用浏览器中的调试工具导航到该按钮并检查ID。 It might be that you are resolving to a different element on the form with the same name which is why you are not seeing the change.
可能是您正在解析表单上具有相同名称的不同元素,这就是您没有看到更改的原因。 See
Select an element
in Chrome ( ctrl+shift+C
). 请参阅在Chrome中
Select an element
( ctrl+shift+C
)。
Also as I mentioned and everyone else did too using <%= _btnOk.ClientID %>
is the best way to select the element id of an ASP.NET control. 另外正如我所提到的,其他人也使用
<%= _btnOk.ClientID %>
是选择ASP.NET控件的元素id的最佳方法。 However, this will only resolve on the .ascx
or .aspx
form. 但是,这只会在
.ascx
或.aspx
表单上解析。 This means that it will fail if you are using a javascript file like MyExternalScript.js
which you reference in the page/control. 这意味着如果您使用的是像在页面/控件中引用的
MyExternalScript.js
这样的javascript文件,它将会失败。 This is because when the control/page is renederd by the ASP.NET engine it will replace the <%= _btnOk.ClientID %>
string with the correct generated client id. 这是因为当ASP.NET引擎重新执行控件/页面时,它将使用正确生成的客户端ID替换
<%= _btnOk.ClientID %>
字符串。 It will not do this for external files like javascript files which means that <%= _btnOk.ClientID %>
will stay as is in that file and JavaScript will correctly throw an exception as it has no idea what to do with that string. 它不会对javascript文件之类的外部文件执行此操作,这意味着
<%= _btnOk.ClientID %>
将保留在该文件中,并且JavaScript将正确抛出异常,因为它不知道如何处理该字符串。
Ways around this: 方法围绕这个:
<script>
element and assign it to a global variable. <script>
元素中解析页面本身所需的ID,并将其分配给全局变量。 Then load your custom javascript file right after which can reference the now resolved client id. runat="server"
tag, this will ensure that ASP.NET will render the control as is without changing the client id. runat="server"
标记,这将确保ASP.NET将按原样呈现控件而不更改客户端ID。 End Edit 结束编辑
debugger;
debugger;
on a new line to your script which will stop processing at that point in your script if you have the development console open and you can step through starting at that point. $(document).ready(function(){/*your code*/});
$(document).ready(function(){/*your code*/});
包装脚本$(document).ready(function(){/*your code*/});
which makes sure your script waits for the page to finish loading before executing. _btnOk.toggle(false);
_btnOk.toggle(false);
and _btnOk.toggle(true);
_btnOk.toggle(true);
to hide and show the button respectively. btnOk.toggle();
btnOk.toggle();
to switch the button to hidden or visible depending on the current state. Here is a self contained working example to show that the JQuery call itself is correct. 这是一个自包含的工作示例,以显示JQuery调用本身是正确的。 The 2nd button hides or shows the first depending on the first buttons state.
第二个按钮根据第一个按钮状态隐藏或显示第一个按钮。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspFormTest.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
ID="_btnOk"
ClientIDMode="Static"
Text="Ok"
runat="server"/>
</div>
<button type="button" onclick="hideShowButton();">Hide or Show ok button</button>
<button type="button" onclick="hideShowButtonToggle();">Toggle button</button>
</form>
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript" language="javascript">
function hideShowButton() {
var aspNetWayOfResolvingId = $("#<%= _btnOk.ClientID %>"); // use this if clientidmode is not static
var _btnOk = $("#_btnOk");
if(_btnOk.is(":visible"))
_btnOk.hide();
else
_btnOk.show();
}
function hideShowButtonToggle() {
var aspNetWayOfResolvingId = $("#<%= _btnOk.ClientID %>"); // use this if clientidmode is not static
var _btnOk = $("#_btnOk");
_btnOk.toggle();
}
</script>
</body>
</html>
In the Asp.net you need to call the buttons using the following way 在Asp.net中,您需要使用以下方式调用按钮
$("#<%= _btnOk.ClientID %>");
You can use by CSS class also. 您也可以使用CSS类。
Create a Dummy CSS class 创建一个虚拟CSS类
<style>
.test{
}
</style>
Assign this .test
class to the _btnOk
so you can hide only that button not the _btnCancel
将此
.test
类分配给_btnOk
这样您就可以只隐藏那个按钮而不是_btnCancel
$(".test").hide();
I see two conditions that would cause the hide
function to fail: 我看到两个会导致
hide
功能失败的情况:
A parent of the button is already hidden when your function is called ( Why doesn't jQuery UI hide element with hidden parent? ) 调用函数时,按钮的父级已经隐藏( 为什么jQuery UI不隐藏隐藏父级的元素? )
Your butt_orange_small
class style has a display
property with the !important
flag. 您的
butt_orange_small
类样式具有带有!important
标志的display
属性。 One solution would be to remove the !important
flag. 一种解决方案是删除
!important
标志。 If, for any reason, you must keep it in the class style, you can do this to hide the button: 如果出于任何原因,您必须将其保留在类样式中,则可以执行此操作以隐藏按钮:
$("#_btnOk").attr('style','display:none !important');
You have to call your function after jquery loads. 你必须在jquery加载后调用你的函数。 You can do that putting "()" after your function.
你可以在你的功能之后加上“()” 。
This is an example: 这是一个例子:
$(function () {
var _btnOk = $("#_btnOk");
_btnOk.hide();
})();
I believe this approach will be efficient: 我相信这种方法会很有效:
$(document).ready(function(){
$("button[id*='_btnOk']").hide();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.