添加两列n将其结果显示在gridview的第三列中

时间:2011-01-05 11:06:49

标签: c# javascript asp.net gridview

评价AnodeRate TotalRate 100 100 200

txtrate,txtanoderate n txttatalrate被添加到gridview的ItemTemplate中。

速率字段在数据库表中,而阳极率n总数不在数据库表中 ,totalrate是readonly,以便用户只能看到它的添加,这个添加将传递给txtrate字段n将在数据库中绑定该值。 我也拿了  <asp:HiddenField ID="hdnTotalRt" runat="server" />因为它是只读的..

示例代码:

enter code here
<asp:TemplateField HeaderText="Standard Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" BorderWidth="0.5pt" Text='<%# Bind("rate") %>' BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtRate" runat="server" ValidationGroup="ValAtSave" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ControlToValidate="txtRate" runat="server" ValidationExpression="[0-9]*.?[0-9]*" ValidationGroup="ValAtSave">
 Nos Pls.
 </asp:RegularExpressionValidator>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="AnodeRate">
          <ItemTemplate>
          <asp:TextBox ID="txtAnodeRate" runat="server" BorderWidth="0.5pt"
          BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Total Rate" >
          <ItemTemplate>
          <asp:TextBox ID="txtTotalRate" runat="server" onchange="javascript:calculate_total_rate()" BorderWidth="0.5pt" Enabled="false"
          BorderColor="green" BackColor="#ffffcc" Width="40px"></asp:TextBox>
          </ItemTemplate>
    </asp:TemplateField>

function calculate_total_rate()
{

document.getElementById('<%=txtTotalRate%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%=txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value=  document.getElementById('<%=txtTotalRate.ClientID %>').value;

}

显示错误,因为当前上下文中不存在名称“txtRate”

1 个答案:

答案 0 :(得分:0)

  

你可以试试这个代码,(我有   替换'='in   的document.getElementById( '&LT;%= txtTotalRate%GT;')   和   的document.getElementById('&LT;%= txtAnodeRate.ClientID   %GT;”通过'#')

function calculate_total_rate()
{

document.getElementById('<%#txtTotalRate.ClientID%>').value = ( document.getElementById('<%=txtRate.ClientID %>').value ) + (document.getElementById('<%#txtAnodeRate.ClientID %>').value );
document.getElementById('<%=hdnTotalRt.ClientID %>').value=  document.getElementById('<%#txtTotalRate.ClientID %>').value;

}