C# 在中继器 Asp.net 中查找标签控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19872148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Label Control in Repeater Asp.net
提问by user2769165
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
我收到了未将对象引用设置为对象实例的错误。我认为是因为标签中没有价值。查找控件不起作用。我可以知道如何解决这个问题吗?非常感谢
回答by user2769165
I think you can use this:
我认为你可以使用这个:
var personId= (Label)friendRepeater.Items[0].FindControl("PersonID");
回答by Alice
The problem is you don't have any Label names "personID", so it couldn't find that control.
问题是您没有任何标签名称“personID”,因此无法找到该控件。
I assume that you want to get value from this line
我假设您想从这一行中获得价值
<asp:Label id="lblID" runat="server"><%# Eval("PersonID") %></asp:Label>
And this Label control names "lblID", so your query code should be
而这个 Label 控件名为“lblID”,所以你的查询代码应该是
System.Web.UI.WebControls.Label la = (System.Web.UI.WebControls.Label)friendRepeater.FindControl("lblID");
Use lblIDto find control instead of PersonID
使用lblID来查找控件而不是PersonID
回答by pushu
foreach (RepeaterItem item in friendRepeater.Items)
{
Label lab = item.FindControl("lblName") as Label;
}
回答by Pavani Manthena
This worked for me.
这对我有用。
Label lblperson = (Label)(rpfrndRepeater.Items[0]).FindControl("lblPerson");
标签 lblperson = (Label)(rpfrndRepeater.Items[0]).FindControl("lblPerson");