我想在一个方法中调用一个按钮事件,该如何实现。
别说把按钮事件的方法独立出来,然后再调用。我试过不行,原因是Ajax的原因。因为该方法有response.write();
或者提供下应该在Ajax如何修改这个方法?

解决方案 »

  1.   

    最好贴代码,不然别人怎么清楚你在说什么?
    javascript可以主动触发按钮事件,用 mybutton.click()方法,后台cs代码只能调用事件的处理函数,不能触发一个事件,除非你自定义一个事件
      

  2.   

    代码比较长,我贴部分吧。
    大概我要实现的功能就是,在考试到了20分钟的时候就会自动提交答案。
    public partial class Examing : System.Web.UI.Page
    {
        Exam ex = new Exam();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
               DateTime time1 = new DateTime(2008, 1, 1, 0, 0, 0);
               Session["time"] = time1;
               int lessionType = int.Parse(Request.QueryString["id"]);//所属于的课程
               DataList1.DataSource = ex.getDataSource(lessionType);
               DataList1.DataBind();
               if(Session["student"]!=null)
               {
                   lblName.Text = ((StudentModel)(Session["student"])).Name;
               }
            }
            
        }
        //交卷按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            submit();
        }    private void submit()
        {
            string idstr = ((StudentModel)Session["student"]).Id;//学生的ID证号。注意是string型的
            int lessionType = int.Parse(Request.QueryString["id"]);//所属于的课程
            foreach (DataListItem item in DataList1.Items)
            {
                RadioButtonList rdo = (RadioButtonList)item.FindControl("RdoItem");
                string answer = rdo.SelectedItem.Text;//考试作出的答案
                int QuestionId = int.Parse((((Label)(item.FindControl("lblid"))).Text));//题目的id
                ex.insertAnswer(idstr, lessionType, QuestionId, answer);
            }
            int score = ex.getScore();//算成绩
            ex.insertScore(idstr, lessionType, score);//插入成绩表
            Response.Write("<script>alert('交卷完成!');location='Default.aspx'</script>");
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            
        }
        //DateTime time1 =new DateTime ();
        
        public void showTime() 
        {        DateTime timeBegin = new DateTime();
            timeBegin = ((DateTime)Session["time"]).AddSeconds(1);
            Session["time"] = timeBegin;
            DateTime timeEnd = new DateTime(2008, 1, 1, 0,0,30);
            lbltime.Text = timeBegin.ToString("mm:ss");
            if(timeBegin.CompareTo(timeEnd)==0)
            {
                Timer1.Enabled = false;
                submit();
            }
            
            
        }我直接按“交卷”按钮是不会出错的。但是由代码判定事件到的时候执行submit();方法就会报错,报错为:
    页面会弹出错误:
    Sys.WebForms.PageRequestManagerParserErrorException:The message received from the server count not be parsed.Common causes for this error are when the response is modified by calls to response.Write(),response filters,HttpModules,or server trace is enabled.
    Details:Error parsing near '<script language=javascript'. 我觉得应该是Ajax的问题吧。但是直接按“交卷”又不会出错噢。。头大了
      

  3.   

    确实是ajax的原因。不用Response.Write,改成:
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.Newid().ToString(),"alert('交卷完成!');location='Default.aspx';", true);
    应该就可以了
      

  4.   

    等于是临时注册了一段处理客户端onload事件的javascript代码,在服务端代码执行完,返回客户端的时候触发
      

  5.   

    还是贴个MSDN的连接吧,比我说的清楚多了:http://msdn.microsoft.com/zh-cn/library/system.web.ui.scriptmanager.registerstartupscript.aspx