473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Order of events, databinding, and UserControls

I have a very simple UserControl which contains an Image and a Label, which
I use to display an image with a caption. I am using this control inside a
DataList, setting the two Public variables using attributes in the *.aspx
page. Everything displays great for the initial load, but whenever I try to
add or delete an item (I have controls to do this on the page), it does not
seem to be recieving any values for the public variables. Here is the
important code I use for the UserControl and the DataList I use it in (if I
left out anything that will help you determine the problem, let me know):
The UserControl:

Public caption As String = ""
Public photo As String = ""

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Width = New Unit(200)
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
The DataList:

<asp:datalist id="datImages" runat="server" EnableViewState="False"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal" DataKeyField="contentid">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id="CapImg" runat="server" EnableViewState="False"
photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR><BR>
<asp:Button id="btnDeleteImg" runat="server"
CausesValidation="False" Font-Bold="True" EnableViewState="False"
Text="Delete" CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
The DeleteCommand Handler:

Private Sub datImages_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs ) Handles
datImages.DeleteCommand
Dim cmdDelete As New OleDbCommand("DELETE FROM newsinfo WHERE
contentid=" & CInt(datImages.DataKeys(e.Item.ItemIndex)), New
OleDbConnection(Global.connectionstring))

If Not e.Item.DataItem Is Nothing AndAlso
System.IO.File.Exists(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content")))) Then
System.IO.File.Delete(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content"))))
cmdDelete.Connection.Open()
cmdDelete.ExecuteNonQuery()
cmdDelete.Connection.Close()
Me.RefreshResults()
End Sub
The Page_Load Method:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.RefreshResults()
End Sub
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT
contentid,textcaption,content FROM newsinfo WHERE contenttype='image'",
Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
Why does everything work fine on the initial load of the page, but not after
a postback? If you need to see any other code, let me know. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
May 21 '06 #1
9 3459
Nathan,
you need to check for postback on pageload, the datalist can recreate
itself, you would need to rebind only if you change the datasource (
like in your DeleteCommand method)

hth
Nathan Sokalski wrote:
I have a very simple UserControl which contains an Image and a Label, which
I use to display an image with a caption. I am using this control inside a
DataList, setting the two Public variables using attributes in the *.aspx
page. Everything displays great for the initial load, but whenever I try to
add or delete an item (I have controls to do this on the page), it does not
seem to be recieving any values for the public variables. Here is the
important code I use for the UserControl and the DataList I use it in (if I
left out anything that will help you determine the problem, let me know):
The UserControl:

Public caption As String = ""
Public photo As String = ""

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Width = New Unit(200)
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
The DataList:

<asp:datalist id="datImages" runat="server" EnableViewState="False"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal" DataKeyField="contentid">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id="CapImg" runat="server" EnableViewState="False"
photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR><BR>
<asp:Button id="btnDeleteImg" runat="server"
CausesValidation="False" Font-Bold="True" EnableViewState="False"
Text="Delete" CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
The DeleteCommand Handler:

Private Sub datImages_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs ) Handles
datImages.DeleteCommand
Dim cmdDelete As New OleDbCommand("DELETE FROM newsinfo WHERE
contentid=" & CInt(datImages.DataKeys(e.Item.ItemIndex)), New
OleDbConnection(Global.connectionstring))

If Not e.Item.DataItem Is Nothing AndAlso
System.IO.File.Exists(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content")))) Then
System.IO.File.Delete(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content"))))
cmdDelete.Connection.Open()
cmdDelete.ExecuteNonQuery()
cmdDelete.Connection.Close()
Me.RefreshResults()
End Sub
The Page_Load Method:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.RefreshResults()
End Sub
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT
contentid,textcaption,content FROM newsinfo WHERE contenttype='image'",
Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
Why does everything work fine on the initial load of the page, but not after
a postback? If you need to see any other code, let me know. Thanks.

May 22 '06 #2
When I do an If Not IsPostBack() in the Page_Load, then one of my other
events does not work because it uses the DataKeys property, which is not
completely created until after databinding. Any other ideas?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Nathan,
you need to check for postback on pageload, the datalist can recreate
itself, you would need to rebind only if you change the datasource ( like
in your DeleteCommand method)

hth
Nathan Sokalski wrote:
I have a very simple UserControl which contains an Image and a Label,
which I use to display an image with a caption. I am using this control
inside a DataList, setting the two Public variables using attributes in
the *.aspx page. Everything displays great for the initial load, but
whenever I try to add or delete an item (I have controls to do this on
the page), it does not seem to be recieving any values for the public
variables. Here is the important code I use for the UserControl and the
DataList I use it in (if I left out anything that will help you determine
the problem, let me know):
The UserControl:

Public caption As String = ""
Public photo As String = ""

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Width = New Unit(200)
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
The DataList:

<asp:datalist id="datImages" runat="server" EnableViewState="False"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal" DataKeyField="contentid">
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id="CapImg" runat="server"
EnableViewState="False" photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR><BR>
<asp:Button id="btnDeleteImg" runat="server"
CausesValidation="False" Font-Bold="True" EnableViewState="False"
Text="Delete" CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
The DeleteCommand Handler:

Private Sub datImages_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs ) Handles
datImages.DeleteCommand
Dim cmdDelete As New OleDbCommand("DELETE FROM newsinfo WHERE
contentid=" & CInt(datImages.DataKeys(e.Item.ItemIndex)), New
OleDbConnection(Global.connectionstring))

If Not e.Item.DataItem Is Nothing AndAlso
System.IO.File.Exists(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content")))) Then
System.IO.File.Delete(Server.MapPath("newsinfo/" &
CStr(CType(e.Item.DataItem, DataRowView).Row("content"))))
cmdDelete.Connection.Open()
cmdDelete.ExecuteNonQuery()
cmdDelete.Connection.Close()
Me.RefreshResults()
End Sub
The Page_Load Method:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.RefreshResults()
End Sub
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT
contentid,textcaption,content FROM newsinfo WHERE contenttype='image'",
Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
Why does everything work fine on the initial load of the page, but not
after a postback? If you need to see any other code, let me know. Thanks.

May 22 '06 #3
the problem would be that grid would bind again and event would be lost,
because the grid has lost the view state information,

on the itemcommand event you should be able to datakey value without
having to rebind it,
you can post more code if you want

Nathan Sokalski wrote:
When I do an If Not IsPostBack() in the Page_Load, then one of my other
events does not work because it uses the DataKeys property, which is not
completely created until after databinding. Any other ideas?

May 22 '06 #4
OK, but I am still having a problem that happens only on postbacks. The problem (or at least the error line it shows in the error, I think the error is somewhere else) is in the extra little class I wrote to display images with captions. Here is a screenshot of the error I am shown:

Server Error in '/VAPresenters' Application.
--------------------------------------------------------------------------------

c:\inetpub\wwwroot\VAPresenters
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: c:\inetpub\wwwroot\VAPresenters

Source Error:

Line 27:
Line 28: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Line 29: Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath(photo))
Line 30: imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
Line 31: i.Dispose() 'Without this line, you may recieve an error saying another process is using the file when you try to delete it

Source File: c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb Line: 29

Stack Trace:

[FileNotFoundException: c:\inetpub\wwwroot\VAPresenters]
System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +204
System.Drawing.Image.FromFile(String filename) +7
VAPresenters.captionimage.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb:29
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Control.AddedControl(Control control, Int32 index) +307
System.Web.UI.ControlCollection.Add(Control child) +153
System.Web.UI.WebControls.DataList.CreateItem(Int3 2 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +111
System.Web.UI.WebControls.DataList.CreateControlHi erarchy(Boolean useDataSource) +686
System.Web.UI.WebControls.BaseDataList.CreateChild Controls() +61
System.Web.UI.Control.EnsureChildControls() +100
System.Web.UI.WebControls.BaseDataList.get_Control s() +12
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e) +20
System.Web.UI.WebControls.BaseDataList.DataBind() +23
VAPresenters.newsinfoadmin.RefreshResults() in c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:64
VAPresenters.newsinfoadmin.datLinksFiles_PreRender (Object sender, EventArgs e) in c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:192
System.Web.UI.Control.OnPreRender(EventArgs e) +67
System.Web.UI.Control.PreRenderRecursiveInternal() +62
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Page.ProcessRequestMain() +1476

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Here is the class code and the couple other things that might be relevant:
The Class's .aspx File:

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="captionimage.ascx.vb" Inherits="VAPresenters.captionimage" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div align="center">
<asp:Image id="imgPhoto" runat="server" EnableViewState="False"></asp:Image><BR>
<asp:Label id="lblCaption" runat="server" EnableViewState="False" Font-Size="X-Small" Width="200px"></asp:Label><br>
</div>
The Class's .aspx.vb File (the "Web Form Designer Generated Code" section is not included here, I did not modify that part):

Public Class captionimage
Inherits System.Web.UI.UserControl

Public caption As String = ""
Public photo As String = ""

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
i.Dispose() 'Without this line, you may recieve an error saying another process is using the file when you try to delete it
imgPhoto.Width = New Unit(200)
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
End Class
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT contentid,textcaption,content FROM newsinfo WHERE contenttype='image'", Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
The 2 Places I call RefreshResults() From:

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

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
If IsPostBack() Then Me.RefreshResults()
End Sub
The error is recieved only on postbacks (no matter what I do to cause the postback, as far as I can tell). If I remove the line in Page_PreRender Then I do not recieve the error, but doing that obviously means that my page does not get updated. Another thing that is confusing me, whether it is related I cannot tell, but in the Template Editor I see the following:


Did I forget something in the UserControl's code? Why is it having a problem creating the control? I don't think it matters for now, but since the line shown in the error is in the control's code, I though it would be good to mention this. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com> wrote in message news:uU**************@TK2MSFTNGP04.phx.gbl...
the problem would be that grid would bind again and event would be lost,
because the grid has lost the view state information,

on the itemcommand event you should be able to datakey value without
having to rebind it,
you can post more code if you want





Nathan Sokalski wrote:
When I do an If Not IsPostBack() in the Page_Load, then one of my other
events does not work because it uses the DataKeys property, which is not
completely created until after databinding. Any other ideas?

May 22 '06 #5
Nathan,
the photo and caption variables in your control are not being set (
atleast in the code that i see), do you want to perform the image
operation on some action ?

Nathan Sokalski wrote:
OK, but I am still having a problem that happens only on postbacks. The
problem (or at least the error line it shows in the error, I think the
error is somewhere else) is in the extra little class I wrote to display
images with captions. Here is a screenshot of the error I am shown:

Server Error in '/VAPresenters' Application.
------------------------------------------------------------------------
c:\inetpub\wwwroot\VAPresenters

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException:
c:\inetpub\wwwroot\VAPresenters

Source Error:

Line 27:
Line 28: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Line 29: Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath(photo))
Line 30: imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
Line 31: i.Dispose() 'Without this line, you may recieve an error saying another process is using the file when you try to delete it
Source File: c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb
Line: 29

Stack Trace:

[FileNotFoundException: c:\inetpub\wwwroot\VAPresenters]
System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) +204
System.Drawing.Image.FromFile(String filename) +7
VAPresenters.captionimage.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb:29
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Control.AddedControl(Control control, Int32 index) +307
System.Web.UI.ControlCollection.Add(Control child) +153
System.Web.UI.WebControls.DataList.CreateItem(Int3 2 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +111
System.Web.UI.WebControls.DataList.CreateControlHi erarchy(Boolean useDataSource) +686
System.Web.UI.WebControls.BaseDataList.CreateChild Controls() +61
System.Web.UI.Control.EnsureChildControls() +100
System.Web.UI.WebControls.BaseDataList.get_Control s() +12
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e) +20
System.Web.UI.WebControls.BaseDataList.DataBind() +23
VAPresenters.newsinfoadmin.RefreshResults() in c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:64
VAPresenters.newsinfoadmin.datLinksFiles_PreRender (Object sender, EventArgs e) in c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:192
System.Web.UI.Control.OnPreRender(EventArgs e) +67
System.Web.UI.Control.PreRenderRecursiveInternal() +62
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Page.ProcessRequestMain() +1476
------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032
Here is the class code and the couple other things that might be relevant:
The Class's .aspx File:

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="captionimage.ascx.vb" Inherits="VAPresenters.captionimage"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div align="center">
<asp:Image id="imgPhoto" runat="server"
EnableViewState="False"></asp:Image><BR>
<asp:Label id="lblCaption" runat="server" EnableViewState="False"
Font-Size="X-Small" Width="200px"></asp:Label><br>
</div>
The Class's .aspx.vb File (the "Web Form Designer Generated Code"
section is not included here, I did not modify that part):

