For redigereing af koden fra
http://www.eksperten.dk/spm/519269Til følgende :
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.Web.Security;
using ByteFX.Data.MySqlClient;
namespace Web100.tmpgalleri
{
public class tmpgalleri : System.Web.UI.Page
{
protected System.Web.UI.WebControls.PlaceHolder ButtonPlaceHolder;
protected System.Web.UI.WebControls.PlaceHolder GalleriPlaceHolder;
protected System.Web.UI.WebControls.Label Label1;
private DataAccess da = new DataAccess();
protected int PushedButtonId
{
get{return (object)Session["PushedButtonId"] != null ? (int)Session["PushedButtonId"] : -1;}
set{Session["PushedButtonId"] = value;}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
disableCaching();
InsertTopButtons();
if (PushedButtonId != -1)
{
InsertLowerButtons(PushedButtonId, true);
}
}
protected override void OnPreRender(EventArgs e)
{
// vi sikrer disposing inden vi afleverer siden
da.Dispose();
base.OnPreRender (e);
}
protected override void OnError(EventArgs e)
{
Response.Write("Der skete en fejl : " + Server.GetLastError().Message);
Server.ClearError();
}
private void disableCaching()
{
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Web.HttpContext.Current.Response.Cache.SetExpires(System.DateTime.Now);
System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
System.Web.HttpContext.Current.Response.AddHeader("cache-control", "no-store");
System.Web.HttpContext.Current.Response.Expires = 0;
}
private void InsertTopButtons()
{
MySqlDataReader reader = da.GetTopButtons();
while (reader.Read())
{
LinkButton button = new LinkButton();
button.CommandArgument = reader.GetString(0);
button.Text = reader.GetString(1);
button.Click += new EventHandler(this.Group_Selected);
ButtonPlaceHolder.Controls.Add(button);
ButtonPlaceHolder.Controls.Add(new LiteralControl(" "));
}
da.CloseReader(reader);
}
public void Group_Selected(object sender, EventArgs e)
{
PushedButtonId = int.Parse(((LinkButton)sender).CommandArgument);
Reload();
}
private void InsertLowerButtons(int topButtonId, bool callFromPaLo)
{
GalleriPlaceHolder.Controls.Clear();
MySqlDataReader reader = da.GetLowerButtons(topButtonId, callFromPaLo);
while (reader.Read())
{
LinkButton galButton = new LinkButton();
galButton.CommandArgument = reader.GetString(0);
galButton.Text = reader.GetString(1);
galButton.Click += new EventHandler(this.Gallery_Selected);
GalleriPlaceHolder.Controls.Add(galButton);
GalleriPlaceHolder.Controls.Add(new LiteralControl("<br>"));
}
da.CloseReader(reader);
}
public void Gallery_Selected(object sender, EventArgs e)
{
Label1.Text = "Gallery selected sender: " + ((LinkButton)sender).Text;
}
private void Reload()
{
Response.Redirect(Request.RawUrl, true);
}
}
}