Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   Order of events, databinding, and UserControls (http://www.velocityreviews.com/forums/t762778-order-of-events-databinding-and-usercontrols.html)

Nathan Sokalski 05-21-2006 07:34 PM

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
njsokalski@hotmail.com
http://www.nathansokalski.com/



Ashish 05-22-2006 01:58 AM

Re: Order of events, databinding, and UserControls
 
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.


Nathan Sokalski 05-22-2006 04:45 AM

Re: Order of events, databinding, and UserControls
 
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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Ashish" <asharma@thisisjunk.com> wrote in message
news:%23gzb2MUfGHA.1208@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.




Ashish 05-22-2006 03:15 PM

Re: Order of events, databinding, and UserControls
 
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?


Ashish 05-22-2006 07:54 PM

Re: Order of events, databinding, and UserControls
 
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
> njsokalski@hotmail.com <mailto:njsokalski@hotmail.com>
> http://www.nathansokalski.com/
>
> "Ashish" <asharma@thisisjunk.com <mailto:asharma@thisisjunk.com>> wrote
> in message news:uULreKbfGHA.1320@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?


Nathan Sokalski 05-23-2006 01:04 AM

Re: Order of events, databinding, and UserControls
 
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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Ashish" <asharma@thisisjunk.com> wrote in message
news:ODHyCmdfGHA.3456@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
>> njsokalski@hotmail.com <mailto:njsokalski@hotmail.com>
>> http://www.nathansokalski.com/
>> "Ashish" <asharma@thisisjunk.com <mailto:asharma@thisisjunk.com>> wrote
>> in message news:uULreKbfGHA.1320@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?




Ashish 05-23-2006 03:14 PM

Re: Order of events, databinding, and UserControls
 
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.


Nathan Sokalski 05-23-2006 04:17 PM

Re: Order of events, databinding, and UserControls
 
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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Ashish" <asharma@thisisjunk.com> wrote in message
news:%23%23ITOunfGHA.1520@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.




Nathan Sokalski 05-23-2006 04:32 PM

Re: Order of events, databinding, and UserControls
 
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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
news:OGS3RRofGHA.3364@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
> njsokalski@hotmail.com
> http://www.nathansokalski.com/
>
> "Ashish" <asharma@thisisjunk.com> wrote in message
> news:%23%23ITOunfGHA.1520@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.

>
>





All times are GMT. The time now is 11:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.