ASP.NET Listview_ItemCreated 上的 Javascript 函数

发布于 2024-09-19 10:53:14 字数 1466 浏览 3 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2024-09-26 10:53:14

这里的问题是 asp:label 标记在 html 中呈现为 span 标记...并且标记中不存在 onload 事件...
因此,您在正确的时间设置了该属性,但您正在尝试处理一个没有被触发的事件!
我认为解决方案是完全避免在您的场景中执行 JavaScript 并使用服务器事件来处理它。

Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
    Dim myLabel As Label = e.Item.FindControl("lblPhoneNumber")
    If Not myLabel Is Nothing Then
        e.Item.Visible = IIf(myLabel.Text = "", True, False)
    End If
End Sub

这样,您可以处理每个项目,并根据标签的值决定是否要隐藏行。

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

Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
    Dim myLabel As Label = e.Item.FindControl("lblPhoneNumber")
    If Not myLabel Is Nothing Then
        e.Item.Visible = IIf(myLabel.Text = "", True, False)
    End If
End Sub

This way you handle every single item and decide if you want to hide your row, based the value of the label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文