N
Nathan Sokalski
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.
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.