今天遇到個(gè)棘手的問題,就是在asp.net中使用confirm來處理,不是單純的用Button1.Attributes.Add("onclick", "javascript:renturn confirm('測試')");的
網(wǎng)上查了之后的解決辦法一是:

aspx中主要代碼
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="無confirm" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="賦值" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body
還有aspx.cs頁面
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:if(confirm('確定')) "+
"{ document.forms[0]['TextBox1'].value= 'ok';}"+
" else"+
" { document.forms[0]['TextBox1'].value= 'no'}");
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "ok")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行確定的處理')</script>");
TextBox1.Text = "";
}
else
{
if(TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行否的處理')</script>");
TextBox1.Text = "";
}
}

1 protected void Page_Load(object sender, EventArgs e)
2 {
3
4 }
5 protected void Button2_Click(object sender, EventArgs e)
6 {
7 Button1.Attributes.Add("onclick", "javascript:if(confirm('確定')) "+
8 "{ document.forms[0]['TextBox1'].value= 'ok';}"+
9 " else"+
10 " { document.forms[0]['TextBox1'].value= 'no'}");
11
12 }
13 protected void Button1_Click(object sender, EventArgs e)
14 {
15 if (TextBox1.Text == "ok")
16 {
17 Response.Write("<script type ='text/javascript'> alert('執(zhí)行確定的處理')</script>");
18 TextBox1.Text = "";
19 }
20 else
21 {
22 if(TextBox1.Text == "no")
23 {
24 Response.Write("<script type ='text/javascript'> alert('執(zhí)行否的處理')</script>");
25 TextBox1.Text = "";
26 }
27 }
28

protected void Button2_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:if(confirm('確定')) "+
"{ document.forms[0]['TextBox1'].value= 'ok';}"+
" else"+
" { document.forms[0]['TextBox1'].value= 'no'}");
Button1.Text = " 有confirm";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "ok")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行確定的處理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行否的處理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先點(diǎn)擊“賦值按鈕”')</script>");
}
}

protected void Button2_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:if(confirm('確定')) "+
"{ document.forms[0]['TextBox1'].value= 'ok';}"+
" else"+
" { document.forms[0]['TextBox1'].value= 'no'}");
Button1.Text = " 有confirm";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "ok")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行確定的處理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行否的處理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先點(diǎn)擊“賦值按鈕”')</script>");
}
}

aspx.cs中主要代碼
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:if(confirm('確定')) "+
"{ document.forms[0]['TextBox1'].value= 'ok';}"+
" else"+
" { document.forms[0]['TextBox1'].value= 'no'}");
Button1.Text = " 有confirm";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "ok")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行確定的處理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('執(zhí)行否的處理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先點(diǎn)擊“賦值按鈕”')</script>");
}
}
}
這只是提供一種簡單的思路,其實(shí)也可以通過textbox的changed時(shí)間來處理的,不過這個(gè)需要postback一次