Order of events, databinding, and UserControls

Discussion in 'ASP .Net Datagrid Control' started by Nathan Sokalski, May 21, 2006.

  1. 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","newsinfo/{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

    http://www.nathansokalski.com/
    Nathan Sokalski, May 21, 2006
    #1
    1. Advertising

  2. Nathan Sokalski

    Ashish Guest

    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","newsinfo/{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, May 22, 2006
    #2
    1. Advertising

  3. 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

    http://www.nathansokalski.com/

    "Ashish" <> wrote in message
    news:%...
    > 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","newsinfo/{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, May 22, 2006
    #3
  4. Nathan Sokalski

    Ashish Guest

    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, May 22, 2006
    #4
  5. Nathan Sokalski

    Ashish Guest

    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(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +111
    > System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +686
    > System.Web.UI.WebControls.BaseDataList.CreateChildControls() +61
    > System.Web.UI.Control.EnsureChildControls() +100
    > System.Web.UI.WebControls.BaseDataList.get_Controls() +12
    > System.Web.UI.WebControls.BaseDataList.OnDataBinding(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
    > <mailto:>
    > http://www.nathansokalski.com/
    >
    > "Ashish" < <mailto:>> wrote
    > in message news:...
    > > 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, May 22, 2006
    #5
  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","newsinfo/{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

    http://www.nathansokalski.com/

    "Ashish" <> wrote in message
    news:...
    > 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(Int32 itemIndex,
    >> ListItemType itemType, Boolean dataBind, Object dataItem) +111
    >> System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean
    >> useDataSource) +686
    >> System.Web.UI.WebControls.BaseDataList.CreateChildControls() +61
    >> System.Web.UI.Control.EnsureChildControls() +100
    >> System.Web.UI.WebControls.BaseDataList.get_Controls() +12
    >> System.Web.UI.WebControls.BaseDataList.OnDataBinding(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
    >> <mailto:>
    >> http://www.nathansokalski.com/
    >> "Ashish" < <mailto:>> wrote
    >> in message news:...
    >> > 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, May 23, 2006
    #6
  7. Nathan Sokalski

    Ashish Guest

    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","newsinfo/{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.
    Ashish, May 23, 2006
    #7
  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

    http://www.nathansokalski.com/

    "Ashish" <> wrote in message
    news:%23%...
    > 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","newsinfo/{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, May 23, 2006
    #8
  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

    http://www.nathansokalski.com/

    "Nathan Sokalski" <> wrote in message
    news:...
    >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
    >
    > http://www.nathansokalski.com/
    >
    > "Ashish" <> wrote in message
    > news:%23%...
    >> 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","newsinfo/{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, May 23, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Order of events, databinding, and UserControls

    Nathan Sokalski, May 21, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    450
    Nathan Sokalski
    May 23, 2006
  2. Replies:
    0
    Views:
    384
  3. Rex the Strange

    Databinding and order of events

    Rex the Strange, Nov 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    344
    Rex the Strange
    Nov 17, 2006
  4. Gokhan Akca

    order of usercontrols processed by events of page

    Gokhan Akca, May 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    155
    Brock Allen
    May 30, 2005
  5. Nathan Sokalski

    Order of events, databinding, and UserControls

    Nathan Sokalski, May 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    8
    Views:
    148
    Nathan Sokalski
    May 23, 2006
Loading...

Share This Page