How do I change or add data to a data repeater and get it to display in ASP.NET
Here is my code-behind, this adds the "OakTreeName" to the datarepeater. There's 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 is essentially what I'd 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开发者_如何学JAVA would like to manipulate the "OakTreeName" variable before it gets placed into the item template. Basically I need to manipulate the "OakTreeName" variable and then input it as the ImageURL for the imagebutton within the item template. How do I do this?
Am I approaching this wrong? Is there a way to manipulate the item template from code-behind before it gets displayed for each round of variables in the data repeater?
Try to add OakTreeManipulationFunction in yours 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
You should be able to modify the control in it's iTem_DataBound event.
<%@ 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>
精彩评论