How and when can I access the controls in the pager row?

N

Nathan Sokalski

I have a FormView control in which I use a PagerTemplate. I am having
trouble accessing the controls in the PagerTemplate. How do I access them,
and in what event should I put the could that accesses them? My current code
is as follows:


Partial Public Class indextest : Inherits System.Web.UI.Page
Private rowcount As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then Me.Refresh()
End Sub

Private Sub Refresh()
'Bind data
Dim photoalbum As New DataTable
Dim dataadapterSelect As New
System.Data.OleDb.OleDbDataAdapter("SELECT * FROM babyphotos ORDER BY
photodate,filename",
System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
dataadapterSelect.Fill(photoalbum)
Me.fviewPhotoAlbum.DataSource = photoalbum
Me.fviewPhotoAlbum.DataBind()
Me.rowcount = photoalbum.Rows.Count
End Sub

Private Sub fviewPhotoAlbum_ItemCreated(ByVal sender As Object, ByVal e
As System.EventArgs) Handles fviewPhotoAlbum.ItemCreated
'Hide unnecessary numeric LinkButtons
For i As Integer = 1 To 10
CType(Me.fviewPhotoAlbum.BottomPagerRow.Cells(0).FindControl("lnkPhotoAlbum"
& CStr(i)), LinkButton).Visible = (i <= Me.rowcount)
CType(Me.fviewPhotoAlbum.BottomPagerRow.Cells(0).FindControl("lnkPhotoAlbum"
& CStr(i)), LinkButton).Text = "&nbsp;" & CStr(i) & "&nbsp;"
Next
End Sub

Private Sub fviewPhotoAlbum_PageIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles fviewPhotoAlbum.PageIndexChanged
Dim currfile As String = CType(Me.fviewPhotoAlbum.DataItem,
DataRowView)("filename").ToString()
Dim photo As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath("images/photoalbum/" & currfile))
CType(Me.fviewPhotoAlbum.FindControl("imgPhoto"), Image).Width =
photo.Width
CType(Me.fviewPhotoAlbum.FindControl("imgPhoto"), Image).Height =
photo.Height
End Sub

