Find Label Control in Repeater Asp.net

Posted by user2769165 on Stack Overflow See other posts from Stack Overflow or by user2769165
Published on 2013-11-09T03:49:37Z Indexed on 2013/11/09 3:53 UTC
Read the original article Hit count: 178

Filed under:
|
|

I am using repeater and I want to find the label control in my repeater. here is my code

 <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>

Here is the code behind for the add button.

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());
         }            
    }

I have get the error of Object reference not set to an instance of an object. I think is because there are no value in the Label. The Find Control are not working. May I know how can fix this problem? Thank you very much

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET