点击aspx上的button,无法运行定义在aspx的相关事件中的程序

hiyi 2003-03-06 10:23:32
WebForm2.aspx中
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebVisitDatabase.WebForm2" %>
增加一个button
<asp:button id="Button1" style="Z-INDEX: 107; LEFT: 299px; POSITION: absolute; TOP: 233px" runat="server" text="确认">
aspx.cs中
一个Lable
</asp:label><asp:label id="Message" style="Z-INDEX: 106; LEFT: 141px; POSITION: absolute; TOP: 236px" runat="server" Width="120px">

WebForm2.aspx.cs中
private void Button1_Click(object sender, System.EventArgs e)
{
message.text=3;
}
点击aspx上的button,为何label没反应?
...全文
173 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
blazelgh 2003-03-06
  • 打赏
  • 举报
回复
Page_load事件里面很多地方有错.编译都过不了.你是怎么运行的啊?
hiyi 2003-03-06
  • 打赏
  • 举报
回复
首先,非常感谢大家的踊跃襄助,但是经过我的逐一核对,尝试,还是不行。
我发现其实在载入的时候aspx.cs中的Page_Load函数就没有真正执行过,更别提其他的控件事件了。下面是我cs的全部代码。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebVisitDatabase
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RangeValidator RangeValidator1;
protected System.Web.UI.WebControls.AdRotator AdRotator1;
protected System.Web.UI.WebControls.AdRotator Adrotator1;
protected System.Web.UI.WebControls.Label UserID;
protected System.Web.UI.WebControls.DropDownList Category;
protected System.Web.UI.WebControls.Label Lable2;
protected System.Web.UI.WebControls.Label Message;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox1;

private void Page_Load(object sender, System.EventArgs e)
{
Calendar1.SelectedDates.Clear();
Calendar1.daynameformat="short";
Calendar1.SelectionMode="DayWeekMonth";
Calendar1.Font-Name="Verdana;Arial";
Calendar1.Font-Size="12px";
Calendar1.Height="180px";
Calendar1.Width="230px";
Calendar1.TodayDayStyle-Font-Bold="True";
Calendar1.DayHeaderStyle-Font-Bold="True";
Calendar1.OtherMonthDayStyle-ForeColor="gray";
Calendar1.TitleStyle-BackColor="#3366ff";
Calendar1.TitleStyle-ForeColor="white";
Calendar1.TitleStyle-Font-Bold="True";
Calendar1.SelectedDayStyle-BackColor="#ffcc66";
Calendar1.SelectedDayStyle-Font-Bold="True";
Calendar1.NextMonthText = "<img src='monthright.gif' border=0>";

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Adrotator1.AdCreated += new System.Web.UI.WebControls.AdCreatedEventHandler(this.Adrotator1_AdCreated);
this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion




private void Button1_Click(object sender, System.EventArgs e)
{
Message.text=3;
}

private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
Message.Text = "选定日期为:" + Calendar1.SelectedDate.ToShortDateString();

}

private void Adrotator1_AdCreated(object sender, System.Web.UI.WebControls.AdCreatedEventArgs e)
{

}

protected void Button2_Click(object sender, System.EventArgs e)
{
Message.Text = "选定日期为:" + Calendar1.SelectedDate.ToShortDateString();
}
}
}
MetallicaG 2003-03-06
  • 打赏
  • 举报
回复
1。首先应该声明相同的控件的名称,检查一下,呵呵。。。
2。OnClick=。。。是调用客户端的事件处理,而后还是调用服务器端的
3。声明this.Button1.Click+=new System.EventHandler(this.Button1_Click)
blazelgh 2003-03-06
  • 打赏
  • 举报
回复
当你拖一般Label到aspx页面的时候.aspx.cs文件会有相应的一行Label的声明,例如:
private System.Web.UI.WebControls.Label label1;
那么你要用这个声明的变量名label1来访问其属性:
private void Button1_Click(object sender, System.EventArgs e){
//message.text=3;
label1.Text=3;
}
你看看你哪里是什么名字咯.
timmy3310 2003-03-06
  • 打赏
  • 举报
回复
<asp:button id="Button1" style="Z-INDEX: 107; LEFT: 299px; POSITION: absolute; TOP: 233px" runat="server" text="确认" onclick="Button1_Click">

然后将private void Button1_Click(object sender, System.EventArgs e){...}
改为:
protected void Button1_Click(object sender, System.EventArgs e){...}
meetweb 2003-03-06
  • 打赏
  • 举报
回复
可能的
问题一:
Message没有大写,见
WebForm2.aspx.cs中
private void Button1_Click(object sender, System.EventArgs e)
{
//message.text=3;
Message.Text=3;
}
问题 二:
private void InitializeComponent()
{
//这里缺少激发button的event
this.Button1.Click += new System.EventHandler(Button1_Click);
}
hiyi 2003-03-06
  • 打赏
  • 举报
回复
我的事件程序是定义在aspx.cs中
如果加onclick="Button1_Click",则需将程序写在aspx中,这样我试过是可以的。
但是为什么放在aspx.cs就没反应呢???
cmsoft 2003-03-06
  • 打赏
  • 举报
回复
<asp:button id="Button1" style="Z-INDEX: 107; LEFT: 299px; POSITION: absolute; TOP: 233px" runat="server" text="确认" onclick="Button1_Click">
悟空师傅来了 2003-03-06
  • 打赏
  • 举报
回复
跟踪看看
usxue 2003-03-06
  • 打赏
  • 举报
回复
将事件的private 改成protected试试看!
呵呵……
hiyi 2003-03-06
  • 打赏
  • 举报
回复
我知道了,因为我是点击工具栏上‘view in brower'按钮来运行的。
assemblyinfo.cs和aspx.cs都有错,没有报出来
但奇怪的是assemblyinfo中关于
[assembly: AssemblyTitle("")]系统报'duplicate AssemblyTitle attribute'错误,但这段代码并不是我加的。我将其删掉就可以了

assemblyinfo.cs中的代码如下:
using System.Reflection;
using System.Runtime.CompilerServices;

//
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//


[assembly: AssemblyTitle("")]
//
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:


//
// In order to sign your assembly you must specify a key to use. Refer to the
// Microsoft .NET Framework documentation for more information on assembly signing.
//
// Use the attributes below to control which key is used for signing.
//
// Notes:
// (*) If no key is specified, the assembly is not signed.
// (*) KeyName refers to a key that has been installed in the Crypto Service
// Provider (CSP) on your machine. KeyFile refers to a file which contains
// a key.
// (*) If the KeyFile and the KeyName values are both specified, the
// following processing occurs:
// (1) If the KeyName can be found in the CSP, that key is used.
// (2) If the KeyName does not exist and the KeyFile does exist, the key
// in the KeyFile is installed into the CSP and used.
// (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility.
// When specifying the KeyFile, the location of the KeyFile should be
// relative to the "project output directory". The location of the project output
// directory is dependent on whether you are working with a local or web project.
// For local projects, the project output directory is defined as
// <Project Directory>\obj\<Configuration>. For example, if your KeyFile is
// located in the project directory, you would specify the AssemblyKeyFile
// attribute as [assembly: AssemblyKeyFile("..\\..\\mykey.snk")]
// For web projects, the project output directory is defined as
// %HOMEPATH%\VSWebCache\<Machine Name>\<Project Directory>\obj\<Configuration>.
// (*) Delay Signing is an advanced option - see the Microsoft .NET Framework
// documentation for more information on this.
//
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