从aspx页面上的按钮调用类

时间:2013-06-22 13:21:39

标签: c# asp.net class onclick

我的页面似乎正在刷新。我不能打电话给LottoTest()课程。

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Draw.aspx.cs" Inherits="Lotto.Draw" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Welcome to the Lotto draw</h1>
        <asp:Button ID="button1" runat="server" Text="DRAW" OnClientClick="LottoTest()" />
    </div>
    </form>
</body>
</html>

Draw.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lotto
{
    public partial class Draw : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public void LottoTest()
        {
            Dictionary<int, int> numbers = new Dictionary<int, int>();
            Random generator = new Random();
            while (numbers.Count < 6)
            {
                numbers[generator.Next(1, 49)] = 1;
            }

            string[] lotto = numbers.Keys.OrderBy(n => n).Select(s => s.ToString()).ToArray();

            foreach (String _str in lotto)
            {
                Response.Write(_str);
                Response.Write("<br/>");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

问题是您正在提出客户请求。将OnClientClick更改为OnClick(不带括号)。

然后将您的方法签名更改为: public void LottoTest(object sender, EventArgs e)代替public void LottoTest()

答案 1 :(得分:2)

从标记中删除OnClientClick并使用OnClick

标记

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Welcome to the Lotto draw</h1>
        <asp:Button ID="button1" runat="server" Text="DRAW" OnClick="LottoTest" />
    </div>
    </form>
</body>
</html>

将发件人和eventargs作为参数添加到您的方法

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Lotto
{
    public partial class Draw : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public void LottoTest(object sender, EventArgs e)
        {
            Dictionary<int, int> numbers = new Dictionary<int, int>();
            Random generator = new Random();
            while (numbers.Count < 6)
            {
                numbers[generator.Next(1, 49)] = 1;
            }

            string[] lotto = numbers.Keys.OrderBy(n => n).Select(s => s.ToString()).ToArray();

            foreach (String _str in lotto)
            {
                Response.Write(_str);
                Response.Write("<br/>");
            }
        }
    }
}