简体   繁体   中英

Working with numeric calculations in form controls using ASP.NET web forms

I've been practicing with asp.net web forms and controls and have now become a bit stuck on the best way to work with numeric data in user input forms for instance a simple example would be a input field called 'length' and one called width the user clicks a button and the result of L * W is calculated and display.

I have tested converting strings to int using Convert.ToInt32(strvalue2); and out-putting the results to the Console ok.

But when i try and convert them and output them to a form control using something like this:

        string strvaluel = length.Text;
        string strvalueW = width.Text;


            int numValuel = Convert.ToInt32(strvaluel);
            int numValueW = Convert.ToInt32(strvalueW);
            outputbox.Text   = numValuel * numValueW;  (error cannot explicitly convert    type 'int' to 'string'

I'm very new to asp.net so any help would be appreciated

Convert it to a string:

outputbox.Text = (numValuel * numValueW).ToString(); 

If you want more control over formatting (commas, etc.) use a format string:

outputbox.Text = string.Format("{0:N}",numValuel * numValueW); 

See the documentation on format strings for more details

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