Repeater control. Using a table that spans rows - asp.net

The following "FindControl" method fails to find the imgAd control. Any idea why? Could it be the Table that contains it? The intent of the table is to line things up in columns across rows.
<asp:Content ID="Content3" ContentPlaceHolderID="phPageContent" runat="Server">
<asp:Repeater ID="repBanner" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Image ID="imgAd" runat="server" AlternateText="Panda Visa" ImageUrl="Images/AffiliateBanners/125%20by%20125.jpg" />
</td>
<td>
<asp:TextBox ID="txtHtml" runat="server" Columns="80" ReadOnly="True" Rows="7" TextMode="MultiLine"></asp:TextBox>
</td>
<td>
<asp:Button runat="server" Text="Copy HTML to Clipboard" OnClientClick="ClipBoard('txtHtml')" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Protected Sub repBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repBanner.ItemDataBound
Dim CurrentAd As Ad = CType(e.Item.DataItem, Ad)
Dim RepeaterItem As RepeaterItem = e.Item
Dim imgAd As Image = CType(RepeaterItem.FindControl("imgAd"), Image)
imgAd.ImageUrl = "Images/" & "125 by 125.jpg" '<<<Error occurs here
End Sub
Object reference not set to an instance of an object.
Here's some debug info that I thought may help:
? RepeaterItem.Controls.Count
1
? RepeaterItem.Controls(0).Controls.Count
0
? typename(RepeaterItem.Controls(0))
"LiteralControl"

You need to check e.Item.ItemType to make sure that you're dealing with an item, not a header or footer. Something like this:
Protected Sub repBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repBanner.ItemDataBound
If (e.Item.ItemType <> ListItemType.Item AndAlso e.Item.ItemType <> ListItemType.AlternatingItem) Then
Return
End If
Dim CurrentAd As Ad = CType(e.Item.DataItem, Ad)
Dim RepeaterItem As RepeaterItem = e.Item
Dim imgAd As Image = CType(RepeaterItem.FindControl("imgAd"), Image)
imgAd.ImageUrl = "Images/" & "125 by 125.jpg" '<<<Error occurs here
End Sub

Related

total in repeater footer, itemdatabound in VB.NET

I want to sum the total of the gender at the repeater footer using vb.net by using item data bound, the behind code is wrong cause I don't know how to do it...
FRONT CODE
<asp:Repeater ID="repGender" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th>Gender</th>
<th>Total</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("GENDER")%>
</td>
<td style="text-align: center">
<%# Eval("TOTAL")%></a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="font-weight: bold">
<td>Grand Total</td>
<td style="text-align: center">
<asp:Label runat="server" ID="lblTotal"></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
BEHIND CODE
Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Total += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, ""))
Else e.Item.ItemType == ListItemType.Footer
End If
End Sub
Ok, so we need to define a simple total var at the form's "class" level. it only needs to persist during the data bind operation.
So, given your above markup, then we have this code to load the repeater.
Dim MyTotal As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData
End If
End Sub
Sub LoadData()
Dim strSQL As String =
"SELECT Gender, count(*) as TOTAL
FROM Employee
GROUP BY Gender"
Dim rstData As New DataTable
Using mycon As New SqlConnection(GetConstr)
Using cmdSQL As New SqlCommand(strSQL, mycon)
mycon.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
MyTotal = 0
repGender.DataSource = rstData
repGender.DataBind()
End Sub
And with your markup we see/get this:
And our data row bind event looks like this:
Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
' We don't have lable or text box with an "id" in repeat
' section, (can't use find control unless you assign "id" to these controls).
' so, lets grab the whole data row used for binding
Dim gData As DataRowView = e.Item.DataItem
MyTotal = MyTotal + gData.Item("TOTAL")
End If
If e.Item.ItemType = ListItemType.Footer Then
' set the total value in footer
Dim lblTotal As Label = e.Item.FindControl("lblTotal")
lblTotal.Text = MyTotal
End If
End Sub

how to make a label in a nested repeater visible programmatically

I have two repeaters, one nested in the other. I'm outputting a list of course titles in the parent repeater and the dates of those courses in the child repeater. This part is working fine. But, not all the course titles necessarily have dates available at any given time, so I want to be able to put a message up under each Course title that currently has no dates saying to check back regularly, blah, blah, blah. I've put the label in the footer template of the nested repeater with visibility=false and am trying to set the visibility=true at the appropriate time in the ItemDataBound Sub. Unfortunately, this is not working. I'm not getting any errors, the label just isn't showing up. I'm really hoping someone can help me out with my code or suggest an alternate way to do this. I'm kinda new to asp.net(VB) and am still struggling a bit. I've never tried nesting repeaters before. Here is my code:
.aspx
<asp:Repeater ID="rptTech" runat="server" >
<HeaderTemplate>
<table class="tableCourses">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<strong><asp:Label runat="server" ID="lblCourseName" Text='<%# Eval("CourseName") %>'></asp:Label></strong>
<br />
<asp:Label runat="server" ID="lblCourseSummary" Text='<%# Eval("Summary") %>'></asp:Label>
<br />
<asp:HyperLink ID="hpCourseMaterial" Visible='<%# CheckCourseMaterial(Eval("CourseMaterial")) %>' NavigateUrl='<%# "/files/Portal_Course_Material/" & Eval("CourseMaterial")%>' Text="Download Course Material" runat="server"></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptTechDates" DataSource='<%#Eval("relCourses") %>' runat="server" >
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<strong><asp:Label runat="server" ID="lblDate" Text='<%# CDate(Eval("dteDate")).ToString("dd/MM/yyyy") %>'></asp:Label></strong><br />
<asp:Label runat="server" ID="dteTime" Text='<%# "Time: " & CDate(Eval("dteTime2")).ToString("HH:mm") & " EST" %>'></asp:Label><br />
<asp:HyperLink ID="Register" NavigateUrl='<%# "/Test.aspx?intMeetingID2=" & Eval("pkWebinarID")%>' Text="Register" runat="server"></asp:HyperLink>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
<asp:Label ID="lblNoCourses" Text="There are no course dates available at this time. Please check back regularly to see any updates." runat="server" Visible="False"></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And Codebehind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim curDate As Date = CDate(Now)
'Create the connection
Dim connstring As String
connstring = ConfigurationManager.ConnectionStrings("LPISQLConn").ConnectionString
Using conn As New SqlConnection(connstring)
Dim cmd As New SqlCommand("SELECT foo1, foo2, foo3 FROM tblFoo", conn)
cmd.CommandType = CommandType.Text
Dim ad As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
ad.Fill(ds)
ds.Relations.Add(New DataRelation("relCourses", ds.Tables(0).Columns("fkcrsID"), ds.Tables(1).Columns("webinarID"), False))
rptTech.DataSource = ds.Tables(0)
rptTech.DataBind()
End Using
End Sub
Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
rptTechDates.DataSource = drv.CreateChildView("relCourses")
If rptTechDates.Items.Count < 1 And rptTechDates IsNot Nothing Then
If e.Item.ItemType = ListItemType.Footer Then
Dim lblNoCourses As Label = TryCast(e.Item.FindControl("lblNoCourses"), Label)
If lblNoCourses IsNot Nothing Then
lblNoCourses.Visible = True
End If
End If
Else
rptTechDates.DataBind()
End If
End If
End Sub
I would do the following:
Change your ItemDataBoundEvent for the parent repeater to:
Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
rptTechDates.DataSource = drv.CreateChildView("relCourses")
rptTechDates.DataBind()
End If
End Sub
Add this to the markup of your child repeater:
OnItemDataBound="rptTechDates_ItemDataBound"
Now add this:
Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataTable).Rows.Count = 0 Then
DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
End If
End If
So, if the number of rows in the child repeater's datasource is 0, then show the label.
EDIT After comment from OP
Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataView).Count = 0 Then
DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
End If
End If
You want to search rptTechDates repeater for the lblNoCourses control. The e.Item.FindControl("lblNoCourses") is searching the rptTech repeater which doesn't exist. See this question to search the control in the rptTechDates footer.
Also, you'll want to call Databind() on the repeater after setting it's DataSource
rptTechDates.DataSource = drv.CreateChildView("relCourses")
rptTechDates.Databind()

Find a repeater that is within another repeater

Ok so my issue is I have three repeaters. Within that repeater I have another repeater and a third one in the second. There is more in between but that's not relevant. Below the HTML is my VB code. My issue is that rptCrashPercentageAvg reutrns Nothing. How can rptCrashStatsDisplay access rptCrashPercentageAvg?
<asp:Repeater ID="rptCrashStatsDisplay" runat="server">
<ItemTemplate>
<asp:Repeater ID="rptCrashPercentage" runat="server">
<ItemTemplate>
<tr class="statsRowA">
<td class="emphasis" style="padding-left: 20px">
<%# DataBinder.Eval(Container.DataItem,"CRASH_TYPE_DESC") %>:
</td>
<td style="padding-left: 5px">
<%--background-color: <%# Iif(DataBinder.Eval(Container.DataItem,"CRASH_TYPE_PERCENT")>DataBinder.Eval(Container.DataItem,"COUNT(*)"), "red", "null") %>"--%>
<%#String.Format("{0:N1}", DataBinder.Eval(Container.DataItem, "CRASH_TYPE_PERCENT"))%>
%
</td>
<asp:Repeater ID="rptCrashPercentageAvg" runat="server">
<ItemTemplate>
<td style="padding-left: 5px">
<%#String.Format("{0:N1}", DataBinder.Eval(Container.DataItem, "AVG_VAL"))%>
%
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Private Sub rptCrashStatsDisplay_ItemDataBound(ByVal sender As System.Object, _
ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCrashStatsDisplay.ItemDataBound
Dim dv As DataRowView = CType(e.Item.DataItem, DataRowView)
If Not IsNothing(dv) Then
Dim rptCrashPercentage As Repeater = CType(e.Item.FindControl("rptCrashPercentage"), Repeater)
Dim view As DataView = dv.CreateChildView("statRel1")
If (view.Count > 0) Then
rptCrashPercentage.DataSource = view
rptCrashPercentage.DataBind()
End If
Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
Dim viewAvg As DataView = dv.CreateChildView("statRel2")
If (viewAvg.Count > 0) Then
rptCrashPercentageAvg.DataSource = viewAvg
rptCrashPercentageAvg.DataBind()
End If
End If
End Sub
I would try making sure you're looking for it in the correct place. It'll look in your repeater's Header for the control and since it won't find it there, it'll be Nothing the first time you try and use it.
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
'Shouldn't be "nothing" here.
End If
Otherwise you could try a more inefficient method:
Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
If rptCrashPercentageAvg IsNot Nothing Then
Dim viewAvg As DataView = dv.CreateChildView("statRel2")
If (viewAvg.Count > 0) Then
rptCrashPercentageAvg.DataSource = viewAvg
rptCrashPercentageAvg.DataBind()
End If
End If
Edit: Also, since it actually is a repeater, you shouldn't need the CType.

fetching values from the repeater control in ItemDataBound event

how to fetch values of repeater control in the ItemDataBound event
i wrote the following asp code
<asp:Repeater ID="repeaterItems" runat="server" OnItemDataBound="rptData_ItemDataBound" >
<itemtemplate>
<tr>
<td style="font-family:Tahoma; font-size:small"><asp:Label id="content" runat="server"> <%#DataBinder.Eval(Container.DataItem, "cont")%></asp:Label></td>
</tr>
<tr><td style="font-family:Verdana; font-size:small"><asp:Label id="url" runat="server"><a style="color:Green" href="<%#DataBinder.Eval(Container.DataItem, "url")%>" target="_blank"><%#DataBinder.Eval(Container.DataItem, "url")%></a></asp:Label></td></tr>
<tr><td><asp:LinkButton runat="server" ID="glossary" >Glossary</asp:LinkButton></td></tr>
<tr><td><asp:HiddenField runat="server" ID="id1"/><%#DataBinder.Eval(Container.DataItem, "id")%></td></tr>
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
</itemtemplate>
</asp:Repeater>
and the ItemDataBound event in vb.net
Protected Sub rptData_ItemDataBound(ByVal source As Object, ByVal e As RepeaterItemEventArgs)
Dim content As String = ""
Dim url As String = ""
Dim lbl1 As Label = TryCast(e.Item.FindControl("content"), Label)
Dim lbl2 As Label = TryCast(e.Item.FindControl("url"), Label)
content = lbl1.Text
url = lbl2.Text
Dim btn As LinkButton = CType(e.Item.FindControl("glossary"), LinkButton)
btn.PostBackUrl = "glossary.aspx?cont=" + content + "&url=" + url
end sub
but i get empty value in content and url...
please help
First of all in markup use Text property of label, do not put databinder.Eval in
<asp:Label ..> do not put content here </asp:Label>
In item databound event first find out control in which template.
follow this example :
Markup:
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("content") %>'></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
<AlternatingItemTemplate></AlternatingItemTemplate>
<SeparatorTemplate></SeparatorTemplate>
</asp:Repeater>
Source Code :
C#:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl = (Label)e.Item.FindControl("Label1");
LinkButton link = (LinkButton)e.Item.FindControl("LinkButton1");
link.PostBackUrl = "abcd/" + lbl.Text;
}
}
VB.Net:
Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Then
Dim lbl As Label = DirectCast(e.Item.FindControl("Label1"), Label)
Dim link As LinkButton = DirectCast(e.Item.FindControl("LinkButton1"), LinkButton)
link.PostBackUrl = "abcd/" + lbl.Text
End If
End Sub