Public Class captionimage
Inherits System.Web.UI.UserControl

Public caption As String = ""
Public photo As String = ""

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
i.Dispose() 'Without this line, you may recieve an error saying
another process is using the file when you try to delete it
imgPhoto.Width = New Unit(200)
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
End Class
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT
contentid,textcaption,content FROM newsinfo WHERE contenttype='image'",
Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
The 2 Places I call RefreshResults() From:

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

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack() Then Me.RefreshResults()
End Sub
The error is recieved only on postbacks (no matter what I do to cause
the postback, as far as I can tell). If I remove the line in
Page_PreRender Then I do not recieve the error, but doing that obviously
means that my page does not get updated. Another thing that is confusing
me, whether it is related I cannot tell, but in the Template Editor I
see the following:
Error Creating Control

Did I forget something in the UserControl's code? Why is it having a
problem creating the control? I don't think it matters for now, but
since the line shown in the error is in the control's code, I though it
would be good to mention this. Thanks.
--
Nathan Sokalski
nj********@hotmail.com <mailto:nj********@hotmail.com>
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com <mailto:as*****@thisisjunk.com>> wrote
in message news:uU**************@TK2MSFTNGP04.phx.gbl...
> the problem would be that grid would bind again and event would be lost,
> because the grid has lost the view state information,
>
> on the itemcommand event you should be able to datakey value without
> having to rebind it,
> you can post more code if you want
>
>
>
>
>
> Nathan Sokalski wrote:
>> When I do an If Not IsPostBack() in the Page_Load, then one of my other
>> events does not work because it uses the DataKeys property, which is not >> completely created until after databinding. Any other ideas?

