如何在代码后面使用javascript确认框和警告框? [英] How to use javascript confirm box and alert box in code behind?

查看:60
本文介绍了如何在代码后面使用javascript确认框和警告框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我正在使用GridView制作电路板,我遇到了一些问题。



当我尝试要删除复选框选中项目的项目,我想要这样。



如果未选中复选框,请按删除按钮,提醒消息请检查项目



或选中复选框,然后按删除按钮,确认消息检查项目将被删除!!



并选择是删除所选项目。



如何制作我的代码?我在下面试过,



当未选中复选框时,警报信息显示正常,但我尝试删除已选中的项目



没有确认消息,仍然确认消息显示.....



请检查我的代码并给我建议。



我尝试过:



protected void lnbDel_Click(object sender,EventArgs e)

{

UserBoardDAC dac = new UserBoardDAC();



foreach(grvList.Rows中的GridViewRow gRow)

{

CheckBox chk =(CheckBox)gRow.FindControl(chk);

if(chk.Checked)

{

string message = @

< script type ='text / javascript'>

var delConfirm = confirm( '检查项目将被删除!!你确定吗?');

if(delConfirm)

{

返回true;

}

其他

{

返回false;

}

< / script>;

this.ClientScript.RegisterClientScriptBlock( this.GetType(),script,message);

int boardItemID = Convert.ToInt32(gRow.Cells [0] .Text);

dac.DeleteSelectedContents( boardItemID);

}

else

{

string message = @

< script type ='text / javascript'>

alert('请检查项目');

< / script>;

this.ClientScript.RegisterClientScriptBlock(this.GetType(),script,message);

}

}

/ / putGridView();

GetCustomPaging(1);



}

解决方案

< blockquote>确认应在客户端处理。这是一个简单的例子:



假设你有这个GridView Columns声明:

 <   >  
< asp:templatefield xmlns:asp = #unknown >
< HeaderTemplate >
< asp:button id = ButtonDelete runat = server text = 删除 / >
< / HeaderTemplate >
< itemtemplate >
< asp:checkbox id = CheckBox1 runat = server / >
< / itemtemplate >
< / asp:templatefield >
< < span class =code-leadattribute> asp:boundfield datafield = CustomerID headertext = ID readonly = True xmlns:asp = #unknown / >
< asp:boundfield datafield = CompanyName headertext = 公司 xmlns:asp = #unknown / >
< asp:boundfield datafield = ContactName headertext = 名称 xmlns:asp = #unknown / >
< asp: boundfield datafield = ContactTitle headertext = 标题 xmlns:asp = #unknown / > ;
< asp:boundfield datafield = 地址 headertext = 地址 xmlns:asp = #unknown / >
< asp:boundfield datafield = 国家/地区 < span class =code-attribute> headertext = 国家/地区 xmlns:asp = #unknown / > ;
< / columns >





然后你可以写一个JavaScript函数来处理confi如下所示:



< script type =   text / javascript language =   javascript> 
function ConfirmOnDelete(item)
{
if (IsValidToDelete( )){
if (确认( 以下项目将被删除: + item + 继续? )== true
return true < /跨度>;
else
return false < /跨度>;
}
else {
alert( 请检查一个项目。);
}
}

function IsValidToDelete(){
var valid = false ;
var gv = document .getElementById( <%= GridView1.ClientID%>);
for var i = 0 ; i< gv.getElementsByTagName( input)。length; i ++){
var node = gv.getElementsByTagName( input )[I];
if (node!= null && node.type == 复选框&& node.checked){
valid = ;
break ;
}
}

返回有效;
}
< / script>





现在,您需要连接JavaScript onclick 删除按钮的事件,用于调用 ConfirmOnDelete()函数。要做到这一点,你可以在RowDataBound事件中这样做:

  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) // 检查RowType
{
// 使用FindControl从Header TemplateField访问LinkBut​​ton
按钮b =(按钮)e.Row.FindControl( ButtonDelete);
// 附加JavaScript函数
b.Attributes.Add( onclick return ConfirmOnDelete(););
}
}





或者最简单的方法是直接调用 ConfirmOnDelete( )函数声明性地如下:

 <   asp:button     id   =  ButtonDelete    runat   =  server    text   = 删除    xmlns:asp   =  #unknown >  
OnClick =ButtonDelete_Click
OnClien tClick =return ConfirmOnDelete(); />
< / asp:button >





注意 ConfirmOnDelete()函数的调用 OnClientClick 事件。



