一个老生长谈的问题-获取confirm的返回值
如题,首先声明:乱答的或模糊答的不给分,不知道就是不知道!
现在正题:我引用了system.windows.forms.dll引用,加入了system.windows.forms命名空间,然后在一个imgbutton的click事件里写了:
public partial class Frame_Frame_Top : System.Web.UI.Page
{
HtmlWindow orderwindow;
HtmlElement formElement;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
online.Text = Application[ "online "].ToString();
online.ForeColor = Color.Yellow;
if (Session[ "uName "] != null)
{
uname.Text = Session[ "uName "].ToString();
}
if (Session[ "uDeptName "] != null)
{
Dept.Text = Session[ "uDeptName "].ToString();
}
//IMG1.Attributes.Add( "onclick ", "javascript:window.parent.location.href( '../Login/Login.aspx ') ");
}
}
protected void img_Click(object sender, ImageClickEventArgs e)
{
WebBrowser web=new WebBrowser();
HtmlDocument doc = web.Document;
orderwindow = doc.Window.OpenNew( "../Login/Login.aspx ", " ");
bool flag = orderwindow.Confirm( "是否決定退出繫統? ");
if (flag)
{
string str = "update EMP_STATE set STA_NOW= '0 ' where EMP_ID=(select EMP_ID from EMP where EMP_NAME= ' " + Session[ "uName "].ToString() + " ') ";
ConnectionDb db = new ConnectionDb();
db.getInt(str);
Response.Redirect( "../Login/Login.aspx ");
}
}
}
但是要报错误:
Server Error in '/EMS ' Application.
--------------------------------------------
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2 ' cannot be instantiated because the current thread is not in a single-threaded apartment.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2 ' cannot be instantiated because the current thread is not in a single-threaded apartment.
Source Error:
Line 36: protected void img_Click(object sender, ImageClickEventArgs e)
Line 37: {
Line 38: WebBrowser web=new WebBrowser();
Line 39: HtmlDocument doc = web.Document;
Line 40: orderwindow = doc.Window.OpenNew( "../Login/Login.aspx ", " ");
高手解答一下,我觉得能搞出来的不是很够,暂时给30分,不够再加,看问题的进展
[解决办法]
WebBrowser web=new WebBrowser();
HtmlDocument doc = web.Document;
orderwindow = doc.Window.OpenNew( "../Login/Login.aspx ", " ");--就算这句是对的,它会继续执行下一句
bool flag = orderwindow.Confirm( "是否決定退出繫統? ");--就算你这句是对的,它不管你选择的是什么依然执行下一个,flag是默认的初始化值。
if (flag)
{
string str = "update EMP_STATE set STA_NOW= '0 ' where EMP_ID=(select EMP_ID from EMP where EMP_NAME= ' " + Session[ "uName "].ToString() + " ') ";
ConnectionDb db = new ConnectionDb();
db.getInt(str);
Response.Redirect( "../Login/Login.aspx ");
}
[解决办法]
知错就改还是好孩子,楼上面的已经给出答案了,在客户端就可以出来了。
[解决办法]
大家不要BS楼主了, 我刚开始的时候也有这样confuse过.
我想楼主以前也没用搞错web编程吧,所以才会这样.
我一直做底层的,以前看web的代码简直头痛, 结构太乱了,后来听说ASP.NET可以象普通编程哪样使用对象了, 就答应人家帮忙做个网站, 结果就犯了这样的错误.
大家不要bs了, 以前走的不是一条道嘛, 哈哈,不管我们是多老的鸟,在web方面我们还是niewbie, 不过要是在solaris,linux,unix上面搞个其他的应用,楼主肯定要比webbie们强吧.
[解决办法]
1.
注意:
删除WinForm相关的东东,只留你的 ImageButton,
2.
关键给 imagebutton 加上 onclientclick= "if(!confirm( '是否決定退出繫統? ')) 这句,
通过客户端确定用户是否提交页面
3。
DEMO
// .aspx
<asp:imagebutton id=img click=img_Click onclientclick= "if(!confirm( '是否決定退出繫統? ')) return false; " runat=server />
// .aspx.cs
protected void img_Click(object sender, ImageClickEventArgs e)
{
//WebBrowser web=new WebBrowser();
//HtmlDocument doc = web.Document;
//orderwindow = doc.Window.OpenNew( "../Login/Login.aspx ", " ");
//bool flag = orderwindow.Confirm( "是否決定退出繫統? ");
//if (flag)
//{
string str = "update EMP_STATE set STA_NOW= '0 ' where EMP_ID=(select EMP_ID from EMP where EMP_NAME= ' " + Session[ "uName "].ToString() + " ') ";
ConnectionDb db = new ConnectionDb();
db.getInt(str);
Response.Redirect( "../Login/Login.aspx ");
//}
//}
}
Good Luck!