循环表行后无法获得每个控件的唯一ID

时间:2012-11-03 17:54:37

标签: asp.net html vb.net visual-studio-2010

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

[/代码]

1 个答案:

答案 0 :(得分:1)

您应该考虑使用 Asp.net Repeater Control 。你做事的方式不是最优的,这是经典的Asp方法,你应该远离它。

在您的方法中,您必须使用客户端html控件(与经典asp相同)并使用Request Object访问它们。