所有文本框值的总和 [英] Sum of all textboxes value

查看:102
本文介绍了所有文本框值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取每个文本框的循环onblur内的所有文本框值的总和,并使用javascript将sum的值设置为文本框外循环的值?

代码如下:-------

How to get sum of all textboxes value inside loop onblur of each textbox and set the value of sum to the value of textbox out side loop using javascript?

code follows:-------

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 1; i <= 1; i++)
        {
            createTable();
        }
    }
    public void createTable()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlDataReader rdr;
        SqlCommand comm = new SqlCommand();
        comm.CommandType = CommandType.StoredProcedure;
        //comm.CommandType = CommandType.Text;
        //comm.CommandText = "select username from maccount";
        comm.CommandText = "maccount_username_sp";
        comm.Connection = conn;
        conn.Open();
        rdr = comm.ExecuteReader();
        Table t = new Table();
        for(int i=0;i<=5;i++)
        {
            
            
            TableCell c = new TableCell();
            c.CssClass = "mystyle";
            c.Text = "HI";
            TableRow r = new TableRow();
            r.Controls.Add(c);
            t.Controls.Add(r);
            TextBox tb = new TextBox();
            //tb.Text = rdr.ToString();
           // tb.Text = rdr.GetString(rdr.GetOrdinal("username"));
           // tb.Text = rdr.GetValue(0).ToString();
            c.Controls.Add(tb);
            tb.ID = i.ToString();
            tb.Attributes.Add("onblur", "javascript:sum('WebUserControl1_" + i + "');");
            Table tUpper = new Table();
            TableRow rUpper = new TableRow();
            TableCell cUpper = new TableCell();
            rUpper.Controls.Add(cUpper);
            cUpper.Controls.Add(t);
            tUpper.Controls.Add(rUpper);
            cUpper.CssClass = "mystyle1";
            this.Controls.Add(tUpper);
           // return tUpper;
        }
        TableRow r1 = new TableRow();
        TableCell c1 = new TableCell();
        TextBox tb1 = new TextBox();
        c1.Controls.Add(tb1);
        r1.Controls.Add(c1);
        t.Controls.Add(r1);
       
    }
}

推荐答案

到目前为止,您尝试了什么?

您需要将onblur JS事件绑定到所有文本框.在JS的onblur事件方法中,遍历所有文本框并计算总和.使用
将总和分配给所需的文本框
What have you tried so far?

You need to bind onblur JS event to all the textboxes. In the onblur event method in JS, loop through all the textboxes and calculate the sum. Assign the sum to the desired textbox using
document.getElementById("txtShowTotal").value = 120;



将"this"作为onBlur JS方法中的参数之一传递.事件触发时,它将帮助您访问JS中的当前文本框详细信息.



Pass ''this'' as one of the paramters in the onBlur JS method. It would help you access the current textbox details in JS when the event would fire.


这篇关于所有文本框值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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