I’m a bit stuck with refactoring my code to make a method available to another page. I have the following code which is giving me trouble. Maybe there is a better angle to come at this from? Essentially I’m trying to make a function static so I can reuse the code but it’s tripping me up.
public static Table BuildProjectTable(string CRNumber)
{
LinkButton SelectLink = new LinkButton();
SelectLink.ID = CRNumber + "," + ShipId + "," + StageId;
ProgressStatus CurrentStatus = AAGlobal.GetProgressStatus(CRNumber, ShipId, StageId);
SelectLink.Text = CurrentStatus.CurrentAssignee;
SelectLink.BackColor = CurrentStatus.CurrentStatus;
SelectLink.ForeColor = Color.White;
BodyCells[CellCount].BackColor = CurrentStatus.CurrentStatus;
BodyCells[CellCount].ForeColor = Color.White;
// This fails because SelectLink_Click isn't static (I think)
//
SelectLink.Click += new EventHandler(SelectLink_Click);
BodyCells[CellCount].Controls.Add(SelectLink);
CellCount++;
}
// If I make this static (fixing above error), Session, Response and lstProcess
// all complain an object reference is required
public void SelectLink_Click(object sender, EventArgs e)
{
LinkButton ClickedLink = (LinkButton)sender;
string[] ClickRef = ClickedLink.ID.ToString().Split(',');
Session["CR_NUMBER"] = ClickRef[0];
Session["SHIP_ID"] = ClickRef[1];
Session["STAGE_ID"] = ClickRef[2];
Session["PROCESS_NAME"] = lstProcess.SelectedValue;
Response.Redirect("~/EditStage.aspx");
}
What is the “lstProcess” ? Is it static ?
If it’s not, your problem might come from that…
Also, assuming you are making asp.net, Session, Response, Cookies,.. are not static and depends on a Request. This explains why you don’t have access to them..
Anyway, making an EventHandler static is not a good idea.
Maybe you should consider making a new static method that takes a Session and whatever you need in parameters and calling it from the EventHandler.