How to change or add data to a data relay and get it to display in ASP.NET

Here is my code, this adds "OakTreeName" to datarepeater. There are about 200 of them.

Dim cmd As New SqlClient.SqlCommand("OakTree_Load", New SqlClient.SqlConnection(ConnStr))
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection.Open()
Dim datareader As SqlClient.SqlDataReader = cmd.ExecuteReader()

OakTree_Thumb_Repeater.DataSource = datareader
OakTree_Thumb_Repeater.DataBind()
cmd.Connection.Close()

      

Here's what I would like to do with my markup:

<ContentTemplate>
   <asp:Repeater ID="OakTree_Thumb_Repeater" runat="server">
      <ItemTemplate>
         <asp:ImageButton ImageUrl="<%# Container.DataItem("OakTreeName") %>" AlternateText="" runat="server" />
         <!-- Or I'd like to do it this way by adding a custom variable to the data repeater -->
         <asp:ImageButton ImageUrl="<%# Container.DataItem("OakTreeThumbURL") %>" AlternateText="" runat="server" />
      </ItemTemplate>
   </asp:Repeater>
</ContentTemplate>

      

I would like to process the "OakTreeName" variable before it is placed in the element template. Basically I need to manipulate the "OakTreeName" variable and then inject it as an ImageURL for the image button in the item template. How to do it?

Am I approaching this the wrong way? Is there a way to manipulate the item template from the code before it is rendered for each round of variables in the data repeater?

+2


a source to share


3 answers


Try to add OakTreeManipulationFunction to your codebehind file and call it from template:

<ContentTemplate>
<asp:Repeater ID="OakTree_Thumb_Repeater" runat="server">
  <ItemTemplate>
    <asp:ImageButton ImageUrl="<%# OakTreeManipulationFunction(Eval("OakTreeName").ToString()) %>" AlternateText="" runat="server" />
    <!-- Or I'd like to do it this way by adding a custom variable to the data repeater -->
    <asp:ImageButton ImageUrl="<%# Container.DataItem("OakTreeThumbURL") %>" AlternateText="" runat="server" />
  </ItemTemplate>
    </asp:Repeater>
</ContentTemplate>

      



Codebehind file:

Public Function OakTreeManipulationFunction(name as string)
    return name;
End Function

      

+5


a source


You should be able to modify the control in this iTem_DataBound event.



+3


a source


<%@ Import Namespace="System.Data" %>

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim mycdcatalog=New DataSet
   mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
   cdcatalog.DataSource=mycdcatalog
   cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">

<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Company</th>
<th>Price</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</html>
</body>

      

Or try this

<%@ Import Namespace="System.Data" %>

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim mycdcatalog=New DataSet
   mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
   cdcatalog.DataSource=mycdcatalog
   cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Company</th>
<th>Price</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</AlternatingItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</html>
</body>

      

0


a source







All Articles