May 22 '06 #6
The place where I set the photo and caption variables is in the .aspx file's
tag for the control by using databinding. This gives the desired results
during the initial load. Here is the section of the code that contains the
tag I am referring to:
<asp:datalist id="datImages" runat="server" DataKeyField="contentid"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id=CapImg runat="server" EnableViewState="true"
photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR>
<asp:Button id="btnDeleteImg" runat="server" Text="Delete"
EnableViewState="False" Font-Bold="True" CausesValidation="False"
CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
Notice that the tag for the captionimage contains attributes called photo
and caption. From some of the debugging that I tried to do when I first
started recieving this error it seemed like the control was being created
before the databinding, which other than the initial load it is due to the
fact that my page's Load event has the call to Me.RefreshResults() inside
the following if statement:

If Not IsPostBack() Then Me.RefreshResults()

It almost feels like I need to somehow wait until after databinding to
execute the captionimage's code, but I believe the page's Load event
automatically calls the captionimage's Load event. I'm not sure what to do
from here.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com> wrote in message
news:OD**************@TK2MSFTNGP05.phx.gbl...
Nathan,
the photo and caption variables in your control are not being set (
atleast in the code that i see), do you want to perform the image
operation on some action ?

Nathan Sokalski wrote:
OK, but I am still having a problem that happens only on postbacks. The
problem (or at least the error line it shows in the error, I think the
error is somewhere else) is in the extra little class I wrote to display
images with captions. Here is a screenshot of the error I am shown:
Server Error in '/VAPresenters' Application.
------------------------------------------------------------------------
c:\inetpub\wwwroot\VAPresenters

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException:
c:\inetpub\wwwroot\VAPresenters

