在应用程序中读取标签的空值

时间:2015-10-28 04:54:45

标签: c# asp.net

在我的应用程序中从前端读取值到标签lbBillableAmount1但是以某种方式将null值传递给它并且代码块终止并转移到catch块。请参考下面的代码:

 protected void RPClientWish_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            if(Convert.ToInt32(Session["RoleId"])==1)
            {
                Label lb2 = (Label)e.Item.FindControl("lb2");
                lb2.Visible = true;
                Label lbBillableAmount1 = (Label)e.Item.FindControl("Label7");

                lbBillableAmount1.Visible = true;
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

    }

请让我知道这方面的解决方案,因为即使在删除标签并创建新标签后仍然会出现错误。     错误是:对象引用未设置为对象的实例。      我已经阅读了很多关于此错误的帖子,但没有解决问题。

这是我的转发器代码:

    <asp:Repeater ID="RPClientWish" runat="server" OnItemDataBound="RPClientWish_ItemDataBound" >
       <HeaderTemplate>
        <div class="col-md-12">
     <div class="EmployeeList">
             <div class="col-md-3">
     <asp:Label ID="lbname" runat="server" Font-Bold="true" Text="Name" style="font-family:'Segoe UI';"></asp:Label>
           </div>
    <div class="col-md-1"> &nbsp;</div>
     <div class="col-md-1">&nbsp;</div>
    <div class="col-md-2"><asp:Label ID="Label2" runat="server" Font-Bold="true" Text="Hours" style="font-family:'Segoe UI';"></asp:Label></div>
      <div class="col-md-1">&nbsp;</div>
                                                                <div class="col-md-2"><asp:Label ID="lb1" runat="server" Font-Bold="true" Text="Billable Hours" style="font-family:'Segoe UI';"></asp:Label></div>
        <div class="col-md-2">
      <asp:Label ID="lb2" runat="server" Font-Bold="true" Text="Billable Amount" style="font-family:'Segoe UI';" Visible="false"></asp:Label>
                   </div>
         </div>
      </div>
           </HeaderTemplate>
         <ItemTemplate>
         <div class="col-md-12">
             <div class="EmployeeList">
            <div class="col-md-3">
<asp:Label ID="lbClientname" runat="server" Font-Bold="true" Text='<%# Eval("ClientName")%>' style="font-family:'Segoe UI';"></asp:Label>
                                                                </div>
            <div class="col-md-1"> &nbsp;</div>
     <div class="col-md-1">&nbsp;</div>
   <div class="col-md-2"><asp:Label ID="lbhours" runat="server" Font-Bold="true" Text='<%# Eval("TotalHours")%>' style="font-family:'Segoe UI';"></asp:Label></div>
 <div class="col-md-1">&nbsp;</div>
<div class="col-md-2"><asp:Label ID="lbBillableHours1" runat="server" Font-Bold="true" Text="00" style="font-family:'Segoe UI';" ></asp:Label></div>
      <div class="col-md-2">

**<asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
      </div>**
    </div>
    <div class="col-md-12"><hr /></div>
   </div>

 </ItemTemplate>

  </asp:Repeater>

1 个答案:

答案 0 :(得分:0)

由于label7定义了ItemTemplate,因此您必须仅在ListItemType中搜索该标签为Item。当事件第一次被触发时,它有HeaderTemplate,因此它找不到任何Lable ID lable7导致错误。所以这里是访问lable7 <的代码/ p>

    protected void RPClientWish_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            try
            {
                if(Convert.ToInt32(Session["RoleId"])==1)
                {
                 if(e.Item.ItemType == ListItemType.Header)
                   {
                    Label lb2 = (Label)e.Item.FindControl("lb2");
                    lb2.Visible = true;
                   }
                 else if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                   {
                    Label lbBillableAmount1 = (Label)e.Item.FindControl("Label7");

                    lbBillableAmount1.Visible = true;
                  }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }