Javascript 如何根据客户端asp.net中的某些条件禁用radiobutton的回发?
我在表格中定义了一个单选按钮列表。当用户选择任何选项时,我们进行回发,然后执行业务逻辑。 现在的问题是,我想在用户选中单选按钮选项时添加一个确认;当用户选择OK时,只有我想继续 样本流:Javascript 如何根据客户端asp.net中的某些条件禁用radiobutton的回发?,javascript,asp.net,radio-button,postback,selectedindexchanged,Javascript,Asp.net,Radio Button,Postback,Selectedindexchanged,我在表格中定义了一个单选按钮列表。当用户选择任何选项时,我们进行回发,然后执行业务逻辑。 现在的问题是,我想在用户选中单选按钮选项时添加一个确认;当用户选择OK时,只有我想继续 样本流: 1.假设选中了radiobutton中的第一项。 2.用户将选择第二项,然后我想添加一个确认,如“确定吗” 3.如果用户选择否 然后,我应该在没有回发的情况下恢复更改的支票 否则 执行回发,然后执行业务逻辑 代码如下: <form id="form1" runat="server"> <asp
1.假设选中了radiobutton中的第一项。
2.用户将选择第二项,然后我想添加一个确认,如“确定吗”
3.如果用户选择否
然后,我应该在没有回发的情况下恢复更改的支票
否则
执行回发,然后执行业务逻辑
代码如下:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RadioButtonList ID="rdo" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
OnSelectedIndexChanged="LabourScheduleType_CheckedChanged">
<asp:ListItem Text="1" Value="1" onClick="confirmcheck();"></asp:ListItem>
<asp:ListItem Text="2" Value="2" onClick="confirmcheck();"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
JavaScript如下所示:
<script type="text/javascript">
function RadioClick(radioButton)
{
if (confirm('Are you sure?'))
{
__doPostBack(radioButton.id,'');
}
else
{
return false;
}
}
</script>
功能RadioClick(radioButton)
{
如果(确认('你确定吗?'))
{
__doPostBack(radioButton.id.);
}
其他的
{
返回false;
}
}
唯一的问题是当我返回false时,单选按钮中的值丢失,即没有选择任何单选按钮:(尝试
OnClientClick='return confirmcheck()“
。在呈现页面时,该值将作为onclick
属性的值呈现。此外,返回结果将表明默认处理是否将继续
OnClick
指定服务器端事件处理程序。尝试以下操作:
<asp:ListItem Text="1" Value="1" onClick="if(!confirmcheck()) return false;"></asp:ListItem>
<asp:ListItem Text="2" Value="2" onClick="if(!confirmcheck()) return false;"></asp:ListItem>
onClick中返回true或false也会阻止回发,因此我们只在confirmcheck()为false时返回
我已经测试过了,它可以正常工作,包括“EnableViewState=true”在单选按钮的HTML声明中。包含此属性后,EDIT1解决方案应能工作,因此如果返回false,状态将不会松动。OnClient单击哪个控件-RadiobuttonList或ListItem?我尝试了这两种方法,但均无效!javascript函数未被调用,回发发生:(这说明了我是如何做到这一点的。它没有单选按钮,但原理是一样的。我现在手头没有Visual Studio,但您应该能够找出为什么它不适合您。呈现的标记是什么?它呈现了onclick吗?-这说明没有OnClientClick事件!您的意思是说没有
OnClientClick
RadioButtonList或ListItem控件的属性?(因为我知道大多数控件都支持此属性。)但是如果它不受支持,那么OnClick
应该不加更改地通过。尝试修改代码,将return
包含在我的代码中。不,它不起作用!它根本不回发!如果我返回false,它将删除选中状态!
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadioButton1.Attributes.Add("onClick", "this.blur(); return RadioClick(this);");
RadioButton2.Attributes.Add("onClick", "this.blur(); return RadioClick(this);");
}
}
<script type="text/javascript">
function RadioClick(radioButton)
{
if (confirm('Are you sure?'))
{
__doPostBack(radioButton.id,'');
}
else
{
return false;
}
}
</script>
<asp:ListItem Text="1" Value="1" onClick="if(!confirmcheck()) return false;"></asp:ListItem>
<asp:ListItem Text="2" Value="2" onClick="if(!confirmcheck()) return false;"></asp:ListItem>