Reputation: 1411
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...
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
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?
Upvotes: 0
Views: 3342
Reputation: 493
Just assign total in DropDown_SelectedIndexChanged
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
int total = Convert.ToInt32(TextBox1.Text) + Convert.Toint32(TextBox2.Text);
TextBox3.Text = total;
}
Upvotes: 1
Reputation: 12610
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);
}
Upvotes: 1
Reputation: 49974
You don't specify whether you want to do it client side (javascript) or server side. For an answer showing you how to do it client side, check this 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? Why not store it in session?
Upvotes: 0
Reputation: 493
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. 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. You can also do it through jquery i can write the code of it too if u want?
Upvotes: 0