简体   繁体   English

在动态生成的文本框中计算总价值

[英]calculate Total value in Dynamically generated textbox

I am using a Website application,In that website i have created dropdownlist and textboxes as dynamically... That dropdown list contains fees list... I select one type of fees in Drop downlist and enter some amount in textbox.. then i select again one type of fees in Dropdownlist and enter some amount in textbox... and finally i select a text named as Total amount in Dropdownlist it have to automatically generate the total value of the all the textboxes(beforeCreated) in the end of the textbox... 我正在使用网站应用程序,在该网站中,我已动态创建了下拉列表和文本框...该下拉列表包含费用列表...我在“下拉列表”中选择一种费用类型,然后在文本框中输入一些金额。再次在Dropdownlist中收取一种费用,并在文本框中输入一些金额...最后,我在Dropdownlist中选择一个名为Total Value的文本,它必须自动在文本框的末尾生成所有文本框的总价值(beforeCreated) ...

How shall i get the total value at end of the textbox.... Anyone plz tell me the solution of this.. Thanks in Advance... 我应如何在文本框的末尾获得总价值。...任何人请告诉我解决方案..在此先感谢...

My Code : //This function for creating a grid Which will be added the dynamically generated Dropdownlist and textbox 我的代码://此函数用于创建网格,将在网格中添加动态生成的Dropdownlist和文本框

     public void SetInitalRowFees()
    {
    DataTable dtFees = new DataTable();
    DataRow drFees = null;
    dtFees.Columns.Add(new DataColumn("Sno", typeof(string)));
    dtFees.Columns.Add(new DataColumn("Column1", typeof(string)));
    dtFees.Columns.Add(new DataColumn("Column2", typeof(string)));
    drFees = dtFees.NewRow();
    drFees["Sno"] = 1;
    drFees["Column1"] = string.Empty;
    drFees["Column2"] = string.Empty;
    dtFees.Rows.Add(drFees);
    //Store the DataTable in ViewState
    ViewState["CurrentTableFees"] = dtFees;
    // Session["CurrentTable"] = dt;
    GrdFeesStructure.DataSource = dtFees.DefaultView;
    GrdFeesStructure.DataBind();
}



  //This function is used to add a new dropdownlist and textbox controls

    public void AddFeesStructureGrid()
    {
    int rowIndex = 0;
    if (ViewState["CurrentTableFees"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableFees"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count > 0)
        {
            for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
            {
                DropDownList box1 = (DropDownList)GrdFeesStructure.Rows[rowIndex].Cells[1].FindControl("ddlFeesDetails");
                TextBox box2 = (TextBox)GrdFeesStructure.Rows[rowIndex].Cells[2].FindControl("txtAmount");
                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["Sno"] = i + 1;
                dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTableFees"] = dtCurrentTable;
            GrdFeesStructure.DataSource = dtCurrentTable;
            GrdFeesStructure.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    SetFeesStructureData();

}

Now How shall i do this? 现在我该怎么做?

All you need is that when you are changeing Index in your dropdownlist, read the Textbox value and add it to last total like below 您需要做的就是,当您在下拉列表中更改索引时,请阅读“文本框”值并将其添加到最后的总计中,如下所示

 protected void DropDown_SelectedIndexChanged(object sender, EventArgse)
 {
     // your other code
     int total += int.parse(TextBox1.Text);
 }

Just assign total in DropDown_SelectedIndexChanged 只需在DropDown_SelectedIndexChanged中分配总计

 protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
 {

     int total = Convert.ToInt32(TextBox1.Text) + Convert.Toint32(TextBox2.Text);
     TextBox3.Text = total;
 }

You have to put third dropdownlist in updatepanel(ajax) & then on its Onselectedindexchanged event retrieve the first & second textbox's value add them & display it in third textbox. 您必须将第三个下拉列表放在updatepanel(ajax)中,然后在其Onselectedindexchanged事件上检索第一个和第二个文本框的值,然后将它们添加并显示在第三个文本框中。 Here's the code for it Client Side code: 这是它的代码客户端代码:

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:DropDownList ID="ddlTotal" runat="server" OnSelectedIndexChanged="totalChange"></asp:DropDownList></ContentTemplate>
        </asp:UpdatePanel>

Server Side Code: 服务器端代码:

 protected void totalChange(object sender, EventArgs e)
        {
            txtTotalFee.Text = Convert.ToInt32(txtFee1.Text)+ Convert.ToInt32(txtFee2.Text;
        }

Correct me if i am wrong & upvote if it works & alwayz only put the control on which you want dynamic updation in updatepanel. 如果我错了,请纠正我,如果可以,请upvote,并且始终将要动态更新的控件放在updatepanel中。 You can also do it through jquery i can write the code of it too if u want? 您也可以通过jquery来做,如果您愿意,我也可以编写它的代码?

You don't specify whether you want to do it client side (javascript) or server side. 您无需指定是客户端(javascript)还是服务器端。 For an answer showing you how to do it client side, check this SO post . 要获得在客户端显示如何操作的答案, 请查看此SO post Your question is basically no different, all you might have to do is include an extra control or two in the calculation process, if you do then just expand the code using the same method. 您的问题基本上没有什么不同,您可能要做的就是在计算过程中包括一个或两个额外的控件,如果您这样做,则只需使用相同的方法扩展代码即可。

Also, why are you including your datatable in viewstate? 另外,为什么要在viewstate中包含数据表? Why not store it in session? 为什么不将其存储在会话中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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