我知道HTTP是无状态的,这其中就是动态的控件状态没有被服务器记下。如有解法希望能提供以上两个问题的程序文件.cs。
解决方案 »
- 无法将类型为“MYL.SQL.MyHander”的对象强制转换为类型“System.Web.UI.Page”。
- 如何给服务器textbox控件增加客户端失去焦点时候,在客户端验证其输入的内容?
- 想注册一个Gmail邮箱!请有Gmail的朋友给我发个邀请!谢谢,在线等
- HtmlInputFile上传文件出错了,请各位大虾帮助(asp.net+C#)
- 关于水晶报表的问题?
- 求救!简单问题!!!!!!
- 100分:这样的效果,数据库如何实现?
- treeview下载的问题
- 在dataGrid里绑定数据为什么总出现错误?
- 在邮件发送程序中,怎样确认对方的e_mail地址是真地址?求助!!
- 帮忙呀!!!,送分送分!
- 查看数据结构问题,高分!
{
ListData();
}
private void ListData()
{
System.Data.SqlClient.SqlConnection connect;
string sql;
System.Data.SqlClient.SqlCommand command;
command.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
System.Web.UI.HtmlControls.HtmlTableRow RecordRow = new HtmlTableRow();
RecordRow.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#9FBEDD';this.style.cursor='hand';";
RecordRow.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#FFFFFF';";
System.Web.UI.HtmlControls.HtmlTableCell DeleteCell = new HtmlTableCell();
DeleteCell.Align = "Center";
DeleteCell.Width = "20%";
DeleteCell.Attributes["onclick"] = "javascript:return confirm('你确认要删除吗?');";
System.Web.UI.WebControls.LinkButton DeleteButton = new LinkButton();
DeleteButton.Text = "删除";
DeleteButton.Click += new System.EventHandler(this.ExamDeleteFlush);
DeleteButton.CommandArgument = (reader.GetValue(3)).ToString();
DeleteCell.Controls.Add(DeleteButton);
RecordRow.Controls.Add(DeleteCell);
this.ExamItemTable.Controls.Add(RecordRow);
}
command.Connection.Close();
}
private void ExamDeleteFlush(object sender,System.EventArgs e)
{
System.Web.UI.WebControls.LinkButton cur = (System.Web.UI.WebControls.LinkButton)sender;
System.Data.SqlClient.SqlConnection connect;
string sql;
System.Data.SqlClient.SqlCommand command;
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
ListData();
}
上面就是经过简化的xx.ascx的相关代码。
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;
using System.Configuration;namespace boole
{
using boole.sys;
using boole.news;
using boole.forum;
using boole.file;
using boole.train;
using boole.workforce;
using boole.vod;
using boole.tech;
using boole.admin;
using boole.log;
//using boole.oper;
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class index : System.Web.UI.Page,IBase
{
protected System.Web.UI.HtmlControls.HtmlTableCell MainCell;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面 this.MainCell.Controls.Clear();
/*
* Author:ZhuZhengGuang
* Date:2003/06/26
* Action:Modify
* Function:Dymanic Load User's Controls
*/
string modname;
try
{
modname = Request.QueryString["module"];
}
catch
{
modname = "log";
}
string filename;
try
{
filename = Request.QueryString["file"];
}
catch
{
filename = "index";
}
System.Web.UI.Control uc;
try
{
uc = this.Page.LoadControl(modname + "\\" + filename + ".ascx");
this.MainCell.Controls.Add(uc);
}
catch
{
try
{
uc = this.Page.LoadControl(modname + "\\index.ascx");
this.MainCell.Controls.Add(uc);
}
catch(Exception err)
{
System.Web.UI.WebControls.Label errLabel = new Label();
errLabel.Text = err.ToString();
this.MainCell.Controls.Add(errLabel);
}
catch
{
uc = this.Page.LoadControl("log\\index.ascx");
this.MainCell.Controls.Add(uc);
}
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion }
}
上面是网站的唯一可运行文件的cs内容
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; using System.Data.SqlClient;
using System.Web.SessionState;
using System.Configuration; using boole; /// <summary>
/// examadmin 的摘要说明。
/// </summary>
public abstract class examadmin : System.Web.UI.UserControl,IBase
{
protected System.Web.UI.WebControls.HyperLink AddExamLink; protected System.Web.UI.HtmlControls.HtmlTable ExamItemTable; protected System.Web.UI.WebControls.Label SumExamLabel;
protected System.Web.UI.WebControls.Label SumPageLabel; protected System.Web.UI.WebControls.Label CurrPageLabel; protected System.Web.UI.WebControls.DropDownList PageSelectList;
protected System.Web.UI.HtmlControls.HtmlTableCell FPNLCell;
protected int RecordsOfPage;
protected int CurrentPage;
private string connectstring; private void Page_Load(object sender, System.EventArgs e)
{
connectstring = ConfigurationSettings.AppSettings["connectstring"];
try
{
this.RecordsOfPage = int.Parse(Request.QueryString["PageRecords"]);
}
catch
{
this.RecordsOfPage = 5;
}
InitFPNL();
ListData();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.PageSelectList.SelectedIndexChanged += new System.EventHandler(this.DropDownSelectedIndexChange);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DropDownSelectedIndexChange(object sender,System.EventArgs e)
{
this.RecordsOfPage = int.Parse(this.PageSelectList.SelectedItem.Value);
InitFPNL();
ListData();
}
private void InitFPNL()
{
try
{
this.CurrentPage = int.Parse(Request.QueryString["CurrPage"]);
}
catch
{
this.CurrentPage = 1;
}
this.CurrPageLabel.Text = this.CurrentPage.ToString();
System.Data.SqlClient.SqlConnection connect = new SqlConnection(this.connectstring);
string sql = "select count(*) from bool_exam_exam";
System.Data.SqlClient.SqlCommand command = new SqlCommand(sql,connect);
command.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
reader.Read();
int totalrecords = int.Parse((reader.GetValue(0)).ToString());
command.Connection.Close();
this.RecordsOfPage = int.Parse(this.PageSelectList.SelectedItem.Value);
int tmpmod = totalrecords%this.RecordsOfPage;
int countpages = totalrecords/this.RecordsOfPage;
int totalpages = (tmpmod > 0)?(1+countpages):countpages;
this.SumExamLabel.Text = totalrecords.ToString();
this.SumPageLabel.Text = totalpages.ToString(); if (totalpages > 1)
{
this.FPNLCell.Controls.Clear();
System.Web.UI.HtmlControls.HtmlTable SelectPageTable = new HtmlTable();
SelectPageTable.CellSpacing = 0;
SelectPageTable.CellPadding = 0;
SelectPageTable.Border = 0;
SelectPageTable.Width = "100%";
System.Web.UI.HtmlControls.HtmlTableRow SelectPageRow = new HtmlTableRow();
if (totalpages > 1 && this.CurrentPage > 1)
{
System.Web.UI.HtmlControls.HtmlTableCell HomePageCell = new HtmlTableCell();
HomePageCell.Align = "Center";
HomePageCell.Width = "20%";
System.Web.UI.WebControls.HyperLink HomePageLink = new HyperLink();
HomePageLink.Text = "首页";
HomePageLink.NavigateUrl = "../index.aspx?module=train&file=examadmin&CurrPage=1&PageRecords=" + this.RecordsOfPage;
HomePageCell.Controls.Add(HomePageLink);
SelectPageRow.Controls.Add(HomePageCell); System.Web.UI.HtmlControls.HtmlTableCell PrevPageCell = new HtmlTableCell();
PrevPageCell.Align = "Center";
PrevPageCell.Width = "20%";
System.Web.UI.WebControls.HyperLink PrevPageLink = new HyperLink();
PrevPageLink.Text = "上一页";
PrevPageLink.NavigateUrl = "../index.aspx?module=train&file=examadmin&CurrPage=" + (this.CurrentPage - 1) + "&PageRecords=" + this.RecordsOfPage;
PrevPageCell.Controls.Add(PrevPageLink);
SelectPageRow.Controls.Add(PrevPageCell);
}
if (totalpages > 1 && totalpages > this.CurrentPage)
{
System.Web.UI.HtmlControls.HtmlTableCell NextPageCell = new HtmlTableCell();
NextPageCell.Align = "Center";
NextPageCell.Width = "20%";
System.Web.UI.WebControls.HyperLink NextPageLink = new HyperLink();
NextPageLink.Text = "下一页";
NextPageLink.NavigateUrl = "../index.aspx?module=train&file=examadmin&CurrPage=" + (this.CurrentPage + 1) + "&PageRecords=" + this.RecordsOfPage;
NextPageCell.Controls.Add(NextPageLink);
SelectPageRow.Controls.Add(NextPageCell); System.Web.UI.HtmlControls.HtmlTableCell LastPageCell = new HtmlTableCell();
LastPageCell.Align = "Center";
LastPageCell.Width = "20%";
System.Web.UI.WebControls.HyperLink LastPageLink = new HyperLink();
LastPageLink.Text = "尾页";
LastPageLink.NavigateUrl = "../index.aspx?module=train&file=examadmin&CurrPage=" + totalpages + "&PageRecords=" + this.RecordsOfPage;
LastPageCell.Controls.Add(LastPageLink);
SelectPageRow.Controls.Add(LastPageCell);
}
SelectPageTable.Controls.Add(SelectPageRow);
this.FPNLCell.Controls.Add(SelectPageTable);
}
else
{
this.FPNLCell.InnerHtml = " ";
}
} private void ListData()
{
System.Data.SqlClient.SqlConnection connect = new SqlConnection(this.connectstring);
string sql = "select ExamName,convert(char(10),BeginTime,111),convert(char(10),EndTime,111),ExamID from bool_exam_exam order by ExamID";
System.Data.SqlClient.SqlCommand command = new SqlCommand(sql,connect);
command.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
this.ExamItemTable.Controls.Clear();
while(reader.Read())
{
System.Web.UI.HtmlControls.HtmlTableRow RecordRow = new HtmlTableRow();
RecordRow.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#9FBEDD';this.style.cursor='hand';";
RecordRow.Attributes["onmouseout"] = "javascript:this.style.backgroundColor='#FFFFFF';";
System.Web.UI.HtmlControls.HtmlTableCell DeleteCell = new HtmlTableCell();
DeleteCell.Align = "Center";
DeleteCell.Width = "20%";
DeleteCell.Attributes["onclick"] = "javascript:return confirm('你确认要删除吗?');";
System.Web.UI.WebControls.LinkButton DeleteButton = new LinkButton();
DeleteButton.Text = "删除";
DeleteButton.Click += new System.EventHandler(this.ExamDeleteFlush);
DeleteButton.CommandArgument = (reader.GetValue(3)).ToString();
DeleteCell.Controls.Add(DeleteButton);
RecordRow.Controls.Add(DeleteCell);
this.ExamItemTable.Controls.Add(RecordRow);
}
command.Connection.Close();
} private void ExamDeleteFlush(object sender,System.EventArgs e)
{
System.Web.UI.WebControls.LinkButton cur = (System.Web.UI.WebControls.LinkButton)sender;
System.Data.SqlClient.SqlConnection connect = new SqlConnection(this.connectstring);
string sql = "delete from bool_exam_exam where ExamId =" + cur.CommandArgument;
System.Data.SqlClient.SqlCommand command = new SqlCommand(sql,connect);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
InitFPNL();
ListData();
}上面就是xx.ascx控件的cs内容。
所以就出现你的这样情况,第一次载入页面,页面上是Page_Load载入的LinkButton控件,因而可以响应LinkButton事件,响应了时间后再次生成的页面就是由LinkButton事件载入的那些LinkButton控件了,所以第二次点击LinkButton就不会响应事件,不过Page_Load又一次载入的LinkButton,又能够响应事件了.如此反复.