Source Error:

Line 27: Line 28: Private Sub Page_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Line 29: Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
Line 30: imgPhoto.Height = New Unit(CInt(200 * i.Height /
i.Width))
Line 31: i.Dispose() 'Without this line, you may recieve an error
saying another process is using the file when you try to delete it
Source File: c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb
Line: 29

Stack Trace:

[FileNotFoundException: c:\inetpub\wwwroot\VAPresenters]
System.Drawing.Image.FromFile(String filename, Boolean
useEmbeddedColorManagement) +204
System.Drawing.Image.FromFile(String filename) +7
VAPresenters.captionimage.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\VAPresenters\captionimage.ascx. vb:29
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Control.LoadRecursive() +98
System.Web.UI.Control.AddedControl(Control control, Int32 index) +307
System.Web.UI.ControlCollection.Add(Control child) +153
System.Web.UI.WebControls.DataList.CreateItem(Int3 2 itemIndex,
ListItemType itemType, Boolean dataBind, Object dataItem) +111
System.Web.UI.WebControls.DataList.CreateControlHi erarchy(Boolean
useDataSource) +686
System.Web.UI.WebControls.BaseDataList.CreateChild Controls() +61
System.Web.UI.Control.EnsureChildControls() +100
System.Web.UI.WebControls.BaseDataList.get_Control s() +12
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e) +20
System.Web.UI.WebControls.BaseDataList.DataBind() +23
VAPresenters.newsinfoadmin.RefreshResults() in
c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:64
VAPresenters.newsinfoadmin.datLinksFiles_PreRender (Object sender,
EventArgs e) in c:\inetpub\wwwroot\VAPresenters\newsinfoadmin.aspx .vb:192
System.Web.UI.Control.OnPreRender(EventArgs e) +67
System.Web.UI.Control.PreRenderRecursiveInternal() +62
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Control.PreRenderRecursiveInternal() +125
System.Web.UI.Page.ProcessRequestMain() +1476
------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032
Here is the class code and the couple other things that might be
relevant:
The Class's .aspx File:
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="captionimage.ascx.vb" Inherits="VAPresenters.captionimage"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div align="center">
<asp:Image id="imgPhoto" runat="server"
EnableViewState="False"></asp:Image><BR>
<asp:Label id="lblCaption" runat="server" EnableViewState="False"
Font-Size="X-Small" Width="200px"></asp:Label><br>
</div>
The Class's .aspx.vb File (the "Web Form Designer Generated Code"
section is not included here, I did not modify that part):
Public Class captionimage
Inherits System.Web.UI.UserControl
Public caption As String = ""
Public photo As String = ""
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim i As System.Drawing.Image =
System.Drawing.Image.FromFile(MapPath(photo))
imgPhoto.Height = New Unit(CInt(200 * i.Height / i.Width))
i.Dispose() 'Without this line, you may recieve an error saying
another process is using the file when you try to delete it
imgPhoto.Width = New Unit(200)
imgPhoto.ImageUrl = photo
lblCaption.Text = caption.Trim()
End Sub
End Class
The Me.RefreshResults() Method:

