gpt4 book ai didi

c# - 从 aspx 页面上的按钮调用类

转载 作者:行者123 更新时间:2023-11-30 14:59:07 26 4
gpt4 key购买 nike

我的页面似乎刚刚刷新。我无法调用 LottoTest() 类 onclick。

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/>");
}
}
}
}

最佳答案

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

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

关于c# - 从 aspx 页面上的按钮调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com