Private Sub fviewPhotoAlbum_PageIndexChanging(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles
fviewPhotoAlbum.PageIndexChanging
Me.fviewPhotoAlbum.PageIndex = e.NewPageIndex
Me.Refresh()
End Sub
End Class


The code that I am not sure what event to put in, or whether is correct, is
the code in the fviewPhotoAlbum_ItemCreated method. I can successfully make
FormView controls with automatically generated Paging controls work, but I
cannot get the PagerTemplate to work. Thanks.
 
G

Gaurav Vaish \(MasterGaurav\)

I have a FormView control in which I use a PagerTemplate. I am having
trouble accessing the controls in the PagerTemplate. How do I access them,
and in what event should I put the could that accesses them? My current
code is as follows:

fromViewCtrl.FindControl('id_of_control') should rescue you.



--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-----------------------------------------
 
N

Nathan Sokalski

That is what I currently use, but what event(s) is this usable in? On the
following site:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.bottompagerrow.aspx

It says:

Note:
The BottomPagerRow property is available only after the FormView
control creates the bottom pager row in the ItemCreated event.



I feel like I have tried every event possible, but I either receive an error
or nothing. Keep in mind that I am trying to access the controls in the
PagerTemplate, not the ItemTemplate. Thanks.
 
G

Gaurav Vaish \(MasterGaurav\)

I feel like I have tried every event possible, but I either receive an
error or nothing. Keep in mind that I am trying to access the controls in
the PagerTemplate, not the ItemTemplate. Thanks.


Handle the event DataBound

fviewPhotoAlbum_DataBound(...)

Dim pager as FormViewRow = fviewPhotoAlbum.BottomPagerRow
Dim myControl as Control = pager.FindControl('lnkPhotoAlbum')

Dim btn as LinkButton = CType(myControl, LinkButton)

End Sub



HTH

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-----------------------------------------
 
N

Nathan Sokalski

I tried that, and just like the other things I tried, it just gives me an
empty browser window. Here is my exact code from my files:


indextest.aspx:

<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="indextest.aspx.vb" Inherits="family.indextest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:FormView ID="fviewPhotoAlbum" runat="server" AllowPaging="True"
HorizontalAlign="Center" Visible="false">
<PagerSettings NextPageImageUrl="images/NextBtn.gif"
PreviousPageImageUrl="images/PrevBtn.gif" Mode="NextPrevious"
Position="Bottom"/>
<RowStyle HorizontalAlign="Center"/>
<ItemTemplate>
<asp:Image ID="imgPhoto" runat="server" BorderWidth="0px"
ImageAlign="Middle" ImageUrl='<%#
DataBinder.Eval(Container,"DataItem.filename","images/photoalbum/{0}")
%>'/><br/>
<asp:Label ID="lblPhotoCaption" runat="server"
AssociatedControlID="imgPhoto" Text='<%#
DataBinder.Eval(Container,"DataItem.caption") %>'/><br/>
<asp:Label ID="lblPhotoDate" runat="server"
AssociatedControlID="imgPhoto" Text='<%#
DataBinder.Eval(Container,"DataItem.photodate","Photo taken on: {0:D}")
%>'/>
</ItemTemplate>
<PagerTemplate>
<asp:ImageButton ID="imgPrevious" runat="server"
AlternateText="Previous Photo" BorderWidth="0px" CausesValidation="False"
CommandArgument="Prev" CommandName="Page" Height="35px"
ImageUrl="images/PrevBtn.gif" Width="35px"/>&nbsp;
<asp:LinkButton ID="lnkPhotoAlbum1" runat="server"
CausesValidation="false" CommandName="Page" Text="A" />
<asp:LinkButton ID="lnkPhotoAlbum2" runat="server"
CausesValidation="false" CommandName="Page" Text="B" />
<asp:LinkButton ID="lnkPhotoAlbum3" runat="server"
CausesValidation="false" CommandName="Page" Text="C" />
<asp:LinkButton ID="lnkPhotoAlbum4" runat="server"
CausesValidation="false" CommandName="Page" Text="D" />
<asp:LinkButton ID="lnkPhotoAlbum5" runat="server"
CausesValidation="false" CommandName="Page" Text="E" />
<asp:LinkButton ID="lnkPhotoAlbum6" runat="server"
CausesValidation="false" CommandName="Page" Text="F" />
<asp:LinkButton ID="lnkPhotoAlbum7" runat="server"
CausesValidation="false" CommandName="Page" Text="G" />
<asp:LinkButton ID="lnkPhotoAlbum8" runat="server"
CausesValidation="false" CommandName="Page" Text="H" />
<asp:LinkButton ID="lnkPhotoAlbum9" runat="server"
CausesValidation="false" CommandName="Page" Text="I" />
<asp:LinkButton ID="lnkPhotoAlbum10" runat="server"
CausesValidation="false" CommandName="Page" Text="J" />&nbsp;
<asp:ImageButton ID="imgNext" runat="server" AlternateText="Next Photo"
BorderWidth="0px" CausesValidation="False" CommandArgument="Next"
CommandName="Page" Height="35px" ImageUrl="images/NextBtn.gif"
Width="35px"/>
</PagerTemplate>
</asp:FormView>
</form>
</body>
</html>


indextest.aspx.vb:

Partial Public Class indextest : Inherits System.Web.UI.Page
Private rowcount As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then Me.Refresh()
End Sub

Private Sub Refresh()
'Bind data
Dim photoalbum As New DataTable
Dim dataadapterSelect As New
System.Data.OleDb.OleDbDataAdapter("SELECT * FROM babyphotos ORDER BY
photodate,filename",
System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
dataadapterSelect.Fill(photoalbum)
Me.fviewPhotoAlbum.DataSource = photoalbum
Me.fviewPhotoAlbum.DataBind()
Me.rowcount = photoalbum.Rows.Count
'Response.Write(Me.rowcount & ControlChars.NewLine)
End Sub

Private Sub fviewPhotoAlbum_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles fviewPhotoAlbum.DataBound
'Hide unnecessary numeric LinkButtons
For i As Integer = 1 To 10
CType(Me.fviewPhotoAlbum.BottomPagerRow.FindControl("lnkPhotoAlbum"
& CStr(i)), LinkButton).Visible = (i <= Me.rowcount)
CType(Me.fviewPhotoAlbum.BottomPagerRow.FindControl("lnkPhotoAlbum"
& CStr(i)), LinkButton).Text = "&nbsp;" & CStr(i) & "&nbsp;"
Next
End Sub

Private Sub fviewPhotoAlbum_PageIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles fviewPhotoAlbum.PageIndexChanged
Dim currfile As String = CType(Me.fviewPhotoAlbum.DataItem,
DataRowView)("filename").ToString()
Dim photo As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath("images/photoalbum/" & currfile))
CType(Me.fviewPhotoAlbum.FindControl("imgPhoto"), Image).Width =
photo.Width
CType(Me.fviewPhotoAlbum.FindControl("imgPhoto"), Image).Height =
photo.Height
End Sub

Private Sub fviewPhotoAlbum_PageIndexChanging(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles
fviewPhotoAlbum.PageIndexChanging
Me.fviewPhotoAlbum.PageIndex = e.NewPageIndex
Me.Refresh()
End Sub
End Class


You will notice that the code in my DataBound eventhandler attempts to set
the Visible property of some of the controls in the PagerTemplate. To make
sure this was not the reason for the empty browser window (although the
ItemTemplate and the other PagerTemplate controls should show anyway), I
tried it with that line commented out and with the whole DataBound event
commented out, but I still receive an empty browser window. The
Response.Write() at the end of the Refresh() method was a check to make sure
my DataSource was not empty, and it wasn't (the Response.Write() output a
3). Why can't I get this thing to work? I don't have any problems when I
don't use the PagerTemplate. Thanks.
 
G

Gaurav Vaish \(www.edujini-labs.com\)

I tried that, and just like the other things I tried, it just gives me an
empty browser window. Here is my exact code from my files:

Let me munch it over the dinner or tomorrow breakfast... I think I need a
reboot right now.
Should work... because I just tried it out a while ago (bound to DataSet
from Customers in Northwind) :D


--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top