Private Sub RefreshResults()
Dim imagedatatable As New DataTable
Dim imageadapter As New OleDbDataAdapter("SELECT
contentid,textcaption,content FROM newsinfo WHERE contenttype='image'",
Global.connectionstring)

imageadapter.Fill(imagedatatable)
datImages.DataSource = imagedatatable
datImages.DataBind()
End Sub
The 2 Places I call RefreshResults() From:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack() Then Me.RefreshResults()
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
If IsPostBack() Then Me.RefreshResults()
End Sub
The error is recieved only on postbacks (no matter what I do to cause
the postback, as far as I can tell). If I remove the line in
Page_PreRender Then I do not recieve the error, but doing that obviously
means that my page does not get updated. Another thing that is confusing
me, whether it is related I cannot tell, but in the Template Editor I see
the following:
Error Creating Control
Did I forget something in the UserControl's code? Why is it having a
problem creating the control? I don't think it matters for now, but since
the line shown in the error is in the control's code, I though it would
be good to mention this. Thanks.
--
Nathan Sokalski
nj********@hotmail.com <mailto:nj********@hotmail.com>
http://www.nathansokalski.com/
"Ashish" <as*****@thisisjunk.com <mailto:as*****@thisisjunk.com>> wrote
in message news:uU**************@TK2MSFTNGP04.phx.gbl...
> the problem would be that grid would bind again and event would be

