Hi All,
I am trying to set some client-side properties on a set of controls in the EditItemTemplate of an ASP.Net ListView.
For instance, if the EditItemTemplate contains something like this:
<tr id='phoneRow'>
<td>
<asp:Label ID="lblPhoneLabel" runat="server" Text="Phone Number: " />
</td>
<td>
<asp:Label ID="lblPhoneNumber" runat="server"
Text='<%# Bind("PhoneNumber") %>' />
</td>
</tr>
I want to only show the row if there is actually a phone number, so JavaScript is something like:
function showOrHidePhoneRow(rowId, labelId)
{
var row = document.getElementById(rowId);
var label = document.getElementById(labelId);
if (label.value == "")
row.style.visibility = "collapsed";
else
row.style.visibility = "visible";
}
I am trying to add a call to this function in the code-behind, but I'm not sure how. It seems onload is too late.
protected void lvwExample_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem item = e.Item as ListViewDataItem;
if (item.DisplayIndex == lvwExample.EditIndex)
{
Label label = item.FindControl("lblPhoneNumber") as Label;
if (label != null)
{
// Get here, but row always visible
label.Attributes.Add("onload",
"showOrHidePhoneRow('phoneRow', '" + label.ClientId + "');");
}
}
}
}
Any pointers would be greatly appreciated.
Thanks, wTs