Steven Zack
Steven Zack

Reputation: 5104

how to bind data to a div in asp.net repeater?

Since I have a field, which contain html content, in dataset returned from database. I want to sue a div to display the html content by using

div.innerHtml=...

I can find div control in Repeater1_ItemDataBound event but don't know how to bind that field to the control. That field is called 'actualContent'. I think write

div.innerHtml=<%=actualContent%> won't work in backend

Then how to bind that field? Thank you in advance.

Upvotes: 1

Views: 9401

Answers (3)

ketan italiya
ketan italiya

Reputation: 296

hi there is two way for this,

1) set div InnerHtml property from .cs file like.

div1.InnerHtml = "This data will be bind.";

2) if you want fill it from datasource so simply use Eval tag in aspx page like

<div ID="div1" runat="server">
<%#Eval("YourColumName")%>
</div>

Upvotes: 0

James Johnson
James Johnson

Reputation: 46057

Add runat="server" to the div, and put your content in between the tagsm like this:

ASPX

Literal method:

<div ID="div1" runat="server">
    <%#Eval("ActualContent")%>
</div>

DIV method:

<div ID="div1">
    <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ActualContent")%>' />
</asp:Panel>

Code behind

Literal method:

string literalValue = ((Literal)e.Item.FindControl("Literal1")).Text;

DIV method:

string divValue = ((HtmlGenericControl)e.Item.FindControl("div1")).InnerHtml;

Upvotes: 3

Icarus
Icarus

Reputation: 63966

Couple of things:

  1. Make sure the div is marked with runat="server"
  2. on ItemData_Bound, find the div and simply do:

    div.innerHTML=Item.actualContent;

That should do it, I think.

Upvotes: 0

Related Questions