request.querystring

how can i use request.querystring in asp.net.i have a linkbutton and this is in a repeater control.i want to play video for the corresponding link _click.i want to pass a fileID of the corresponding link to a function,how can i do this ?
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="LnkBtn"
OnClick="ButtonShowVideo_Click"><%#Eval("FileName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Protected Sub ButtonShowVideo_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ButtonShowVideo.Click
Repeater1.DataSource = GetSpecificVideo(**here i want to get the fileID**)
Repeater1.DataBind()
End Sub
Private Function GetSpecificVideo(ByVal i As Object) As DataTable
'pass the id of the video
Dim connectionString As String = ConfigurationManager
.ConnectionStrings("UploadConnectionString").ConnectionString
Dim adapter As New SqlDataAdapter("SELECT FileName, FileID,FilePath " +
"FROM FileM WHERE FileID = #FileID", connectionString)
adapter.SelectCommand.Parameters.Add("#FileID", SqlDbType.Int).Value =
DirectCast(i,Integer)
Dim table As New DataTable()
adapter.Fill(table)
Return table
End Function
This won't help you:
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="LnkBtn"
OnClick="ButtonShowVideo_Click"><%#Eval("FileName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
as it will not put FileName into query stirng. Rather use this method:
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="LnkBtn"
CommandArgument='<%#Eval("FileName")%>'
OnClick="ButtonShowVideo_Click"><%#Eval("FileName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
In code behind:
Protected Sub ButtonShowVideo_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ButtonShowVideo.Click
Dim btn as LinkButton = sender as LinkButton
if(btn is not null) then
if(NOT string.IsNullOrEmpty(btn.CommandArgument)) then
dim vid as integer = Convert.ToInt32(btn.CommandArgument)
Repeater1.DataSource = GetSpecificVideo(vid)
Repeater1.DataBind()
end if
end if
End Sub
If you plan to use query string, the button click can do either of the following:
Hyperlinks to a page with the designated quertstring to identify the action/entity etc
Do a server-side redirect to the page as describe in item 1 above
So where is your issue that you find challenging?

Resources