开发者

ASP.NET Server tag is not well formed error in a listview

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:Image开发者_Python百科Button 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?


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.


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;
}


Try it this way

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


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 + "')";
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