在单个按钮上使用javacsript警报和确认框单击!! [英] Using javacsript alert and confirmation box on single button click!!
本文介绍了在单个按钮上使用javacsript警报和确认框单击!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我有一个按钮btnDelete-
Hey everyone
I have a button btnDelete --
<asp:Button ID="btnDelete" runat="server" Text="Delete" Style="float: left; margin-right: 10px;
color: black; font-weight: bold; margin-left: 40px" Height="26px" Width="67px"
OnClientClick="javascript:return confirm(''Are you sure you want to delete this item?'')"
OnClick="btnDelete_Click" />
按钮点击处理程序的代码是-
and codebehind for button click handler is--
protected void btnDelete_Click(object sender, EventArgs e)
{
RadGrid Grid = (this.FindControl("RadGrid1") as RadGrid);
if (RadGrid1.SelectedItems.Count != 0)
{
string Pid = Convert.ToString(Grid.SelectedValues["Pid"]);
if (!string.IsNullOrEmpty(Pid))
{
try
{
int resultDel = objSQLHelper.DeleteRowsInDb(Convert.ToInt32(Pid));
//RadGrid1.Rebind();
BindGrid();
}
catch (Exception ex)
{
}
}
}
}
有没有一种方法可以验证此按钮客户端,例如,如果没有选择任何行,它会显示警告请选择一行",并且直到选中某行后才会显示确认框?...
谢谢
Amit
is there a way by which i can validate this button client side,such that if no row is selectced,it shows an alert "Please select a row" and no confirmation box is shown until a row is selected?...
Thanks
Amit
推荐答案
使用Java脚本的控件验证此文本框代码.
我希望它对U有用.
如果所有路径都返回true,请检查条件onClientClick Delete,如果所有路径都返回true,则插入代码以进行确认.
Control Validation using Javascript This code for Text box.
I hope its useful for U.
Check Condition onClientClick Before Delete if all path return true then insert code for Confirmation.
<pre lang="cs">// Get All Childe Node value inside Selected Div
function CheckSelection() {
var DivCollection = document.getElementById("UrradGrid");
var inputDiv = DivCollection.getElementsByTagName("input");
if (DivCollection.hasChildNodes) {
for (var i = 0; i < inputDiv.length; i++) {
if (inputDiv[i].type == ''text'') {
if (inputDiv[i].id.search(''txtBuildingName'') != -1) {
var id = inputDiv[i].id;
var txtControl = document.getElementById(id);
var txtValue = txtControl.value;
if (txtControl.value == "") {
txtControl.focus();
alert("Please Enter Correct Name");
return false;
}
}
}
}
} // hasChildENd
Arvind的解决方案:
Arvind''s solution:
function CheckSelection() {
var myGrid = document.getElementById("<%=RadGrid1.ClientID%>");
var checks = myGrid.getElementsByTagName("input");
var selected = false;
if (checks.hasChildNodes) {
for (var i = 0; i < checks.length; i++) {
if (checks[i].type == 'checkbox')
selected = checks[i].checked == 1;
}
}
if(selected)
return confirm('Are you sure you want to delete this item?')
else
return false;
}
...
...
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete" Style="float: left; margin-right: 10px;<br />
color: black; font-weight: bold; margin-left: 40px" Height="26px" Width="67px"<br />
OnClientClick="return CheckSelection()"<br />
OnClick="btnDelete_Click" />
if (RadGrid1.SelectedItems.Count != 0)
{
string Pid = Convert.ToString(Grid.SelectedValues["Pid"]);
if (!string.IsNullOrEmpty(Pid))
{
try
{
int resultDel = objSQLHelper.DeleteRowsInDb(Convert.ToInt32(Pid));
//RadGrid1.Rebind();
BindGrid();
}
catch (Exception ex)
{
}
}
}
else
AlertBox("Hey you, select something first.");
...
...
protected void AlertBox(string message)
{
ClientScript.RegisterClientScriptBlock(
Page.GetType(), "alertBox", "alert('" + message + "');", true);
}
这篇关于在单个按钮上使用javacsript警报和确认框单击!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文