简体   繁体   中英

How to call .cs file from a .aspx.cs in asp.net

Is it possible to store this method with labels in class file? Example:

aspx.cs

   private void addSalat()
    {


                    decimal sum= Convert.ToDecimal(Label6.Text);
                    decimal sum2= Convert.ToDecimal(Label17.Text);
                    decimal sum3= Convert.ToDecimal(Label54.Text);
                    decimal sum4= Convert.ToDecimal(Label66.Text);
                    decimal sum5= Convert.ToDecimal(Label78.Text);
                    decimal sum6= Convert.ToDecimal(Label90.Text);
                    decimal sum7= Convert.ToDecimal(Label102.Text);
                    decimal sum8= Convert.ToDecimal(Label114.Text);
                    decimal sum9= Convert.ToDecimal(Label126.Text);

                    decimal sum= sum+ sum2+
                        sum3+ sum4+ sum5+ sum6+ sum7+ sum8
                        + sum9;

                    Label42.Text = sum.ToString();
                }

//do calculations here and call this method in aspx.cs file, i dont't know how to put here labels if is possible:

 public class Class1
{
    public void Sum()
    {

                    decimal sum= Convert.ToDecimal(Label6.Text);
                    decimal sum2= Convert.ToDecimal(Label17.Text);
                    decimal sum3= Convert.ToDecimal(Label54.Text);
                    decimal sum4= Convert.ToDecimal(Label66.Text);
                    decimal sum5= Convert.ToDecimal(Label78.Text);
                    decimal sum6= Convert.ToDecimal(Label90.Text);
                    decimal sum7= Convert.ToDecimal(Label102.Text);
                    decimal sum8= Convert.ToDecimal(Label114.Text);
                    decimal sum9= Convert.ToDecimal(Label126.Text);

                    decimal sum= sum+ sum2+
                        sum3+ sum4+ sum5+ sum6+ sum7+ sum8
                        + sum9;

                    Label42.Text = sum.ToString();
    }
}

You would have to do something like this:

private decimal Sum(decimal firstValue, params decimal[] moreValues)
{
    decimal val = firstValue;

    return val + moreValues.Sum(); // Note this is not recursion. It is calling the LINQ Sum() function.
}

And call it like this:

Sum(5.2); // returns 5.2
Sum(6, 10); // returns 16
Sum(1,2,3); // returns 6

Or something like this:

decimal sum = Sum(Convert.ToDecimal(Label6.Text), 
                  Convert.ToDecimal(Label17.Text), 
                  Convert.ToDecimal(Label54.Text)
                  // and so forth
                  );

I would just leave it in the code-behind of the page. Since you're basically just adding values there's not a lot to gain by splitting it into a different class. If you were doing more complex calculations that needed unit testing, etc. then I would consider splitting it out.

You could also make the code a little cleaner by using Linq to sum the values:

decimal sum = new [] {Label6, Label17, Label54, Label66, Label78, Label90, Label102, Label114, Label126}
              .Sum(lbl => Convert.ToDecimal(lbl.Text));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM