2010-02-24 100 views
0

我正在使用网站应用程序,在该网站中,我创建了下拉列表和文本框作为动态...该下拉列表包含费用列表...我在下拉列表中选择一种费用类型和在文本框中输入一些金额..然后我在Dropdownlist中再次选择一种类型的费用,并在文本框中输入一些金额...最后,我选择了一个文本命名为Dropdownlist中的总金额,它必须自动生成所有的总金额textboxes(beforeCreated)在文本框的末尾...计算动态生成的文本框中的总价值

我应该如何获得文本框结束的总价值.... 任何人请告诉我这个的解决方案.. 在此先感谢.. 。

我的代码: //这个函数用于创建将被添加动态生成的下拉列表和文本框

 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(); 

} 

现在我应如何做到这一点的网格?

回答

0

你必须把第三下拉列表中的UpdatePanel(阿贾克斯)&然后在其Onselectedindexchanged事件检索第一&第二个文本框的值将它们添加&在第三个文本框显示。下面是它 客户端代码的代码:

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

服务器端代码:

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

纠正我,如果我错了,&给予好评,如果它工作& alwayz只能把您要动态控制updatepanel中的更新。 你也可以通过jQuery来做到这一点,如果你想要,我也可以写它的代码?

+0

我劝分配总对文本框输入做一些验证,如果用户输入'one'而不是1,或者除了数字之外还有其他内容? – 2010-02-24 08:24:09

+0

@Tony:我想他只是在询问服务器端代码,他可能会早些时候验证他的页面,以便文本框只能有int类型的值。是不是在 – 2010-02-24 10:18:44

0

您不指定是否要在客户端(javascript)或服务器端执行此操作。有关如何做客户端的答案,check this SO post。你的问题基本上没有什么不同,你可能需要做的是在计算过程中包含一两个额外的控制,如果你这样做,那么只需使用相同的方法展开代码。

此外,为什么你包括你的数据表在视图状态?为什么不把它存储在会话中?

1

所有你需要的是,当你在你的下拉列表量变到质变指数,阅读文本框的值,并把它添加到最后总像下面

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

我应该在哪里分配这个总数? – Suryakavitha 2010-02-24 09:49:21

+0

很明显。当DropDown Selected Index改变时----毕竟,你可以在任何你喜欢的地方显示Total。或者甚至可以在页面的某个位置显示总数,并且每当用户更改索引时,总值就会显示总数。 – 2010-02-25 04:39:30

1

就在DropDown_SelectedIndexChanged

protected void DropDown_SelectedIndexChanged(object sender, EventArgs e) 
{ 

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

我没有文本框作为textbox1,textbox2和textbox3 ...我已经生成该文本框为动态... – Suryakavitha 2010-02-24 10:30:07

+0

然后给你的动态生成的事件编号 – 2010-02-24 11:10:00

+0

是你的问题解决或不? – 2010-02-25 04:35:54