1

我正在循环整个表格以及控件。对于每一行,用户可以输入一个数字来执行计算并显示在同一行的标签中,但它会继续影响所有其余的行,因为它们处于循环中,因此每个控件没有唯一的 id。

[代码]

<%@ Page Title="" Language="VB" MasterPageFile="~/public.master" AutoEventWireup="false" CodeFile="ccalc.aspx.vb" Inherits="ccalc" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.OleDb" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

    <div>
  <h1>Estimated Monthly Electricity Consumption Calculator</h1>
</div>
<div>
    <%




    Dim id As Integer
    Dim catname As String

    Dim db As New databaseconnection
    Dim cmd As New OleDbCommand
    cmd.Connection = db.connection
    cmd.CommandText = "select * from ecg_projectDB2.dbo.DeviceCategory"
    cmd.CommandType = CommandType.Text
    Dim dr As OleDbDataReader
    dr = cmd.ExecuteReader
    Dim found As Boolean = False
    While dr.Read
        found = True
        id = dr("CategoryID")
            catname = dr("CategoryName")




        %>    

<table width="100%" cellpadding="5" cellspacing="1" bgcolor="#FFFFFF" class="ten">
                      <tbody>
                        <tr>
                          <td colspan="5"><h2><% Response.Write(catname)%></h2></td>
                        </tr>
                        <tr align="center">
                          <th class="style1" bgcolor="#FFCC66">Electrically Powered Items</th>
                          <th class="ten" bgcolor="#FFCC66"><div align="center">Quantity</div></th>
                          <th class="ten" bgcolor="#FFCC66"><div align="center">Average
                            monthly KWh</div></th>
                          <th class="ten" bgcolor="#FFCC66"><div align="center">KWh/month</div></th>
                          <th class="ten" bgcolor="#FFCC66"><div align="center">GHc /month</div></th>
                        </tr>


                       <%
                           Dim appid As Integer
                           Dim appname As String
                           Dim wpm As Single

                           Dim brb As New OleDbCommand
                           brb.Connection = db.connection
                           brb.CommandText = "select * from ecg_projectDB2.dbo.Appliances where CategoryID = '" & id & "'"
                           brb.CommandType = CommandType.Text
                           Dim br As OleDbDataReader
                           br = brb.ExecuteReader
                           Dim ins As Boolean = False
                           Dim counter As Integer = 0
                           While br.Read
                               ins = True
                               appid = br("ApplianceID")
                               'quantity.ID = appid

                               kwh.ID = appid
                               ghc.ID = appid


                               appname = br("ApplianceName")
                               wpm = br("Wattpermin")

                               counter = counter + 1

                               Dim qid = quantity.id
                               Dim kwhid = kwh.ID


                               Dim totusage As Single
                               'Label1.Text = quantity.ID

                               If IsPostBack Then

                                   Dim aaa = quantity.ID

                                   If counter  Then
                                       'Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("MainContent")

                                       kwh.Text = quantity.UniqueID


                                       'kwh.Text =  Results.text

                                   End If


                                   End If


                           %>

                        <tr>
                          <td class="style1"><strong><% Response.Write(appname)%></strong></td>
                          <td class="highlight"><div align="center">

                              <asp:TextBox ID="quantity" runat="server" AutoPostBack="True" CssClass="input" 
                                  Width="79px" ></asp:TextBox>
&nbsp;&nbsp;</div></td>
                          <td><div align="center">
                            <input name="refrigeratorMonthKWh" value="182" type="hidden" />
                              <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                          </div></td>
                          <td><div align="center">
                              &nbsp;<asp:TextBox ID="kwh" runat="server" CssClass="input4" Width="59px"></asp:TextBox>
                          </div></td>
                          <td><div align="center">
                              &nbsp;<asp:TextBox ID="ghc" runat="server" CssClass="input4" Width="59px"></asp:TextBox>
                          </div></td>
                        </tr>

                        <%

                        End While
                        brb.Dispose()
                        br.Close()


                            %>


                      </tbody>
                    </table>


                    <%




                End While
                cmd.Dispose()
                dr.Close()

                       %>


</div>
    <% 

     %>
<div>

<table width="100%" border="0" cellpadding="5" bgcolor="#FFF7E5" class="ten">
                      <tbody>
                        <tr>
                          <td><font color="#9f7f40">Estimated</font> monthly <u><font color="#FF0000">household </font></u>*
                            usage:
                              <asp:TextBox ID="totalusage" runat="server" CssClass="input4" Width="59px"></asp:TextBox>
&nbsp;kWh; <br /></td>
                        </tr>
                        <tr>
                          <td class="highlight"><font color="#9f7f40">Estimated</font> monthly <u><font color="#FF0000">household</font></u>*
                            bill: &cent;
                                                    <asp:TextBox ID="totalbill" runat="server" 
                                  CssClass="input4" Width="59px"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                          <td class="highlight"><h3><strong><em>*Heating usage
                            not included in household totals</em></strong></h3></td>
                        </tr>
                      </tbody>
                    </table>

</div>


</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

[/代码]

4

1 回答 1

1

you should really consider using Asp.net Repeater Control. The way you doing things is NOT optimal, this is Classic Asp Approach, and you should stay away from it.

in your approach you have to use client side html controls (same as classic asp) and access them using Request Object.

于 2012-11-03T20:10:23.050 回答