在Repeater Asp.net中查找标签控件 [英] Find Label Control in Repeater Asp.net

查看:66
本文介绍了在Repeater Asp.net中查找标签控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

这篇关于在Repeater Asp.net中查找标签控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