【问题标题】:How do we prevent the labels in a repeater from being overwritten?我们如何防止中继器中的标签被覆盖?
【发布时间】:2013-04-10 11:06:49
【问题描述】:

我使用转发器控件来显示在 ItemDataBound 事件期间绑定了三个项目的数据代码的问题是最后一条记录显示在转发器的所有行中覆盖了已经存在的值,我该如何停止这?我使用这样的代码

.aspx.cs 代码

DataSet dsJobCardHistory =new DataSet();
double dcPriceIncl;
double dcPriceExcl; 
double dcTax;

protected void Job_History()
{ 
   dsJobCardHistory = objReportManager.Get_JobCard_History(strCustNo, strTranId);
}

protected void repJobCard_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

  for (int i = 0; i < dsJobCardHistory.Tables[0].Rows.Count;i++)

   {
     dcPriceIncl = Convert.ToDouble(dsJobCardHistory.Tables[0].Rows[i][6].ToString());
     dcTax = dcPriceIncl * 0.14;
     dcPriceExcl = dcPriceIncl - dcTax;

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==
                                ListItemType.AlternatingItem)
       {
            ((Label)e.Item.FindControl("lblRepPrice")).Text = 
             Convert.ToString(dcPriceExcl);
            ((Label)e.Item.FindControl("lblRepTax")).Text = 
             Convert.ToString(dcTax);
            ((Label)e.Item.FindControl("lblRepTotal")).Text = 
              Convert.ToString(dcPriceIncl);
       }                
    }

}

.aspx 代码

<asp:Repeater ID="repJobCard" runat="server" onitemdatabound="repJobCard_ItemDataBound">
     <td align="center" width="15%">                                   
          <font style="font-size:14px">
               <strong>
                   <asp:Label ID="lblRepPrice" runat="server" Text="">
                   </asp:Label>
               </strong>
          </font>
    </td>
    <td align="center" width="15%">                                   
        <font style="font-size:14px">
            <strong>
               <asp:Label ID="lblRepTax" runat="server" Text="">
               </asp:Label>
            </strong>
        </font>
    </td>
    <td align="center" width="15%">                                   
        <font style="font-size:14px">
              <strong>
                  <asp:Label ID="lblRepTotal" runat="server" Text="">
                  </asp:Label>
              </strong>
        </font>
   </td>

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    试试这样的

    protected void Job_History()
    { 
       dsJobCardHistory = objReportManager.Get_JobCard_History(strCustNo, strTranId);
       repJobCard.DataSource = dsJobCardHistory ;
       repJobCard.DataBind();
    }
    
    protected void repJobCard_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string price = DataBinder.Eval(e.Item.DataItem, "dcPriceExcl").ToString(); // dcPriceExcl -- or whatever the column names are for your data.
        string tax = DataBinder.Eval(e.Item.DataItem, "dcTax").ToString();
        string total = DataBinder.Eval(e.Item.DataItem, "dcPriceIncl").ToString();
    
        Label lblRepPrice = e.Item.FindControl("lblRepPrice") as Label;
        Label lblRepTax = e.Item.FindControl("lblRepTax") as Label;
        Label dcPriceIncl = e.Item.FindControl("dcPriceIncl") as Label;
    
        lblRepPrice.Text = price;
        lblRepTax.Text = tax;
        dcPriceIncl.Text = total;
    }
    

    还有

    <asp:Repeater ID="repJobCard" runat="server" onitemdatabound="repJobCard_ItemDataBound">
        <ItemTemplate>
            All of your HTML
        </ItemTemplate>
     </asp:Repeater>
    

    【讨论】:

    • 谢谢@Stanley,我试过了,但它抛出了 NullReferenceException。
    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2021-02-08
    • 2020-07-23
    • 2018-06-27
    相关资源
    最近更新 更多