0
  • 我有 2 个文件。一种是 aspx 及其 ascx。
  • aspx 包含计算考试倒计时时间的 Javascript。
  • 当计时器值达到 00:00:00 时,我想调用 ascx 文件中的事件“提交考试”。

最终论文.aspx.cs

public partial class Final_Paper : System.Web.UI.Page 
{
    long timerStartValue = 600;

    protected void Page_Load(object sender, EventArgs e)
    {            
        if (!Page.IsPostBack)
        {
            this.timerStartValue = long.Parse(ConfigurationManager.AppSettings["Delay"].ToString());
            this.TimerInterval = 500;
        }
    }

    public string message()
    {
        string val;
        val = Request.Form["timerData"].ToString();
        TimeSpan ts = TimeSpan.FromMilliseconds(Convert.ToDouble(val.ToString()));
        return ts.ToString();            
    }

    void Page_PreRender(object sender, EventArgs e)
    {
        StringBuilder bldr = new StringBuilder();            
        bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lbltime.ClientID);
        bldr.Append("Timer.go()");
        Page.ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
        Page.ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
    }

    void Page_PreInit(object sender, EventArgs e)
    {
        string timerVal = Request.Form["timerData"];
        if (timerVal != null || timerVal == "")
        {
            timerVal = timerVal.Replace(",", String.Empty);
            timerStartValue = long.Parse(timerVal);
        }
    }

    private Int32 TimerInterval
    {
        get
        {
            object o = ViewState["timerInterval"];
            if (o != null) { return Int32.Parse(o.ToString()); }
            return 50;
        }
        set { ViewState["timerInterval"] = value; }

    }

}

最终论文.aspx

<script type="text/javascript">
    function myTimer(startVal, interval, outputId, dataField) {
        this.value = startVal;
        this.OutputCntrl = document.getElementById(outputId);
        this.currentTimeOut = null;
        this.interval = interval;
        this.stopped = false;
        this.data = null;
        var formEls = document.form1.elements;
        if (dataField) {
            for (var i = 0; i < formEls.length - 1; i++) {
                if (formEls[i].name == dataField) {
                    this.data = formEls[i];
                    i = formEls.length + 1;
                }
            }
        }

        myTimer.prototype.go = function() {
            if (this.value > 0 && this.stopped == false) {
                this.value = (this.value - this.interval);
                if (this.data) {
                    this.data.value = this.value;
                }
                var current = this.value;
                this.OutputCntrl.innerHTML = this.Hours(current) + ':' + this.Minutes(current) + ':' + this.Seconds(current);
                this.currentTimeOut = setTimeout("Timer.go()", this.interval);
            }
            else {                
                alert('Time Out!');
                window.location('index.aspx');
            }



        }
        myTimer.prototype.stop = function() {
            this.stopped = true;
            if (this.currentTimeOut != null) {
                clearTimeout(this.currentTimeout);
            }
        }
        myTimer.prototype.Hours = function(value) {
            return Math.floor(value / 3600000);
        }
        myTimer.prototype.Minutes = function(value) {
            return Math.floor((value - (this.Hours(value) * 3600000)) / 60000);
        }
        myTimer.prototype.Seconds = function(value) {
            var hoursMillSecs = (this.Hours(value) * 3600000)
            var minutesMillSecs = (this.Minutes(value) * 60000)
            var total = (hoursMillSecs + minutesMillSecs)
            var ans = Math.floor(((this.value - total) % 60000) / 1000);

            if (ans < 10)
                return "0" + ans;

            return ans;
        }
    }           



</script>

Exam_paper.ascx

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lbTime.Text = ((Final_Paper)this.Page).message();
        foreach (ListViewItem item in paper_list.Items)
        {
            RadioButton ansA = (RadioButton)item.FindControl("ansA");
            RadioButton ansB = (RadioButton)item.FindControl("ansB");
            RadioButton ansC = (RadioButton)item.FindControl("ansC");
            RadioButton ansD = (RadioButton)item.FindControl("ansD");
            Label rightAns = (Label)item.FindControl("rightAns");


            if (ansA.Checked && rightAns.Text == "ansA")
            {
                a = a + 1;
            }
            else if (ansB.Checked && rightAns.Text == "ansB")
            {
                a = a + 1;
            }
            else if (ansC.Checked && rightAns.Text == "ansC")
            {
                a = a + 1;
            }
            else if (ansD.Checked && rightAns.Text == "ansD")
            {
                a = a + 1;
            }
            else
            {
                a = a + 0;
            }
        }

        marks = (float)2 * (float)a;

        addResult();

        Session.Remove("stud");
        if (Session["stud"] == null)
        {
            Response.Redirect("index.aspx");
        }            

    }
4

3 回答 3

2

使您的 btnSubmit 成为您的用户控件的公共属性,以便可以从您的 aspx 文件访问它,同时可以从客户端脚本访问它。这是一个小例子。

用户控制代码:

public partial class Sample : System.Web.UI.UserControl
{
    public Button SubmitButton
    { get { return this.btnSubmit; } set { this.btnSubmit = value; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //Do Something
    }
}

用户控件 HTML:

<asp:Button ID="btnSubmit" runat="server" Text="Button" 
    onclick="btnSubmit_Click" />

ASPX:

<head runat="server">
    <script>
        setTimeout(function () {
            document.getElementById("<%=Sample1.SubmitButton.ClientID %>").click();
        }, 2000);
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:Sample ID="Sample1" runat="server" />
    </div>
    </form>
</body>
</html>

祝你好运!

于 2012-08-22T22:16:56.273 回答
0

提交按钮在哪里都没关系。你只需要它的ID。然后,在超时调用按钮的单击方法。像这样的东西:

document.getElementById("mySubmitExamButtonId").click();
于 2012-08-22T22:13:41.447 回答
0

您可以添加一个隐藏按钮,然后附加 btnSubmit_click 事件。然后您可以使用 javascript 调用事件

__doPostBack('<%=btnSubmit.ClientID%>',"OnClick")
于 2012-08-22T22:15:58.080 回答