请问,想实现点击一个按钮,能实现本页面的内容改变,该怎么做?比如 首页的新闻是国际新闻,点按钮后,首页显示的是国内新闻。

解决方案 »

  1.   

    看看这个自定义控件的代码,有帮助!!
    namespace ch09.CommonControl
    {
    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; /// <summary>
    /// MyToolBar 的摘要说明。
    /// </summary>
    public class MyToolBar : System.Web.UI.UserControl
    {
    public delegate void delOnButtonClick(int id);
    public event delOnButtonClick OnButtonClick; private void Page_Load(object sender, System.EventArgs e)
    {
    CreateButtons();
    } private void CreateButtons()
    {
    SqlConnection cn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=;");
    SqlDataAdapter da = new SqlDataAdapter("select * from employees",cn);
    DataTable dt = new DataTable();
    da.Fill(dt); for(int i=0;i<dt.Rows.Count;i++)
    {
    LinkButton lb = new LinkButton();
    lb.Text = dt.Rows[i]["firstname"].ToString()+" | ";
    lb.CommandName = dt.Rows[i]["employeeid"].ToString();
    lb.Click += new EventHandler(lb_Click);
    this.Controls.Add(lb);
    }
    } private void lb_Click(object sender,System.EventArgs e)
    {
    LinkButton lb = (LinkButton)sender;
    OnButtonClick(int.Parse(lb.CommandName));
    } #region Web 窗体设计器生成的代码
    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
    }
    }
      

  2.   

    这是上面设及到的另一控件代码
    namespace ch09.CommonControl
    {
    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; /// <summary>
    /// MyMenuBar 的摘要说明。
    /// </summary>
    public class MyMenuBar : System.Web.UI.UserControl
    {
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } public int EmpID
    {
    set
    {
    if(value > 0)
    {
    CreateMenus(value);
    }
    }
    } private void CreateMenus(int id)
    {
    SqlConnection cn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=;");
    SqlDataAdapter da = new SqlDataAdapter("select shipname from orders where employeeid=" + id,cn);
    DataTable dt = new DataTable();
    da.Fill(dt); for(int i=0;i<dt.Rows.Count;i++)
    {
    HyperLink hl = new HyperLink();
    hl.Text = dt.Rows[i][0].ToString();
    hl.NavigateUrl = "";
    this.Controls.Add(hl); Literal li = new Literal();
    li.Text = "<br>";
    this.Controls.Add(li);
    } } #region Web 窗体设计器生成的代码
    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
    }
    }