在Repeater Asp.net中查找标签控件

时间:2013-11-09 03:49:38

标签: c# asp.net repeater

我正在使用转发器,我想在转发器中找到标签控件。这是我的代码

 <asp:Repeater ID="friendRepeater" runat="server">

    <table cellpadding="0" cellspacing="0">

    <ItemTemplate>
    <tr style=" width:700px; height:120px;">
       <td>
       <div style=" padding-left:180px;"> 
           <div id="leftHandPost" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;">
              <div id="childLeft" style=" padding-left:5px;">
                 <div id="photo"  style=" border: thin solid black; width:100px;height:100px;">
                   <asp:Image id="photoImage" runat="server" ImageUrl='<%# String.Concat("Images/", Eval("Picture")) %>' Width="100px" Height="100px" />
                 </div>
               </div><!--childLeft-->
            </div><!--leftHandPost-->
            </div>
        </td>

                        <td>
                            <div id="rightHandPost" style=" float:right; padding-right:260px;">
                                <div id="childRight" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;">
                                    <strong><asp:Label id="lblName" runat="server"><%# Eval("PersonName") %></asp:Label></strong><br />
                                    <div style=" float:right; padding-right:10px;"><asp:Button runat="server" Text="Add" onClick="add" /></div><br />
                                    <asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label><br />
                                    <asp:Label id="lblEmail" runat="server"><%# Eval("Email") %></asp:Label>
                                </div><!--childRight-->
                            </div><!--rightHandPost-->
                        </td>
                    </tr>

    </ItemTemplate>

    <AlternatingItemTemplate>
     <tr style=" width:700px; height:120px;">
       <td>
       <div style=" padding-left:180px;"> 
           <div id="Div1" style="float:left; width:120px; height:120px; border: medium solid #cdaf95; padding-top:5px;">
              <div id="Div2" style="padding-left:5px;">
                 <div id="Div3"  style=" border: thin solid black; width:100px;height:100px;">
                   <asp:Image id="photoImage" runat="server" ImageUrl='<%# String.Concat("Images/", Eval("Picture")) %>' Width="100px" Height="100px" />
                 </div>
               </div><!--childLeft-->
            </div><!--leftHandPost-->
        </div>
        </td>

                        <td>
                            <div id="Div4" style=" float:right; padding-right:260px;">
                                <div id="Div5" style="width:400px; height:120px; border: medium solid #cdaf95; padding-top:5px; padding-left:10px;">
                                    <strong><asp:Label id="lblName" runat="server"><%# Eval("PersonName")%></asp:Label></strong>
                                    <div style=" float:right; padding-right:10px;"><asp:Button id="btnAdd" runat="server" Text="Add" onClick="add"></asp:Button></div><br />
                                    <br />
                                    <asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label><br />
                                    <asp:Label id="lblEmail" runat="server"><%# Eval("Email") %></asp:Label>
                                </div><!--childRight-->
                            </div><!--rightHandPost-->
                        </td>
                    </tr>
    </AlternatingItemTemplate>

    <FooterTemplate>
    </table>
    </FooterTemplate>

</asp:Repeater>

以下是添加按钮的代码。

protected void add(object sender, EventArgs e)
    {           
        DateTime date = DateTime.Now;
        System.Web.UI.WebControls.Label la = (System.Web.UI.WebControls.Label)friendRepeater.FindControl("PersonID");
        String id = la.Text;

        try
        {
           MySqlConnection connStr = new MySqlConnection();
           connStr.ConnectionString = "Server = localhost; Database = healthlivin; Uid = root; Pwd = khei92;";
           String insertFriend = "INSERT INTO contactFriend(friendID, PersonID, PersonIDB, date) values (@id, @personIDA, @personIDB, @date)";
           MySqlCommand cmdInsertStaff = new MySqlCommand(insertFriend, connStr);
           cmdInsertStaff.Parameters.AddWithValue("@id", "F000004");
           cmdInsertStaff.Parameters.AddWithValue("@personIDA", "M000001");
           cmdInsertStaff.Parameters.AddWithValue("@personIDB", id);
           cmdInsertStaff.Parameters.AddWithValue("@date", date);
           connStr.Open();
           cmdInsertStaff.ExecuteNonQuery();

           MessageBox.Show("inserted");
           connStr.Close();

         }
         catch (Exception ex)
         {
            MessageBox.Show(ex.ToString());
         }            
    }

我得到的Object引用错误未设置为对象的实例。我认为是因为Label中没有任何价值。查找控件无法正常工作。我可以知道如何解决这个问题吗?非常感谢你

4 个答案:

答案 0 :(得分:6)

foreach (RepeaterItem item in friendRepeater.Items)
{
   Label lab = item.FindControl("lblName") as Label;
}

答案 1 :(得分:3)

我认为你可以使用它:

var personId= (Label)friendRepeater.Items[0].FindControl("PersonID");

答案 2 :(得分:1)

问题是你没有任何标签名称“personID”,因此无法找到该控件。

我认为你想从这一行获得价值

<asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label>

此Label控件名为“lblID”,因此您的查询代码应为

System.Web.UI.WebControls.Label la = (System.Web.UI.WebControls.Label)friendRepeater.FindControl("lblID");

使用 lblID 查找控件而不是 PersonID

答案 3 :(得分:-1)

这对我有用。

标签lblperson =(标签)(rpfrndRepeater.Items [0])。FindControl(&#34; lblPerson&#34;);