列表视图中的 ASP.NET 服务器标记格式不正确错误

发布于 2024-10-21 14:46:21 字数 1148 浏览 2 评论 0原文

下面的图像按钮就像一个魅力

             <asp:ListView ID="ListView1" runat="server">
                <layouttemplate>
                    <asp:PlaceHolder id="itemPlaceholder" runat="server" />
                </layouttemplate>
                <ItemTemplate> 
                  <asp:ImageButton onmouseover="javascript:show('Error1');"  ID="btnContainsError" runat="server"/> 
                </ItemTemplate>
            </asp:ListView>

但现在我想“让它工作”列表视图中的每条记录

所以对于记录1它应该像

    <asp:ImageButton onmouseover="javascript:show('Error1');" 
    ID="btnContainsError" runat="server"/>

第二条记录

<asp:ImageButton onmouseover="javascript:show('Error2');" 
 ID="btnContainsError" runat="server"/>

等等......

不幸的是我的一些尝试制作,如下所示,产生 Server tag is not wellforming 错误

<asp:ImageButton onmouseover="javascript:show('<%# "Error" & DataBinder.Eval(Container.DataItem, "Counter") %>');" ID="btnContainsError" runat="server"/>

那么你能指出我正确的方法吗?

The Following Image button works like a charm

             <asp:ListView ID="ListView1" runat="server">
                <layouttemplate>
                    <asp:PlaceHolder id="itemPlaceholder" runat="server" />
                </layouttemplate>
                <ItemTemplate> 
                  <asp:ImageButton onmouseover="javascript:show('Error1');"  ID="btnContainsError" runat="server"/> 
                </ItemTemplate>
            </asp:ListView>

But now I would like to "make it work" for every record within the list view

So for record 1 it should be like

    <asp:ImageButton onmouseover="javascript:show('Error1');" 
    ID="btnContainsError" runat="server"/>

for the second record

<asp:ImageButton onmouseover="javascript:show('Error2');" 
 ID="btnContainsError" runat="server"/>

and so on...

Unfortunately some of the tries i made, like the following, produce the Server tag is not well formed error

<asp:ImageButton onmouseover="javascript:show('<%# "Error" & DataBinder.Eval(Container.DataItem, "Counter") %>');" ID="btnContainsError" runat="server"/>

So could you please point me, the correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

昇り龍 2024-10-28 14:46:21

问题是你不能将该连接放在 <%# %> 内部。控件内的标签。在这种情况下,我通常做的是在表单对象上创建一个公共属性,然后从控件内调用该属性。例如,将其放在表单对象的代码后面:

Public Function GetError(ByVal sVal As String) As String
    Return "Error " & sVal
End Function

然后您可以将以下内容放入您的 aspx 页面中:

<asp:ImageButton onmouseover="javascript:show('<%# GetError(DataBinder.Eval(Container.DataItem, "Counter")) %>');" ID="btnContainsError" runat="server"/>

这应该可以工作。

The problem is that you can't put that concatenation inside of the <%# %> tags within the control. What I usually do in this situation is create a public property on the form object and then call that property from within the control. For Example put this on your form object on the code behind :

Public Function GetError(ByVal sVal As String) As String
    Return "Error " & sVal
End Function

And then you can put the following in your aspx page:

<asp:ImageButton onmouseover="javascript:show('<%# GetError(DataBinder.Eval(Container.DataItem, "Counter")) %>');" ID="btnContainsError" runat="server"/>

This should work.

深海少女心 2024-10-28 14:46:21

如果您在属性内使用服务器端代码标签 (<% %>),则需要将属性值用单引号而不是双引号括起来。

尝试将 onmouseover 属性更改为:

onmouseover='javascript:show("<%# "Error" + DataBinder.Eval(Container.DataItem, "Counter") %>");' 

或者,您可以在后面的代码中创建一个方法并调用它

onmouseover='javascript:show("<%# GetErrorText(Container.DataItem) %>");

并在后面的代码中:

protected string GetErrorText(object dataItem)
{
    return "Error" + (dataItem as MyObject).Counter;
}

if you are using server side code tags (<% %>) inside an attribute, you need to wrap the attribute value in single quotes instead of double quotes.

Try changing your onmouseover attribute to this:

onmouseover='javascript:show("<%# "Error" + DataBinder.Eval(Container.DataItem, "Counter") %>");' 

Alternatively, you can create a method in your code behind and call it

onmouseover='javascript:show("<%# GetErrorText(Container.DataItem) %>");

And in your code behind:

protected string GetErrorText(object dataItem)
{
    return "Error" + (dataItem as MyObject).Counter;
}
静若繁花 2024-10-28 14:46:21

试试这个方法

<asp:ImageButton onmouseover='show(this.errorMessage);' errorMessage='<%# "Error" & DataBinder.Eval(Container.DataItem, "Counter") %>' ID="btnContainsError" runat="server"/>

Try it this way

<asp:ImageButton onmouseover='show(this.errorMessage);' errorMessage='<%# "Error" & DataBinder.Eval(Container.DataItem, "Counter") %>' ID="btnContainsError" runat="server"/>
又怨 2024-10-28 14:46:21

我认为您不能在任何服务器端控件内拥有任何类型的 <% 服务器端标记 - 即带有 runat="server" 的任何内容。

这是未经测试的,而且我已经有一段时间没有做过任何经典的 ASP.NET 了...但是现在就开始吧!

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView_OnItemDataBound">
    <layouttemplate>
        <asp:PlaceHolder id="itemPlaceholder" runat="server" />
    </layouttemplate>
    <ItemTemplate> 
        <asp:ImageButton ID="btnContainsError" runat="server"/> 
    </ItemTemplate>
</asp:ListView>

请注意 OnItemDataBound 属性。

现在在您的代码中:

protected void ListView_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem) //You might want to change this
    {
        //this will be what ever you are binding... so the T in IEnumerable<T> that is the ListView.DataSource
        var someClassItem = e.Item.DataItem as SomeClass;

        if (string.IsNullOrWhiteSpace(someClassItem.Counter))
        {
            ImageButton btnContainsError = e.Item.FindControl("btnContainsError") as ImageButton;
            btnContainsError.Attributes["onmouseover"] = "javascript:show('Error" + someClassItem.Counter + "')";
        }
    }
}

I don't think you can have any type of <% server side tag inside any server side control - i.e. anything with runat="server".

This is untested and it's been a while since I've done any classic ASP.NET... but here goes!

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView_OnItemDataBound">
    <layouttemplate>
        <asp:PlaceHolder id="itemPlaceholder" runat="server" />
    </layouttemplate>
    <ItemTemplate> 
        <asp:ImageButton ID="btnContainsError" runat="server"/> 
    </ItemTemplate>
</asp:ListView>

Note the OnItemDataBound attribute.

Now in your code:

protected void ListView_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem) //You might want to change this
    {
        //this will be what ever you are binding... so the T in IEnumerable<T> that is the ListView.DataSource
        var someClassItem = e.Item.DataItem as SomeClass;

        if (string.IsNullOrWhiteSpace(someClassItem.Counter))
        {
            ImageButton btnContainsError = e.Item.FindControl("btnContainsError") as ImageButton;
            btnContainsError.Attributes["onmouseover"] = "javascript:show('Error" + someClassItem.Counter + "')";
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文