如何使用此代码显示百分比 [英] How to disply percentage with this code

查看:96
本文介绍了如何使用此代码显示百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在textbox8中显示百分比?其中txt6是自动获得的,而txt7是asp.net c#中给出的500个框值,而在单击txtbox 8时却没有任何区别.

How to disply percentage in textbox8? where txt6 got automatically and txt7 is 500 box value given in asp.net c# without taking anything between when click on txtbox 8 it should come.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnkB_Click(object sender, EventArgs e)
    {
        lbl1.Visible = lblMes.Visible=lbl2.Visible = lbl3.Visible = lbl4.Visible = lbl5.Visible = lbl6.Visible = lbl7.Visible = lbl8.Visible =lbl9.Visible = txt1.Visible = txt2.Visible = txt3.Visible = txt4.Visible = txt5.Visible = txt6.Visible = txt7.Visible = lbl10.Visible=btnS.Visible=btnC.Visible=txt8.Visible=true;
    }
    protected void txt1_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txt1.Text)) && (!string.IsNullOrEmpty(txt2.Text)) && (!string.IsNullOrEmpty(txt3.Text)) && (!string.IsNullOrEmpty(txt4.Text)) && (!string.IsNullOrEmpty(txt5.Text)))
        {
            txt6.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text) + Convert.ToInt32(txt3.Text) + Convert.ToInt32(txt4.Text) + Convert.ToInt32(txt5.Text)).ToString();
        }
    }

    protected void txt2_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txt1.Text)) && (!string.IsNullOrEmpty(txt2.Text)) && (!string.IsNullOrEmpty(txt3.Text)) && (!string.IsNullOrEmpty(txt4.Text)) && (!string.IsNullOrEmpty(txt5.Text)))
        {
            txt6.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text) + Convert.ToInt32(txt3.Text) + Convert.ToInt32(txt4.Text) + Convert.ToInt32(txt5.Text)).ToString();
        }

    }
    protected void txt3_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txt1.Text)) && (!string.IsNullOrEmpty(txt2.Text)) && (!string.IsNullOrEmpty(txt3.Text)) && (!string.IsNullOrEmpty(txt4.Text)) && (!string.IsNullOrEmpty(txt5.Text)))
        {
            txt6.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text) + Convert.ToInt32(txt3.Text) + Convert.ToInt32(txt4.Text) + Convert.ToInt32(txt5.Text)).ToString();
        }


    }
    protected void txt4_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txt1.Text)) && (!string.IsNullOrEmpty(txt2.Text)) && (!string.IsNullOrEmpty(txt3.Text)) && (!string.IsNullOrEmpty(txt4.Text)) && (!string.IsNullOrEmpty(txt5.Text)))
        {
            txt6.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text) + Convert.ToInt32(txt3.Text) + Convert.ToInt32(txt4.Text) + Convert.ToInt32(txt5.Text)).ToString();
        }


    }
    protected void txt5_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txt1.Text)) && (!string.IsNullOrEmpty(txt2.Text)) && (!string.IsNullOrEmpty(txt3.Text)) && (!string.IsNullOrEmpty(txt4.Text)) && (!string.IsNullOrEmpty(txt5.Text)))
        {
            txt6.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text) + Convert.ToInt32(txt3.Text) + Convert.ToInt32(txt4.Text) + Convert.ToInt32(txt5.Text)).ToString();
        }

    }

    protected void txt8_TextChanged1(object sender, EventArgs e)
    {

    }
}






h






h

推荐答案

第一件事.回到VS设计器中,将文本"框的名称更改为描述其功能的名称:tbOrderQuantity或其他名称,而不是txt1.这将使您的代码更具可读性.

然后,用一个公共处理程序替换前五个文本框的处理程序:目前,您有五个相同的例程,这意味着如果发现问题或您的规范,您必须记住对所有这些例程进行相同的更改变化.

在那之后,看看在其中放置一些错误检查/捕获,这样您的程序不仅会导致异常,而且在用户第一次按下错误的键时会崩溃.

最后,算出您实际想要做的事情:您知道如何获取文本框内容,您知道如何转换数据类型,并且您知道如何设置新的文本框内容.因此,确定一个百分比应该并不困难.照原样,我们不知道您要达到的目标.
First things first. Go back to the VS designer and change the names of your Text boxes to something that describes what that do: tbOrderQuantity or whatever, rather than txt1. It will make your code a whole lot more readable.

Then, replace the handlers for the first five text boxes with a common handler: at the moment you have five identical routines, which means that you must remember to make the same change in all of them if you find a problem, or if your specification changes.

After that, look at putting some error checking / capture in there, so your program won''t just cause an exception and crash out the first time the user hits the wrong key.

Finally, work out what you actually want to do: you know how to get a text box content, you know how to convert datatypes, and you know how to set a new textbox content. So working out a percentage shouldn''t be difficult. As it is, we don''t know exactly what you are trying to achieve.


这篇关于如何使用此代码显示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