How would you refactor page members from ASPX code?

I have inherited a project that uses the following template to pass parameters from the code behind the aspx page. I know this is wrong, but I am on the fence to best reorganize it.

Code for:

using System;
using System.Web;

namespace BadPassing
{
    public partial class _Default : System.Web.UI.Page
    {
        private Random rnd = new Random(); //Is this bad?

        protected int numberOne; //this is bad
        protected int numberTwo; //this is bad

        protected void Page_Load(object sender, EventArgs e)
        {
            numberOne = rnd.Next(100);
            numberTwo = rnd.Next(100);
        }
    }
}

      

ASPX page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BadPassing._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Bad Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="http://www.google.com/search?q=<%=this.numberOne %>%2B<%=this.numberTwo %>">
            Add <%=this.numberOne %> and <%=this.numberTwo %> using google.
        </a>
    </div>
    </form>
</body>
</html>

      

I understand that numberOne and numberTwo are not thread safe and may cause incorrect behavior if two people load the page at the same time. Also, if the page references numberOne and numberTwo to store values โ€‹โ€‹between postbacks, multiple concurrent users can cause unexpected results.

I understand why the above method is so bad, and if so, how would you best refactor this code?

On the other hand, is it incorrect to store page level state services (like Random) as member variables of the page class?

0


source to share


3 answers


This code is fine. Member variables are created each time the page is loaded because a new instance of the class is created each time the page is loaded. If the variables were static then only then would you have problems loading 2 pages at the same moment.



Think about what kind of controls you are throwing onto the page. They are member variables, but they have no problem with multiple requests loading the page at the same time.

+4


source


Typically, if two users load the page at the same time, they will be using different instances of the page. Since numberOne and numberTwo are instance variables, there will be no shared state and no threading issues.



+1


source


Even though the code (as mentioned) is really not wrong, if you wanted to refactor it I would probably do something like lines

using System; using System.Web;

namespace BadPassing
{
    public partial class _Default : System.Web.UI.Page
    {
        private Random rnd = new Random(); //Is this bad?

        protected int numberOne; //this is bad
        protected int numberTwo; //this is bad

        protected void Page_Load(object sender, EventArgs e)
        {
            numberOne = rnd.Next(100);
            numberTwo = rnd.Next(100);
            searchLink.NavigationUrl = String.Format("http://www.google.com/search?q={0}%2B{1}", numberOne, numberTwo);
            searchLink.Text = String.Format("Add {0} and {1} using google", numberOne, numberTwo);
        }
    }
}

      

and then in the aspx page just add the control <asp:hyperlink>

0


source







All Articles