User Controls with in datagrids


Z

Zi

I have a user control within a data grid. I am binding the user
control to one of the values from the data grid. The data grid
implements paging. It is all working ok for the first page but once i
page to the second page of the datagrid, the user contorl stops
working correctly (displaying the correct data).

I have attached the code that is envolved.

Thanks a lot

Regards,

Zi

======== USER CONTROL CODE============
-------- ASCX code -----------
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="AlertButton.ascx.vb"
Inherits="wcBulletinBoard.AlertButton"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

<asp:ImageButton id="imgAlert" Runat="server" AlternateText="Click
here to add a watch to this title" ></asp:ImageButton>
<asp:TextBox Visible="true" ID="m_MessageID" Runat="server"/>
<asp:TextBox Visible="true" ID="m_intHasAlert" Runat="server"/>

-------- User contorl code behind -----------

Public Class AlertButton
Inherits System.Web.UI.UserControl

Protected WithEvents imgAlert As
System.Web.UI.WebControls.ImageButton
Protected WithEvents m_MessageID As
System.Web.UI.WebControls.TextBox
Protected WithEvents m_intHasAlert As
System.Web.UI.WebControls.TextBox
Public mMessageid As Integer
Public AlertType As Integer '1=header 2=Thread 3=Response
Public intHasAlert As Boolean 'Records weather there is an alert
on that message or not
Public intMessageID As Integer 'This should he the ID of the
message that you are watching.
Public Shared intCounter As Integer ' Counts the number of
controls that have been instanciated

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
intCounter = intCounter + 1

If Not IsPostBack Then
If intHasAlert And (intMessageID <> 0) Then
imgAlert.ImageUrl = "../../image/AlertON.gif"
Else
imgAlert.ImageUrl = "../../image/AlertOFF.gif"
End If
If intMessageID > 0 Then
m_MessageID.Text = intMessageID
m_intHasAlert.Text = intHasAlert
End If
End If
End Sub

====================== ASPX web form
====================================================
-------- ASPX code -----------

<%@ Page Language="vb" enableViewState="false" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="wcBulletinBoard.WebForm1"
trace="false"%>
<%@ Register TagPrefix="MyControls" TagName="Header"
Src="Control/Header.ascx" %>
<%@ Register TagPrefix="MyControls" TagName="Alert"
Src="Control/AlertButton.ascx" %>
<%@ Register TagPrefix="MyControls" TagName="Footer"
Src="Control/Footer.ascx" %>


<form id="Form1" method="post" runat="server">
<MYCONTROLS:HEADER RUNAT="server" ID="Header2" NAME="Header1" />
<asp:datagrid id="dgrdHeaders" runat="server" width="100%"
GridLines="None" AllowPaging="True" PAGERSTYLE-VISIBLE="False"
AutoGenerateColumns="False" EditItemIndex="123" BorderStyle="None">
<HEADERSTYLE FORECOLOR="White" CSSCLASS="title"
BACKCOLOR="#003366"></HEADERSTYLE>
<COLUMNS>
<asp:BoundColumn
Visible="False"
READONLY="True"
DataField="intAlert" ></asp:BoundColumn>
<asp:BoundColumn
DataField="dTimeStamp"
ReadOnly="True"
HeaderText="Date and Time">
<HEADERSTYLE WIDTH="90" Wrap="False" ></HEADERSTYLE>
<ITEMSTYLE
HorizontalAlign="Left"
VerticalAlign="Top"
CssClass="txt"
Wrap="False" ></ITEMSTYLE></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Alert">
<HEADERSTYLE Wrap="False"></HEADERSTYLE>
<ItemTemplate>
<asp:CheckBox id="chkintAlert"
AutoPostBack="True"
TextAlign="Right"
runat="server"
checked='<%#Container.DataItem("intAlert")%>'/>
<asp:textbox runat="server" id="txtTemp"
text='<%#Container.DataItem("intHeaderPK")%>'></asp:textbox>
<MyControls:Alert
runat="server"
AlertType="0"
intHasAlert='<%#Container.DataItem("intAlert")%>'
intMessageID='<%#Container.DataItem("intHeaderPK")%>'/>
</ItemTemplate>
<ItemStyle
CssClass="txt"
HorizontalAlign="Center"
VerticalAlign="Top"></ItemStyle></asp:TemplateColumn>
<asp:HyperLinkColumn
DataNavigateUrlField="intHeaderPK"
DataNavigateUrlFormatString="ThreadPage.aspx?HeaderFK={0}"
DataTextField="vchTitle" HeaderText="Title">
<ITEMSTYLE
HorizontalAlign="Left"
VerticalAlign="Top"
Wrap="true"
width="100%"></ITEMSTYLE></asp:HyperLinkColumn>
<asp:BoundColumn
DataField="vchAuthorName"
HeaderText="Author">
<HEADERSTYLE Wrap="False" WIDTH="55px"></HEADERSTYLE>
<ItemStyle HorizontalAlign="Left"
VerticalAlign="Top"
CssClass="txt"></ItemStyle></asp:BoundColumn>
<asp:BoundColumn DataField="intThreadCount"
HeaderText="Threads">
<HEADERSTYLE Wrap="False" ></HEADERSTYLE>
<ItemStyle
CssClass="txt"
HorizontalAlign="Center"
VerticalAlign="Top" ></ItemStyle></asp:BoundColumn>
</COLUMNS>
<PAGERSTYLE NEXTPAGETEXT="next" PREVPAGETEXT="prev"
HORIZONTALALIGN="Center"></PAGERSTYLE></asp:datagrid></TD>
<td vAlign="top" rowspan="2">
<table cellpadding="0" cellspacing="0" border="0" width="100%"
style="BORDER-BOTTOM: #000066 1px solid">
<tr><td align=right>
<asp:datagrid id="dgrdLatestLoggedOn" runat="server"
GridLines="None" Width="190" AutoGenerateColumns="False"
CssClass="txt" BorderStyle="None" PageSize="5">
<HEADERSTYLE FORECOLOR="White" CSSCLASS="title"
BACKCOLOR="#003366" Wrap="False"></HEADERSTYLE>
<COLUMNS>
<asp:BoundColumn DataField="vchAuthorName"
HeaderText="&nbsp;Recent Users">
<ItemStyle Wrap="False" ></ItemStyle></asp:BoundColumn>
<asp:BoundColumn DataField="dTimeStamp" >
<ItemStyle Wrap="False"
HorizontalAlign="right"></ItemStyle></asp:BoundColumn></COLUMNS>
<PAGERSTYLE NEXTPAGETEXT="next" PREVPAGETEXT="prev"
HORIZONTALALIGN="Center"></PAGERSTYLE></asp:datagrid></td></tr></table></TD></TR>
</TD></TR>