lost,
> because the grid has lost the view state information,
>
> on the itemcommand event you should be able to datakey value without
> having to rebind it,
> you can post more code if you want
>
>
>
>
>
> Nathan Sokalski wrote:
>> When I do an If Not IsPostBack() in the Page_Load, then one of my

other
>> events does not work because it uses the DataKeys property, which is

not
>> completely created until after databinding. Any other ideas?

May 23 '06 #7
what if you do the photo operation from the postback of the button event
(or OnItemCommand) instead of page_load ?

Nathan Sokalski wrote:
The place where I set the photo and caption variables is in the .aspx file's
tag for the control by using databinding. This gives the desired results
during the initial load. Here is the section of the code that contains the
tag I am referring to:
<asp:datalist id="datImages" runat="server" DataKeyField="contentid"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id=CapImg runat="server" EnableViewState="true"
photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR>
<asp:Button id="btnDeleteImg" runat="server" Text="Delete"
EnableViewState="False" Font-Bold="True" CausesValidation="False"
CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
Notice that the tag for the captionimage contains attributes called photo
and caption. From some of the debugging that I tried to do when I first
started recieving this error it seemed like the control was being created
before the databinding, which other than the initial load it is due to the
fact that my page's Load event has the call to Me.RefreshResults() inside
the following if statement:

If Not IsPostBack() Then Me.RefreshResults()

It almost feels like I need to somehow wait until after databinding to
execute the captionimage's code, but I believe the page's Load event
automatically calls the captionimage's Load event. I'm not sure what to do
from here.

May 23 '06 #8
I might have to end up doing that, but if I were to do that, there wouldn't
be much point in even creating a UserControl, since it is so small and the
photo operation is the only code that is in the UserControl. I might need to
do that due to time restraints. I will also try this other idea. What if I
put the photo operation code in the UserControl's PreRender event? would it
get called immediately after the UserControl's Load event, or would it get
called from the page's PreRender event? Either way, thanks for your
suggestion, hopefully one of these two ideas will work. Thanks, I will let
you know.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
what if you do the photo operation from the postback of the button event
(or OnItemCommand) instead of page_load ?

Nathan Sokalski wrote:
The place where I set the photo and caption variables is in the .aspx
file's tag for the control by using databinding. This gives the desired
results during the initial load. Here is the section of the code that
contains the tag I am referring to:
<asp:datalist id="datImages" runat="server" DataKeyField="contentid"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id=CapImg runat="server" EnableViewState="true"
photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR>
<asp:Button id="btnDeleteImg" runat="server" Text="Delete"
EnableViewState="False" Font-Bold="True" CausesValidation="False"
CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
Notice that the tag for the captionimage contains attributes called photo
and caption. From some of the debugging that I tried to do when I first
started recieving this error it seemed like the control was being created
before the databinding, which other than the initial load it is due to
the fact that my page's Load event has the call to Me.RefreshResults()
inside the following if statement:

If Not IsPostBack() Then Me.RefreshResults()

