0

I have a ListView control with DataPager, i am trying to show results from database into ListView the database have field in which i have store content from ajaxhtmlextender i have bind ListView with database like this

protected void ListEvents()
    {
        conn = new SqlConnection(connSting);
        cmdListEvent = new SqlCommand("SELECT * FROM LatestEvents",conn);
        table = new DataTable();

        conn.Open();

        adpter = new SqlDataAdapter(cmdListEvent);
        adpter.Fill(table);
        ListEvent.DataSource = table;
        ListEvent.DataBind();

        conn.Close();

    }

and the .aspx file

<asp:ListView ID="ListEvent" runat="server" 
           OnItemDataBound="ListEvent_ItemDataBound" >

<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</LayoutTemplate>

<ItemTemplate>
<div class="contmainhead">
<h1 id="evhead"><asp:Label ID="LabelTittle" runat="server"><%#Eval("Tittle") %></asp:Label></h1>
</div>
<div class="contmain">
<asp:Label ID="LabelBody" runat="server"> <%#Eval("Body") %></asp:Label>
</div>
</ItemTemplate>

</asp:ListView>

It is giving the intended results but the problem is the label

<asp:Label ID="LabelBody" runat="server"> <%#Eval("Body") %></asp:Label>

showing all the formatted text and images as html markup, i know to work the label perfectly i have to use this function

Server.HtmlDecode();

i tried it like this

protected void ListEvent_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Label LabelBody = (Label)e.Item.FindControl("LabelBody");
            LabelBody.Text = Server.HtmlDecode(LabelBody.Text);
        }
    }

But the label shows nothing. . so how can i make the label show the content correctly? Your help will be greatly appreciated . .Thanx

user2517610
  • 275
  • 2
  • 8
  • 27

2 Answers2

0

try

<asp:Label ID="LabelBody" runat="server" text='<%#Eval("Body") %>' />

EDIT :

if the above didn't work try :

<asp:Label ID="LabelBody" runat="server" text="<% #Eval("Body").ToString() %>" />
sm_
  • 2,572
  • 2
  • 17
  • 34
  • still giving the same results – user2517610 Jul 09 '13 at 11:02
  • Have you tried this removing the ListEvent_ItemDataBound event ? you shouldn't need it using the Eval .. what you are doing here is doing Server.HtmlDecode on LabelBody.Text which is still an empty string, actually LabelBody.Text is going to be bound without the event, here you are overriding the binding and setting it to its original empty value. – sm_ Jul 09 '13 at 13:18
  • I've replaced single quotations by double ones – sm_ Jul 09 '13 at 13:43
  • I have tried after removing the ListEvent_ItemDataBound event but still the same results and i tried both suggestions but it's also not working – user2517610 Jul 09 '13 at 15:03
  • Okay i will post another answer using ListEvent_ItemDataBound event :) – sm_ Jul 09 '13 at 15:08
0
protected void ListEvent_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
           ListViewDataItem dataItem = (ListViewDataItem) e.Item;
           Label LabelBody = (Label)e.Item.FindControl("LabelBody");
           LabelBody.Text = (string) DataBinder.Eval(dataItem.DataItem, "Body");
        }
    }

Please make sure there is a column named in your returned datatable and also remove the <%# EVAL %> tag from the text attribute of your label, leave it empty or do not specify the attribute in your aspx

sm_
  • 2,572
  • 2
  • 17
  • 34
  • Thank You very much now it's working perfectly i have added this line LabelBody.Text = Server.HtmlDecode(LabelBody.Text); and it's giving the intended result. . .Once again thank you very much bro :) – user2517610 Jul 09 '13 at 15:24
  • Most welcome :) I still don't see how the Server.HtmlDecode(LabelBody.Text) helped you though :( – sm_ Jul 09 '13 at 15:27
  • 1
    Well i found it here http://stackoverflow.com/questions/10825791/how-do-i-stop-htmleditorextender-encoding-html-in-postback – user2517610 Jul 09 '13 at 15:36
  • Ohh i get it now so your html is being encoded on postback and thats why you need it :) – sm_ Jul 09 '13 at 15:47