C# 从aspx页面上的按钮调用类
我的页面似乎正在刷新。我无法在单击时调用C# 从aspx页面上的按钮调用类,c#,asp.net,class,onclick,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-t
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/>");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间乐透
{
公共部分类绘图:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共无效乐透测试()
{
字典编号=新字典();
随机生成器=新随机();
while(number.Count<6)
{
数字[generator.Next(1,49)]=1;
}
string[]lotto=numbers.Keys.OrderBy(n=>n).Select(s=>s.ToString()).ToArray();
foreach(乐透中的字符串)
{
响应。写入(_str);
响应。写入(“
”);
}
}
}
}
问题在于您正在发出客户端请求。将OnClientClick更改为OnClick(不带括号)
然后将方法签名更改为:
public void LottoTest(对象发送方,事件参数)
而不是public void LottoTest()
从标记中删除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>
欢迎来到乐透抽奖
将发送者和事件参数作为参数添加到方法中
代码
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间乐透
{
公共部分类绘图:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共无效LottoTest(对象发送方,事件参数e)
{
字典编号=新字典();
随机生成器=新随机();
while(number.Count<6)
{
数字[generator.Next(1,49)]=1;
}
string[]lotto=numbers.Keys.OrderBy(n=>n).Select(s=>s.ToString()).ToArray();
foreach(乐透中的字符串)
{
响应。写入(_str);
响应。写入(“
”);
}
}
}
}
Brilliant!:)对象发送方eventArgs e的用途是什么?发送方是调用事件的对象(即您在页面上单击的按钮),e表示与调用事件的对象关联的任何参数(即,在GridView中,e可以让您访问正在处理的行的信息,例如e.row.CommandArgument)。
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/>");
}
}
}
}