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

3

我正在使用 Repeater 控件,并且想要在我的 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());
         }            
    }

我遇到了“对象引用未设置为对象实例”的错误。我认为是因为标签中没有值。Find Control 方法也无法正常工作。请问如何解决这个问题?非常感谢。


дҪ еә”иҜҘдҪҝз”Ё CommandName е’Ң CommandArgumentпјҢиҖҢдёҚжҳҜиҜ•еӣҫд»Һж ҮзӯҫдёӯжЈҖзҙўеҖјгҖӮ - Gabriel GM
好的...我明白你的意思和想法。我会尝试并看看。谢谢 :D - user2769165
4个回答

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

3
我认为你可以使用这个:

我认为您可以使用以下内容:

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

对象引用未设置到对象的实例。 - user2769165
我认为你的问题不是找控件。 - user1968030

1
问题是您没有任何标签名称为“personID”,因此它找不到该控件。
我假设您想从此行获取值。
<asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label>

而这个标签控件的名称是"lblID",因此您的查询代码应该是

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

使用lblID来查找控件,而不是PersonID

1
糟糕...我犯了一个错误...我已经更改了它,但是没有将值插入到数据库中,也没有出现错误。我能了解更多吗? - user2769165
该方法 cmdInsertStaff.ExecuteNonQuery(); 应返回受影响的行数,您应该检查返回值是否大于零,如果不是,则表示您连接到MySQL的命令存在问题。请还要设置断点和调试以检查要插入的所有值。 - Alice
嗯,非常抱歉我没有理解您的意思。能否再详细解释一下呢?非常感谢。 - user2769165
首先,在运行时您必须检查所有的值是否正确,对于这种情况,我指的是从PersonID字段获取的id值。然后,您应该修改此行**cmdInsertStaff.ExecuteNonQuery();int rowAffected = cmdInsertStaff.ExecuteNonQuery();**,并检查rowAffected值,如果它大于0,则表示您的插入代码已完成。但如果不是,则表示您的插入代码存在问题。 - Alice
我不知道你的整个项目架构,但可能有许多原因导致你无法将数据插入到数据库中。如果你确信你的连接字符串是正确的并且可以连接到数据库,那么问题就出在你的插入命令和数据库中的数据字段权限上。 - Alice

-1

这对我有用。

Label lblperson = (Label)(rpfrndRepeater.Items[0]).FindControl("lblPerson");


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接