</TABLE>
</table>
</form>
-------------------------- code behind aspx ----------------------
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents dgrdHeaders As
System.Web.UI.WebControls.DataGrid
Protected WithEvents dgrdLatestLoggedOn As
System.Web.UI.WebControls.DataGrid
Protected WithEvents lblLoggedOn As
System.Web.UI.WebControls.Label
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents btnAdmin As System.Web.UI.WebControls.Button
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents lblCurrentPage As
System.Web.UI.WebControls.Label
Protected WithEvents lblOf As System.Web.UI.WebControls.Label
Protected WithEvents lblPageCount As
System.Web.UI.WebControls.Label
Protected WithEvents lnkNext As
System.Web.UI.WebControls.LinkButton
Protected WithEvents ibtnAlertNewHeader As
System.Web.UI.WebControls.ImageButton
Protected WithEvents lnkPrev As
System.Web.UI.WebControls.LinkButton
Protected WithEvents ws As localhost.Service1
Public Shared intWebform1PageCounter As Integer

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'====== TEST CODE ===========
intWebform1PageCounter = intWebform1PageCounter + 1
'====== END TEST CODE ===========
If Session("AuthorID") > 0 Then
ws = New localhost.Service1
ws.Credentials =
System.Net.CredentialCache.DefaultCredentials

dgrdHeaders.DataSource =
ws.SelectHeader(Session("dAuthorLastLogon"), Session("AuthorID"))
dgrdLatestLoggedOn.DataSource = ws.SelectLatestLoggedOn()

dgrdHeaders.DataBind()

dgrdLatestLoggedOn.DataBind()

Dim ds As DataSet
ds = ws.SelectAlertNewHeader(Session("AuthorID"))

If ds.Tables(0).Rows(0).Item(0) > 0 Then
ibtnAlertNewHeader.ImageUrl = "../image/AlertON.gif"
Else
ibtnAlertNewHeader.ImageUrl = "../image/AlertOFF.gif"
End If
Session("HeaderFK") = 0
If dgrdHeaders.PageCount = 1 Then
lblPageCount.Text = "1"
lnkNext.Visible = False
lnkPrev.Visible = False
Else
lblPageCount.Text = dgrdHeaders.PageCount
End If
Else
Response.Redirect("logon.aspx")
End If
End Sub
Public Sub NavigationLink_click(ByVal sender As System.Object,
ByVal e As CommandEventArgs)
If sender.commandname = "Prev" Then
If dgrdHeaders.CurrentPageIndex <> 0 Then
dgrdHeaders.CurrentPageIndex =
dgrdHeaders.CurrentPageIndex - 1
lblCurrentPage.Text = dgrdHeaders.CurrentPageIndex + 1
lnkNext.Visible = True
If lblCurrentPage.Text = 1 Then
lnkPrev.Visible = False
End If
End If
ElseIf sender.commandname = "Next" Then
If dgrdHeaders.CurrentPageIndex <> dgrdHeaders.PageCount -
1 Then
dgrdHeaders.CurrentPageIndex =
dgrdHeaders.CurrentPageIndex + 1
lblCurrentPage.Text = dgrdHeaders.CurrentPageIndex + 1
If lblCurrentPage.Text = dgrdHeaders.PageCount Then
lnkNext.Visible = False
lnkPrev.Visible = True
Else
lnkNext.Visible = True
lnkPrev.Visible = True
End If
End If
End If
dgrdHeaders.DataBind()
End Sub
Private Sub dgrdLatestLoggedOn_PageIndexChanged(ByVal source As
Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
Handles dgrdLatestLoggedOn.PageIndexChanged
dgrdLatestLoggedOn.CurrentPageIndex = e.NewPageIndex
dgrdLatestLoggedOn.DataBind()
End Sub
Private Sub btnAdmin_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAdmin.Click
Response.Redirect("AuthorAdmin.aspx")
End Sub
Private Sub ibtnAlertNewHeader_Click(ByVal sender As Object, ByVal
e As System.Web.UI.ImageClickEventArgs) Handles
ibtnAlertNewHeader.Click
ws = New localhost.Service1
ws.Credentials = System.Net.CredentialCache.DefaultCredentials
If ibtnAlertNewHeader.ImageUrl = "../image/AlertON.gif" Then
ws.AddDelAlert(localhost.enumAlert.NewHeader, False,
Session("AuthorID"), 0)
ibtnAlertNewHeader.ImageUrl = "../image/AlertOFF.gif"
Else
ws.AddDelAlert(localhost.enumAlert.NewHeader, True,
Session("AuthorID"), 0)
ibtnAlertNewHeader.ImageUrl = "../image/AlertOFF.gif"
End If
Response.Redirect("webform1.aspx")
End Sub
 
Ad

Advertisements


Top