对于服务器端删除,我建议你参考这个例子:使用CheckBox进行GridView多次删除并确认 [< a href =http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-multiple-delete-with-checkbox-and-confirm.aspx\"target =_ blanktitle =New Window> ^ ]


这里列出的一些可能的方法



要求用户确认他们想继续采取行动| ASP.NET论坛 [ ^ ]



你必须记住的一件事是当用户单击删除按钮并进入服务器代码时,您的服务器代码无法在客户端上执行代码,因此您无法让服务器代码向客户端发送javascript,等待单击按钮,然后继续。


Hello.

I`m making a board using GridView, and I have some problem.

When I try to delete items that check box checked item, I want to like this.

if check box unchecked, and press delete button, alert message "Please check the items"

Or check box checked, and press delete button, confirm message "Check item will deleted!!"

and choose yes selected items are deleted.

How can I make my code? I tried below,

When check box unchecked, alert message shows well, But I try to delete checked items

there`s no confirm message, still confirm message showed.....

Please check my code and give me you advice.

What I have tried:

protected void lnbDel_Click(object sender, EventArgs e)
{
UserBoardDAC dac = new UserBoardDAC();

foreach (GridViewRow gRow in grvList.Rows)
{
CheckBox chk = (CheckBox)gRow.FindControl("chk");
if (chk.Checked)
{
string message = @"
<script type='text/javascript'>
var delConfirm = confirm('Check item will deleted!! Are you sure?');
if (delConfirm)
{
return true;
}
else
{
return false;
}
</script>";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", message);
int boardItemID = Convert.ToInt32(gRow.Cells[0].Text);
dac.DeleteSelectedContents(boardItemID);
}
else
{
string message = @"
<script type='text/javascript'>
alert('Please check the items');
</script>";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", message);
}
}
//PutGridView();
GetCustomPaging(1);

}

解决方案

Confirmation should be handled at the client-side. Here's a quick example:

Suppose that you have this GridView Columns declaration:

<columns>
   <asp:templatefield xmlns:asp="#unknown">
   <HeaderTemplate>
      <asp:button id="ButtonDelete" runat="server" text="Delete" />
   </HeaderTemplate>
   <itemtemplate>
      <asp:checkbox id="CheckBox1" runat="server" />
   </itemtemplate>
   </asp:templatefield>
   <asp:boundfield datafield="CustomerID" headertext="ID" readonly="True" xmlns:asp="#unknown" />
   <asp:boundfield datafield="CompanyName" headertext="Company" xmlns:asp="#unknown" />
   <asp:boundfield datafield="ContactName" headertext="Name" xmlns:asp="#unknown" />
   <asp:boundfield datafield="ContactTitle" headertext="Title" xmlns:asp="#unknown" />
   <asp:boundfield datafield="Address" headertext="Address" xmlns:asp="#unknown" />
   <asp:boundfield datafield="Country" headertext="Country" xmlns:asp="#unknown" />
</columns> 



You could then write a JavaScript function to handle the confirmation like in the following:

<script type="text/javascript" language="javascript">
        function ConfirmOnDelete(item)
        {
		if(IsValidToDelete()){
          		if (confirm("The following item(s) will be deleted: " + item + "Continue?")==true)
            			return true;
          		else
            		return false;
		}
		else{
			alert("Please check an item.");
		}
        }

	function IsValidToDelete() {
    		var valid = false;
    		var gv = document.getElementById("<%=GridView1.ClientID%>");
    		for (var i = 0; i < gv.getElementsByTagName("input").length; i++) {
        		var node = gv.getElementsByTagName("input")[i];
        		if (node != null && node.type == "checkbox" && node.checked) {
            			valid = true;
            			break;
        		}
    		}

    		return valid;
	}
</script>



Now, you need to hook up a JavaScript onclick event for the Delete button to invoke the ConfirmOnDelete() function. To to do that, you could do it like this at RowDataBound event:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.Header) //check for RowType
        {
            //access the LinkButton from the Header TemplateField using FindControl
            Button b = (Button)e.Row.FindControl("ButtonDelete"); 
            //attach the JavaScript function
            b.Attributes.Add("onclick", "return ConfirmOnDelete();"); 
        }
}



Or the simplest way is to directly call the ConfirmOnDelete() function declaratively like this:

<asp:button id="ButtonDelete" runat="server" text="Delete" xmlns:asp="#unknown">
	    OnClick="ButtonDelete_Click"
	    OnClientClick="return ConfirmOnDelete();" />
</asp:button>



Notice the call to ConfirmOnDelete() function in OnClientClick event.

For the server-side deletion, I would suggest you to refer this example: GridView Multiple Delete with CheckBox and Confirm[^]


A few possible methods listed here

Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]

One thing you have to bear in mind is that when the user clicks the delete button and it goes into your server code, your server code can't execute code on the client so you can't have your server code send javascript to the client, wait for a button to be clicked, then continue on.


这篇关于如何在代码后面使用javascript确认框和警告框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