今天遇到个棘手的问题,就是在asp.net中使用confirm来处理,不是单纯的用Button1.Attributes.Add("onclick", "javascript:renturn confirm('测试')");的
网上查了之后的解决办法一是:
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('执行确定的处理')</script>");
TextBox1.Text = "";
}
else
{
if(TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('执行否的处理')</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('执行确定的处理')</script>");
18 TextBox1.Text = "";
19 }
20 else
21 {
22 if(TextBox1.Text == "no")
23 {
24 Response.Write("<script type ='text/javascript'> alert('执行否的处理')</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('执行确定的处理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('执行否的处理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先点击“赋值按钮”')</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('执行确定的处理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('执行否的处理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先点击“赋值按钮”')</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('执行确定的处理')</script>");
TextBox1.Text = "";
}
else
{
if (TextBox1.Text == "no")
{
Response.Write("<script type ='text/javascript'> alert('执行否的处理')</script>");
TextBox1.Text = "";
}
else
{
Response.Write("<script type ='text/javascript'> alert('先点击“赋值按钮”')</script>");
}
}
}
这只是提供一种简单的思路,其实也可以通过textbox的changed时间来处理的,不过这个需要postback一次