It almost feels like I need to somehow wait until after databinding to
execute the captionimage's code, but I believe the page's Load event
automatically calls the captionimage's Load event. I'm not sure what to
do from here.

May 23 '06 #9
My PreRender idea worked! Thank you for all your help, you were a big help
in achieving my goal for this project, I appreciate it.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
I might have to end up doing that, but if I were to do that, there wouldn't
be much point in even creating a UserControl, since it is so small and the
photo operation is the only code that is in the UserControl. I might need
to do that due to time restraints. I will also try this other idea. What if
I put the photo operation code in the UserControl's PreRender event? would
it get called immediately after the UserControl's Load event, or would it
get called from the page's PreRender event? Either way, thanks for your
suggestion, hopefully one of these two ideas will work. Thanks, I will let
you know.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Ashish" <as*****@thisisjunk.com> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
what if you do the photo operation from the postback of the button event
(or OnItemCommand) instead of page_load ?

Nathan Sokalski wrote:
The place where I set the photo and caption variables is in the .aspx
file's tag for the control by using databinding. This gives the desired
results during the initial load. Here is the section of the code that
contains the tag I am referring to:
<asp:datalist id="datImages" runat="server" DataKeyField="contentid"
CellPadding="0" CellSpacing="5" RepeatColumns="3"
RepeatDirection="Horizontal">
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Bottom"></ItemStyle>
<ItemTemplate>
<uc1:captionimage id=CapImg runat="server"
EnableViewState="true" photo='<%#
DataBinder.Eval(Container,"DataItem.content","news info/{0}").Trim() %>'
caption='<%# DataBinder.Eval(Container, "DataItem.textcaption").Trim()
%>'></uc1:captionimage><BR>
<asp:Button id="btnDeleteImg" runat="server" Text="Delete"
EnableViewState="False" Font-Bold="True" CausesValidation="False"
CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:datalist>
Notice that the tag for the captionimage contains attributes called
photo and caption. From some of the debugging that I tried to do when I
first started recieving this error it seemed like the control was being
created before the databinding, which other than the initial load it is
due to the fact that my page's Load event has the call to
Me.RefreshResults() inside the following if statement:

If Not IsPostBack() Then Me.RefreshResults()

It almost feels like I need to somehow wait until after databinding to
execute the captionimage's code, but I believe the page's Load event
automatically calls the captionimage's Load event. I'm not sure what to
do from here.


May 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richard | last post by:
Hi, I'm having issues with DataBinding. I have a Form with two data bound comboboxes on it. When you select something in one combobox I may {or may not} want to change the selected item in the...
7
by: Dan Nash | last post by:
Hi guys Ok, i have an aspx with a user control. In the user controls pageload i set a property (mystring = "test";). i then want to access that from the page_load of the aspx thats using the...
0
by: louise raisbeck | last post by:
Hi there, I am databinding a datagrid on the onload with the usual !IsPostBack condition. I have to create some dynamic checkboxes, because i dont know how many i will need until the dataset...
0
by: Scott McChesney | last post by:
I have a problem I hope you folks can help me with. I have an application that is using a tab-based interface, with the ability for users to drag an item from a ListBox onto the tab control. ...
3
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
0
by: craigkenisston | last post by:
I have a form with a repeater that is filled from a BLL object, and a dropdownlist. The dropdownlist has a parameters that is used in the BLL class, the number of rows about to be displayed. I...
0
by: seigo | last post by:
Hello, I faced with the following problem. I have a PlaceHolder on a page and a few UserControls which have custom events, for instance: public delegate void SelectHandler(object sender,...
1
by: Rex the Strange | last post by:
I'm having a problem here and am looking for the answer. I have two listboxes. One is a dropdown and the other is a simple list of items which is databound . I want to be able to allow the user to...
4
by: Joergen Bech | last post by:
Just out of curiosity: What is your favorite method of making sure that anything that happens on a form, only happens in response to a single, external event? Take the example below. I have made...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.