Getting text from databound label

Pharlymal

New Member
I am binding the result set of a database query to the Repeater control, using the <template> to lay out a <table> as follows:<BR><BR>//================================================== ===<BR>// Renders a table looking like:<BR>//<BR>// <checkbox here> 1 Bugs<BR>// <checkbox here> 2 Peter <BR>// <checkbox here> 3 Roger<BR>// ...etc... <BR>//================================================== ===<BR><asp:repeater id="repeaterRabbit" runat="Server"><BR> <template name="HeaderTemplate"><table></template><BR> <template name="ItemTemplate"><BR> <tr><BR> <td><BR> <asp:checkbox id="Select" runat="Server"/><BR> <asp:label id="RabbitId" runat="Server"><%# ((DataRowView)Container.DataItem)["RabbitId"].ToString() %></asp:label><BR> <%# ((DataRowView)Container.DataItem)["RabbitName"].ToString() %><BR> </td><BR> </tr><BR> </template><BR> <template name="FooterTemplate"></table></template><BR></asp:repeater><BR><BR><BR>//================================================== ========<BR>// Now put a linkbutton so the user can shoot some rabbits<BR>//================================================== ========<BR><asp:linkbutton onClick="ShootRabbit_Click" runat="Server">Shoot Selected</asp:linkbutton><BR><BR> <BR><BR>//================================================== =========<BR>// Now in ShootRabbit_Click() we find out which Rabbits were <BR>// selected to be shot.<BR>//================================================== =========<BR>protected void ShootRabbit_Click(Object Src, EventArgs E ) <BR>{<BR> for( int i = 0; i < repeaterRabbit.Items.Count; i++ )<BR> {<BR> if( ((CheckBox)(repeaterRabbit.Items.FindControl("Select"))).Checked )<BR> {<BR> Response.Write("TestText");<BR> Response.Write(((Label)(repeaterWebSite.Items.FindControl("RabbitId"))).Text );<BR> }<BR> }<BR>}<BR><BR><BR>When I click the linkbutton and invoke ShootRabbit_Click() the only thing that is written to the page is "TestText", no RabbitId. I am not getting an error, so ASP.NET seems to be able to find the <asp:label> control, but there doesn't seem to be anything in the Text property. It works if I just hard code in a value into the label and don't use databinding. So how do I get at the text of the databound label?<BR><BR><BR>TIA<BR>Aaron<BR>It doesn't look like yo uare telling it to say anything.<BR>First of all, take the response.write out. You don;t need that.<BR>Then try<BR>repeaterWebSite.Items.FindControl("RabbitId").Text = "Say Something" <BR><BR>See what I mean?First, there was an typo in the code clip I posted. The "repeaterWebSite" in the last line was junk I accidently left in, instead it should read:<BR><BR>Response.Write(((Label)(repeaterRabbit.Items.FindControl("RabbitId"))).Text ); <BR><BR>Next, thanks for the reply Rob. The Response.Write was just for test purposes, intended to display the content of the asp:label's Text property - which was what I was trying to get at. And since it wasn't rendering anything I had I feeling I there wasn't anything in the Text property.<BR><BR>In fact, the reason that the Response.Write wasn't rendering anything was becuase the asp:label's Text property was empty. In the line:<BR><BR><asp:label id="RabbitId" runat="Server"><%# ((DataRowView)Container.DataItem)["RabbitId"].ToString() %></asp:label> <BR><BR>the databinding was not putting anything in the Text property as I originally thought it would. Instead, this line is creating a databound literal control as a child control of the asp:label. The following line actually does the trick:<BR><BR><asp:label id="RabbitId" Text='<%# ((DataRowView)Container.DataItem)["RabbitId"].ToString() %>' runat=Server/><BR><BR>Here the DataItem is acctually assigned to the asp:label's Text property - and the code clip works. (Note the single quotes as well).<BR><BR>Thanks to Nikhil Kothari at Microsoft for the help on this.<BR><BR>Aaron
 
Top