ASP.NET Listview_ItemCreated 上的 Javascript 函数
我试图在 ASP.Net ListView 的 EditItemTemplate 中的一组控件上设置一些客户端属性。
例如,如果 EditItemTemplate 包含类似这样的内容:
<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>
我只想在实际上有电话号码时显示该行,因此 JavaScript 类似于:
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";
}
我试图在代码隐藏中添加对此函数的调用,但我不知道怎么办。看来 onload 来得太晚了。
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 + "');");
}
}
}
}
任何指示将不胜感激。
谢谢, 工作时间
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是 asp:label 标记在 html 中呈现为 span 标记...并且标记中不存在 onload 事件...
因此,您在正确的时间设置了该属性,但您正在尝试处理一个没有被触发的事件!
我认为解决方案是完全避免在您的场景中执行 JavaScript 并使用服务器事件来处理它。
像
这样,您可以处理每个项目,并根据标签的值决定是否要隐藏行。
The problem here is that an asp:label tag is rendered as a span tag in the html... and the onload event doesn't exist in the tag...
So you are setting the attribute at the right time, but you are trying to handle an event that doesn't get fired!
I think the soution is to complete avoid javascript execution in your scenario and use the server event to handle it.
Something like
This way you handle every single item and decide if you want to hide your row, based the value of the label.