我知道HTTP是无状态的,这其中就是动态的控件状态没有被服务器记下。如有解法希望能提供以上两个问题的程序文件.cs。

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    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的相关代码。
      

  2.   

    to zzg_zhu (朱正广) 把你的源代码都贴出来
      

  3.   

    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;
    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内容
      

  4.   

    namespace boole.train
    {
    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 = "&nbsp;";
    }
    } 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内容。
      

  5.   

    把你上面是网站的唯一可运行文件的cs内容的Page_Load中的代码全部的放到Page_Init事件中试试
      

  6.   

    我知道原因了:1, 在用户事件(比如点击了按钮,dropdownlist控件的index改变)中加入的控件,及其这些控件关联的事件,在下一次postback时是这些控件是不会别加入的,事件也就不会得到响应了.2,你的问题是在用户控件的Page_Load事件中有ListData()方法,建立的那些LinkButton,并关联了事件,在Page_Load事件中加入的控件在postback回来后是再次载入的,所以控件的事件也会得到响应.当DropDownSelectedIndexChange事件发生时,又调用了ListData()方法,清除掉了Page_Load中建立的那些控件,又新建了一次LinkButton控件并关联事件,而这些控件在以后的postback回来是不会别恢复的.
    所以就出现你的这样情况,第一次载入页面,页面上是Page_Load载入的LinkButton控件,因而可以响应LinkButton事件,响应了时间后再次生成的页面就是由LinkButton事件载入的那些LinkButton控件了,所以第二次点击LinkButton就不会响应事件,不过Page_Load又一次载入的LinkButton,又能够响应事件了.如此反复.
      

  7.   

    你说的没有错!LinkButton控件的事件产生的LinkButton控件,而产生的LinkButton控件是不能触发其事件的!该如何解决呢?我试过用第二个问题方法来解决,但产生了第二个问题!
      

  8.   

    在代码种添加控件,并要这个添加控件能够响应事件,则必须是在page_Load事件或之前就添加了,否则事件是得不到响应的,所以看你自己调整程序吧,设法把要响应事件的控件在Page_Load事件之间加入到页面